Let’s begin with keynotes before starting with the detailed explanation:-
- An exception is any event, which interrupts the normal flow of the program execution.
- ‘Throwable‘ class is the superclass (remember Object class is the superclass of all the Java classes) of all errors and exceptions in the Java language.
SuperClass and SubClass in Java
- Types of Exceptions ‘checked exceptions’ and ‘unchecked exceptions’. All RuntimeExceptions and Errors are unchecked exceptions. Rest of the exceptions are called checked exceptions.
- Checked exceptions should be handled in the code to avoid compile time errors.
- Exceptions can be handled by using ‘try-catch‘ block. Try block contains the code to be handled from exception, while catch block contains the remedy for the exception.
- The finally block after a try … catch statement is executed regardless whether an exception is caught or not.
- Incase a method doesn’t handle the exception, hence it is mandatory to specify the exception type in the method signature using ‘throws‘ clause.
- We can explicitly throw an exception using ‘throw‘ clause.
Do like our Facebook page to learn more.
What is Exception?
An exception is a problem, which occurs during the execution of a program, and it results in interrupting the normal flow of the program’s implementation.
It can be in the form of invalid input data, type mismatch, File not found, and network failure and many more.
Flow of Exception:-
Error ->Exception object ->Caught
Error in the program, results in the creation of Exception object at runtime which contains information about the error, including its type and the state of the program when the error occurred.
Now Java runtime attempts to find a block of code (Exception Handler) that can handle the exception in our program. It starts searching from the current method to calling methods in reverse and at last the main method. If an appropriate handler code is found then the exception is handled normally, otherwise program terminates.
Types of Exception:-
Checked exception: –
These exceptions are checked at compile time and it refers to the exceptional scenario that a well-written application should try to recover from.
It means if a method is throwing a checked exception then it should handle the exception using try-catch block or it should declare the exception using throws keyword, otherwise the program will give a compilation error. That is the reason it is known as checked Exception because these exceptions are checked at Compile time
For example, while working with external files, incase we try to compile the code without any exceptional handling,compilation error will be thrown and user is forced to handle that, before starting with the execution.So we need to add try-catch block or need to throw IOException.
More examples NoSuchMethodException, ClassNotFoundException.
Unchecked Exception: –
These are not checked at compile time, it means if our program is throwing an unchecked exception and even if we didn’t handle/declare that exception, the program won’t give a compilation error.
It consists of Error and Runtime Exceptions.
It is type of exceptional conditions that are external to the application, and that the application usually cannot anticipate or recover from. Typical Java programs should not catch Errors.
For example, say our code is able to open the file for reading but due to hardware or system failure, this operation is not successful. The unsuccessful read will throw java.io.IOError. An application might choose to catch this exception, in order to notify the user of the problem or it also might make sense for the program to print a stack trace and exit.
Runtime Exception: –
These are exceptional conditions that are internal to the application, and represent problems that are the result of a programming, such as logic errors or improper use of an API.
For example, we are able to pass the filename to FileReader method but our logic passes a null object to the method, thus ‘NullPointerException’ will be thrown. The application can catch this exception, but it probably makes more sense to eliminate the bug that caused the exception to occur.
More examples, dividing by zero(ArithmeticException), attempting to access an array element through an index that does not exists(ArrayIndexOutOfBoundsException)
Runtime exceptions can occur anywhere in the application flow.Does this means we need to add runtime exceptions in every method declaration, where it can happen?.However it would reduce a program’s clarity. Furthermore, the compiler does not require that we catch or specify runtime exceptions (although we can).So we generally avoid handling Runtime Exception.
Now we are done with the basics of Exception and its types in Java, in our next post we would learn handling Java exceptions.