Exception handling in java is one of the powerful mechanisms to handle runtime errors caused by exceptions. Exception handling plays an important role in software development.
Let ‘s understand what is Exception in java?
Exception means abnormal condition. In Java, an exception is an event that disrupts the normal flow of the program. It is an object which is thrown at runtime.
What is Exception Handling in Java?
Exception handling in java helps in minimizing exceptions and helps in recovering from exceptions. It is one of the powerful mechanisms to handle runtime exceptions and makes it bug-free.. An exception handling is defined as an abnormal condition that may happen at runtime and disturb the normal flow of the program.
Example of Exception Handling
Let's consider a scenario:
1. statement 1;
2. statement 2;
3. statement 3;
4. statement 4;
5. statement 5;//exception occurs
6. statement 6;
7. statement 7;
Suppose there are 7 statements in a Java program and an exception occurs at statement 5; the rest of the code will not be executed, i.e., statements 6 and 7 will not be executed. However, when we perform exception handling, the rest of the statements will be executed. That is why we use exception handling in java.
Hierarchy of java Exception Classes
The java.lang.Throwable class is the root class of Java Exception hierarchy inherited by two subclasses: Exception and Error. The hierarchy of Java Exception classes is given below:
Before understanding types of exception let us understand what is difference between exception and error?
What is Exception?
As we earlier discussed. Exception is an event that occurs during the execution of the program and interrupts the normal flow of program instructions. These are the errors that occur at compile time and run time. It occurs in the code written by the developers. It can be recovered by using the try-catch block and throws keyword. There are two types of exceptions i.e. checked and unchecked.
What is an Error?
Errors are problems that mainly occur due to the lack of system resources. It cannot be caught or handled. It indicates a serious problem. It occurs at run time. These are always unchecked.AnexampleoferrorsisStackOverFlowError,VirtualMachineError,OutOfMemoryError, etc. are the subclasses of the Error class.
Types of java Exceptions
Checked Exceptions
Those exceptions that are checked at compile-time comprises checked exceptions.
They are child classes of Exception except for RuntimeException.
The program will not compile if they are not handled.
Example: IOException, ClassNotFoundException, etc.
Unchecked Exceptions
Those exceptions that are checked at runtime comprises unchecked exceptions.
They are child classes of RuntimeException.
They give runtime errors if not handled explicitly.
Example: ArithmeticException, NullPointerException etc.
Keywords used in Exception Handling
Java provides five keywords that are used to handle the exception. The following table describes each.
1. try: The try keyword used to a specify a block where an exception can occur. It is always followed by catch block, which handles the exception that occurs in associated try block. A try block must be followed by catch block or finally block or both.
2. Catch: The "catch" block is used to handle the exception. This block must follow the try block and a single try block can have several catch blocks associated with it. When an exception occurs in a try block, the corresponding catch block that handles that particular exception executes. It can be followed by finally block later.
3. Finally: A finally block contains all the crucial statements that must be executed whether an exception occurs or not. The statements present in this block will always execute, regardless an exception occurs in the try block or not such as closing a connection, stream etc.
4. throw: The "throw" keyword is used to throw an exception.
5. throws: The "throws" keyword is used to declare exceptions. It specifies that there may occur an exception in the method. It doesn't throw an exception. It is always used with method signature.
Comentários