Chapter 3 Exception Handling
Chapter 3 Exception Handling
Chapter 3 Exception Handling
Errors in program
• Errors are most of the time part of software development even for a
seasoned developers.
• There are basically 3 types of errors
• 1. compile time error-
• 2. Runtime error- this error represents inefficency of the computer
system to execute particular statement
• 3. Logical error-
o Boolean b;b++;- operator can’t be applied
o Int I=1233333;- incompatible type
o Losing precision
Exception handling
2) Unchecked Exception
The classes which inherit RuntimeException are known as unchecked exceptions e.g. ArithmeticException,
NullPointerException, ArrayIndexOutOfBoundsException etc. Unchecked exceptions are not checked at compile-time, but
they are checked at runtime.
3) Error
Error is irrecoverable e.g. OutOfMemoryError, VirtualMachineError, AssertionError etc.
Java Exception Keywords
There are 5 keywords which are used in handling exceptions in Java.
Keyword Description
finally The "finally" block is used to execute the important code of the
program. It is executed whether an exception is handled or not.
Let's see an example of Java Exception Handling where we using a try-catch statement to handle the exception.
public class JavaExceptionExample{
public static void main(String args[]){
try{
//code that may raise exception
int data=100/0;
}catch(ArithmeticException e){System.out.println(e);}
//rest code of the program
System.out.println("rest of the code...");
}
}
Output:
Exception in thread main java.lang.ArithmeticException:/ by zero
Exception in thread main java.lang.ArithmeticException:/ by zero rest of the code...rest of the code...
Common Scenarios of Java Exceptions Example
There are given some scenarios where unchecked exceptions may occur. They are as
follows:
1) A scenario where ArithmeticException occurs
If we divide any number by zero, there occurs an ArithmeticException.
int a=50/0;//ArithmeticException
String s="abc";
int i=Integer.parseInt(s);//NumberFormatException
Java try block is used to enclose the code that might throw an exception. It must be used within the
method.
If an exception occurs at the particular statement of try block, the rest of the block code will not
execute. So, it is recommended not to keeping the code in try block that will not throw an exception.
Java try block must be followed by either catch or finally block.
try{
//code that may throw an exception
}catch(Exception_class_Name ref){}
try{
//code that may throw an exception
}finally{}
Java catch block
• Java catch block is used to handle the Exception by declaring the type
of exception within the parameter. The declared exception must be the
parent class exception ( i.e., Exception) or the generated exception
type. However, the good approach is to declare the generated type of
exception.
• The catch block must be used after the try block only. You can use
multiple catch block with a single try block.
Internal working of java try-catch block
Java try-catch block example
Output:
java.lang.ArithmeticException: / by zero
rest of the code
Java try-catch block example
import java.io.FileNotFoundException;
import java.io.PrintWriter;
Output:
File saved successfully
Java Multi-catch block
A try block can be followed by one or more catch blocks. Each catch block
must contain a different exception handler. So, if you have to perform
different tasks at the occurrence of different exceptions, use java multi-
catch block.
Points to remember
• At a time only one exception occurs and at a time only one catch block
is executed.
• All catch blocks must be ordered from most specific to most general,
i.e. catch for ArithmeticException must come before catch for
Exception.
Java Multi-catch block
class Excep6{
public static void main(String args[]){
try{
try{
System.out.println("going to divide");
int b =39/0;
}catch(ArithmeticException e){System.out.println(e);}
try{
int a[]=new int[5];
a[5]=4;
}catch(ArrayIndexOutOfBoundsException e){System.out.println(e);}
System.out.println("other statement);
}catch(Exception e){System.out.println("handeled");}
System.out.println("normal flow..");
}
}
finally block in exception handling
• Java finally block is a block that is used to execute important code such
as closing connection, stream etc.
• Java finally block is always executed whether exception is handled or
not.
• Java finally block follows try or catch block.
• If you don't handle exception, before terminating the program, JVM
executes finally block(if any).
• For each try block there can be zero or more catch blocks, but only one
finally block.
Java throw exception
4) Throw is used within the method. Throws is used with the method
signature.
5) You cannot throw multiple You can declare multiple exceptions
exceptions. e.g.
public void method()throws
IOException,SQLException.