Exception handling in Java:What and Why?
Firstly it is one of the strong mechanisms to handle run period errors. So that the normal flow of the application can be controlled. Secondly, on this content, we will learn about Exception Handling in Java.
What is Exception in Java?
Firstly According to Dictionary Meaning: Exception is an abnormal type. Secondly in Java, an exception is an event that stops the normal flow of the program. Besides, it is an object which is thrown at runtime.It is a mechanism to handle run period errors.
Advantage of Exception Handling
The main advantage is to keep the normal flow of the application. An exception normally stops the normal flow of
Firstly let’s see an example of where we are using a try-catch statement to hold the exception.
public class JavaExceptionExample{
public static void main(String args[]){
try{
//code that may raise an exception
int data=100/0;
}catch(ArithmeticException e){System.out.println(e);}
//rest code of the program
System.out.println(“rest of the code…”);
}
}.
The Output of this program: Exception in thread main java.lang.ArithmeticException:/ by zero
rest of the code.
Types of Java Exceptions
Firstly there are two types: checked and unchecked. Secondly, an error is considered as an unchecked exception. As per Oracle, there are three types of exceptions too:
Checked Exception
The classes which straightly take over throwable class except for Run Period Exception. And Error is known as checked exceptions.
Unchecked Exception
The classes which take over Runtime Exception are known as unchecked exceptions
Error
Mistake so-called Error is irrecoverable.
What are the Great Practices of Exception Handling in Java?
- Use Specific Exceptions.
- Throw Early or Fail-Fast.
- Catch Late.
- Closing Resources.
- Logging Exceptions.
- Single catch block for multiple exceptions.
- Using Custom Exceptions.
- Naming Conventions and Packaging.
- Use Exceptions Judiciously.
- Document the Exceptions Thrown.
What are the Java Exception Handling Keywords?
Java exception handling mainly controlled through five keywords: try, catch, throw, throws, and finally. Here is the basic form of this block too.
try {
// block of code
}
catch ( ExceptionType1 e) {
// Exception handling routine for ExceptionType1 (optional)
}
catch (ExceptionType2 e ) {
// Exception handling routine for ExceptionType2 (optional)
}
catch (ExceptionType_n e) {
// Exception handling routine for ExceptionType_n (optional)
}
finally {
// Program code of exit (optional)
}.
Check out this post to know more on exception handling in Java
Keywords for Java Exception Handling?
Firstly we will watch them, and secondly, we will write a simple program. Hence showing how to use them.
Throw
We know that if an exception happens, an exception object is created and then the Java run period starts to handle them. Sometimes we may want to generate exceptions clearly in our code, for example in a user proof program we should show an exception to the client if the password is null. the throw keyword is used to throw an exception to the run period to handle it.
Throws
When we are throwing an exception in a method and not picking it. Besides, we should use throws keyword in the method signature to let the caller program know the exceptions that may be thrown by the method. The caller method might hold these exceptions to its caller method using throws keyword. Finally, we can provide several exceptions in the throws clause and it can be utilized with the main() method also.
Try-catch
We use a try-catch bar for exception handling in our code. Besides, it is the beginning of the block and catch is at the end of the try block to hold the exceptions.
Furthermore, we can have many catch blocks with a try and try-catch block too. Hence catch block has a parameter that should be of part Exception.
check out this post to know more on java vs C++
Finally
finally block is choosable and can be utilized only with a try-catch block. Since the exception stops the process of execution, we may have some sources open that will not get closed, so we can finally block. Finally, the block gets exhibited always, whether an exception occurred or not.
Conclusion
Firstly Java Exceptions are the best way of holding exceptions. Furthermore, it guarantees program integrity in any situation. Secondly, Programmers should always try to utilize exceptions as it makes their code better. Finally and most importantly this content gives you in detail on Exception Handling in Java.
check this post to learn more on java servelant.