Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 17

Exception Handling in Java

Exception

 What is the dictionary meaning?

Abnormal condition

 What does it mean in programming language?


• An action that disrupts the normal flow of execution of the program.
 Examples:
• Out of memory
• Unable to open the file which exists
• Opening a file that does not exist
• Dividing a number by zero
• Invalid format of data
• Network connection problem
• etc.
Run-time: Built-in System message

Abrupt termination
Is the message user friendly?
Definition of method 1

No exception handling

r
ro
mechanism

er
main method

e
m
n-ti
ru
If
Abrupt termination
Call to method1
Rest of the code will Definition of method 1
not execute

Exception handling
Call to method2 mechanism present
Why is it required?

 Normal scenario – any run-time error occurs, it


terminates the program with system generated message.
 Why handle them?
• To maintain normal flow of execution of program /
application.
• Meaningful and user understandable message can be
displayed
Difference between Error and Exception

 Error is irrecoverable
• OutOfMemoryError
• VirtualMachineError
• AssertionError etc.
 Exception - can handle such conditions and take
necessary corrective actions. 
• NullPointerException
• ArithmeticException
• ArrayIndexOutOfBoundsException etc.
Throwable: Base class of Exceptions
• Exception (sub-class) • Error (sub-class)
– IOException – StackOverflowError
– SQLException – VirtualMachineError
– ClassNotFoundException – OutOfMemoryError
– RunTimeException
• ArithmeticException
• NullPointerException
• NumberFormatException
• IndexOutOfBoundsException
Types of Exceptions in Java

1. Checked Exceptions
a. Which directly inherit Throwable class
b. Checked at compile-time
c. Example: FileReader class – file specified in constructor does
not exist, FileNotFoundException occurs. Compiler prompts
the programmer to handle it.
2. Unchecked Exceptions
a. Which inherit RunTimeException class
b. Checked at run-time
c. Example: Declaring an array of 5 elements and trying to
access 6th element, ArrayIndexOutOfBoundsException
exception occurs. With method documentation, programmer
can take preventive action in code.
Exception Handling in Java

 An object that is “thrown” when some abnormal


condition occurs in the program
• ClassNotFoundException, IOException, SQLException, etc.
• These are built-in classes.
How can it be handled?

int array[] = {10, 20, 30, 40, 50};


try
{
array[6] = 70;
for(int i = 0; i < array.length; i++)
System.out.println(array[i]);

System.out.println(" code in main continues to execute");

}catch(ArrayIndexOutOfBoundsException b)
{
System.out.println("Array index accessed outside the fixed size");
}
int num = 0, deno = 0;
System.out.println("enter numerator and denominator");
try
{
Scanner s1 = new Scanner (System.in);
Scanner s2 = new Scanner (System.in);
num = s1.nextInt();
deno = s2.nextInt();

System.out.println("rest of the code in main will execute");


}catch(InputMismatchException e)
{
System.out.println("You have not entered number");
} catch(ArithmeticException e)
{
System.out.println(“Denominator cannot be zero");
}
try
{ num = s1.nextInt();
deno = s2.nextInt();
// ………. Other code here
}catch(InputMismatchException e)
{
System.out.println("You have not entered number");
} catch(ArithmeticException e)
{
System.out.println(“Denominator cannot be zero");
} catch(Exception e)
{
System.out.println(“Default: Any other exception caught");
}finally
{
System.out.println(“Will always execute . . . .”);
}
What are the keywords?

 try
 catch
 throw
 throws
 finally
throw keyword: Throwing exceptions
public class ExceptionClass
{  
   static void Check(int num)
{  
     if(num == zero)  
      throw new ArithmeticException("not valid");  
     else  
       //code here
   }  
  }  
 public static void main(String args[]){  
      validate(13);  
      System.out.println("rest of the code...");  
  }  
Exception propagation Definition of method 1

method 2 called

main method Exception Handling


mechanism PRESENT here

Call to method 1
Definition of method 2
//normal execution
continues Exception occurs

Exception handling
Call to method 3 mechanism ABSENT here
throws keyword

 To declare an exception
• For programmer to know about the possible exception
thrown
 Checked exceptions can be forwarded in call stack

void CopyFile() throws IOException


{
// ….. Code here
}
Home work

 What is the difference between error and exception?


 What is an exception?
 What is the difference between throw and throws
keyword?
 What is the use of finally keyword?
 Which is the base class of exceptions?
 What is the difference between checked and unchecked
exception?
 What is the difference final, finally and finalize keyword?
 What is a runtime error, logical error and syntax error?
Explain with an example.

You might also like