In the above program:
Case 1:
java ExceptionTest 1 (enter value of x as 1 from command line)
10
Case 2:
java ExceptionTest 0 (enter value of x as 0 from command line)
Enter correct value
Get 100% Hike!
Master Most in Demand Skills Now!
Finally Block
There are some cases in which statements after catch block will not be executed:
- When you have return statement inside the try and catch block.
- When exception occurs in the try block and matching catch block is not found.
- When exception occurs in the catch block, etc.
If you want to execute statements after catch block which fails to execute, then you have those statements in the finally block.
Syntax:
try{
// statements
} catch(){
// statements
}finally{
//statements
}
Program to understand finally block
In the above program, finally block will be executed always.
Types of Exception in Java
There are two types of exceptions:
- Checked exceptions
- Unchecked exceptions
Checked Exception
- These are also called compile time exceptions.
- All the subclasses of java.lang.Exception except java.lang.RuntimeException and its sub classes are checked exception.
- In other words, if you have any java statement that may cause any exception and that exception is verified by the compiler by forcing you to report the exceptions are called checked exceptions.
- Checked Exception must be reported in any one of the two ways:
- Using try-catch block
- Propagating the exception using throws
- If you are not reporting checked exception then you will get the following error message at compile time:
Unreported exception <ExceptionName>; must be caught or declared to be thrown.
Unchecked Exception
- These are also called as runtime exceptions.
- lang.RuntimeException and its subclasses are unchecked exceptions.
- If you have any java statement that may cause any exception and that exception is not verified by the compiler at compile time, then those exceptions are called as unchecked exceptions.
- In case of unchecked exception, compiler is not responsible to verify whether you are reporting about the exception or not, i.e. unchecked exceptions may or may not be reported.
Throw keyword
- throw is a keyword used to throw exceptions explicitly.
- You can throw any checked or unchecked exceptions.
- You can throw any built-in or user-defined exceptions.
Syntax:
throw <throwableTypeObjectRef>
Example:
throw new NullPointerException();
throw new StudentNotFoundException();
Throws keyword
- throws is a keyword used to propagate the exceptions to the caller method by specifying at method level.
- You can define any checked or unchecked exceptions at method level.
- You can define any built-in or user defined exceptions at method level.
- When the exception is unchecked, then throws keyword is optional. But for checked exception throws keyword is mandatory.
User Defined Exception
The exceptions which are defined and implemented by application developer as per project requirements are called user defined exceptions.
Example:
StudentNotFoundException
InvalidDetailsException
UnsuccessfulAttemptsException
Conclusion
This brings us to the end of our discussion on exception handling. In this tutorial, we have learned in detail about the overview of exception handling in Java.
We have also covered almost all of the main parts of exception handling. If you want to learn more, I would suggest trying our Intellipaat Java course, which covers in-depth knowledge of most of the important topics in Java, such as basics, arrays, exception handling, multithreading, and more. This will help you become a good Java programmer.
Join the Intellipaat Java training course and certification today, and let your career soar toward the realm of software development.