Exception handling in Java? – User friendly Tech help
Exception handling plays a crucial part in mastering Selenium, and designing Frameworks, thus we thought of digging deeper into this topic.We have also discussed about ways of handling Exceptions.
n
nLet’s begin with keynotes before starting with the detailed explanation:-
n
- n
- 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.
n
n
n n
n
n SuperClass and SubClass in Java n
n
n
- 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.
n
n
n
n
n
n
n
n
n
Do like our Facebook page to learn more.
n
nWhat is Exception?
n
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.
n
nIt can be in the form of invalid input data, type mismatch, File not found, and network failure and many more.
n
nFlow of Exception:-
n
nError ->Exception object ->Caught
n
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.
n
nNow 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.
n
n
nTypes of Exception:-
n
Exception Hierarchy |
n
nChecked exception: –
nThese exceptions are checked at compile time and it refers to the exceptional scenario that a well-written application should try to recover from.
n
nIt 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
n
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.
nMore examples NoSuchMethodException, ClassNotFoundException.
n
nUnchecked Exception: –
nThese 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.
n
nIt consists of Error and Runtime Exceptions.
n
nError: –
nIt 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.
n
nFor 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.
n
nRuntime Exception: –
nThese 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.
n
nFor 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.
n
nMore examples, dividing by zero(ArithmeticException), attempting to access an array element through an index that does not exists(ArrayIndexOutOfBoundsException)
n
nNote: –
nRuntime 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.
n
nNow we are done with the basics of Exception and its types in Java, in our next post we would learn handling Java exceptions.