JAVA
What is an
Exception?
An exception is an
unwanted or unexpected event, which occurs during the execution of a program
i.e at run time, that disrupts the normal flow of the program’s instructions.
Error vs Exception
The exception
handling in java is one of the powerful mechanism to handle the runtime errors so
that normal flow of the application can be maintained.
The core advantage of exception handling is to maintain the normal flow of the
application. Exception normally disrupts the normal flow of the
application that is why we use exception handling.
Let's take 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;
8. statement 8;
9. statement 9;
10. statement 10;
Suppose there is 10 statements in your program and there
occurs an exception at statement 5, rest of the code will not be executed i.e.
statement 6 to 10 will not run. If we perform exception handling, rest of the
statement will be executed. That is why we use exception handling in java.
1.
Checked
Exception
2.
Unchecked
Exception
3.
Error
The classes that extend Throwable class except
RuntimeException and Error are known as checked exceptions e.g.IOException,
SQLException etc. Checked exceptions are checked at compile-time.
The classes that extend RuntimeException are known as
unchecked exceptions e.g. ArithmeticException, NullPointerException,
ArrayIndexOutOfBoundsException etc. Unchecked exceptions are not checked at
compile-time rather they are checked at runtime.
Error is irrecoverable e.g. OutOfMemoryError,
VirtualMachineError, AssertionError etc.
There are 5 keywords used in java exception handling.
1.
try
2.
catch
3.
finally
4.
throw
5.
throws
Java try block is used to enclose the code that might
throw an exception. It must be used within the method.
Java try block must be followed by either catch or
finally block.
public class TryCatch{
public class //code
that may throw exception
public class catch(Exception_class_Name
ref){}
public class Try{
public class //code
that may throw exception
public class }finally(){}
Java catch block is used to handle the Exception. It must
be used after the try block only.
You can use multiple catch block with a single try.
Let's try to understand the problem if we don't use
try-catch block.
public class TryCatch{
public static void main(String args[]){
int data=50/0
System.out.println
}
}
Output:
Exception in thread main java.lang.ArithmeticException:/
by zero
As displayed in the above example, rest of the code is not executed (in such case, rest of the code... statement is not printed).There can be 100 lines of code after exception. So all the code after exception will not be executed.
Let's see the solution of above problem by java try-catch
block.
public class TryCatch1{
public static void main(String args[]){
class Try{
int data=50/0
}catch(ArithmeticException
e){System.out.println(e)}
}System.out.println("rest
of the code");
}
}
Output:Exception in thread main
java.lang.ArithmeticException:/ by zero
Internal working of java try-catch block
The JVM firstly checks whether the exception is handled
or not. If exception is not handled, JVM provides a default exception handler
that performs the following tasks: