Exception handling in java

12
Exception Handling Syntax for exception class handling Try catch finally block Throw Throws User Defined Exceptions Exception class hirearachy

Transcript of Exception handling in java

Page 1: Exception handling in java

Exception HandlingSyntax for exception class handling

Try catch finally blockThrow

Throws User Defined Exceptions

Exception class hirearachy

Page 2: Exception handling in java

What is Exception• An exception is an event, which occurs during the execution of a program

that disrupts the normal flow of the program's instructions

Types of exceptions• Checked exceptions• Unchecked exceptions

Checked exceptions compiler checks them during compilation to see whether the

programmer has handled them or not. If these exceptions are not handled, it will give compilation error Eg:ClassNotFoundException

Unchecked exceptionsRuntime Exceptions are also known as Unchecked Exceptions as the compiler

do not check whether the programmer has handled them or not but it’s the duty of the programmer to handle these exceptions and provide a safe exit. Eg:ArithmeticException

Page 3: Exception handling in java
Page 4: Exception handling in java

Syntax for Exception HandlingTry{ //code that may throw exception }catch(Exception_class_Name ref){} Try Block• The try block contains a block of program statements within which an

exception might occur• A try block is always followed by a catch block, • A try block must followed by a Catch block or Finally block or both

Catch Block• A catch block must be associated with a try block• The corresponding catch block executes if an exception of a particular type

occurs within the try block.

Page 5: Exception handling in java

Flow of try catch block

• If an exception occurs in try block then the control of execution is passed to the catch block from try block.

• The exception is caught up by the corresponding catch block.• A single try block can have multiple catch statements associated with it,

but each catch block can be defined for only one exception class. • After the execution of all the try blocks, the code inside the finally block

executes. It is not mandatory to include a finally block at all

Page 6: Exception handling in java

public static void main(String[] args) { System.out.println("enter you choice in 1 or 2");int n; Scanner sc=new Scanner(System.in); n=sc.nextInt(); //taking inputswitch(n){ /* * Arithmetic exception */case 1:{ try { int numarator=26,denominator=0;int c=numarator/denominator; System.out.println("eception raised"); }catch(ArithmeticException e){ System.out.println("arithmetic exception");} }/*case*/break; /** number format exception*/case 2:{try{ int a=Integer.parseInt("X");}//trycatch(NumberFormatException e){ System.out.println("Numberformat exception"); }}/*case*/break;}//switch}//public

Page 7: Exception handling in java

Try catch finally block• public static void main(String[] args)

{ System.out.println("enter you choice in 1:multiple catch blocks\n 2:Example finally block\n3:try block with finally block");int n; Scanner sc=new Scanner(System.in); n=sc.nextInt(); //taking input at runtimeswitch(n){ /* * Arithmetic exception */case 1:{case 2:{ try{ int numarator=10,denominator=0;int c=numarator/denominator;System.out.println("the value of c is"+c);}catch(NumberFormatException e2){ System.out.println("arithmetic exception cought"); }finally{System.out.println("finally block excute every time"); }}/*case*/break;case 3:{ try{ int numarator=10,denominator=0;int c=numarator/denominator;System.out.println("the value of c is"+c);} finally {System.out.println("finally block excute every time"); }}/*case*/break;}//switch}//public

Page 8: Exception handling in java

Throw• The Java throw keyword is used to explicitly throw an exception• We can throw either checked or unchecked exception in java by throw

keyword.• Program execution stops on encountering throw statement, and the

closest catch statement is checked for matching type of exception.

Syntax• throw ThrowableInstance

Creating Instance of Throwable Class• Using a parameter in catch block.• Creating instance with new operator.

Page 9: Exception handling in java

public class Throw_Statement{ static void age(int a)

{/** throwing an exception creating with new operator */if(a<26){ throw new ArithmeticException("less than age");}else

System.out.println("Correct age");}//age()public static void main(String[] args) { age(10); }//main

}//class

####output####Exception in thread "main" java.lang.ArithmeticException: less than age

Page 10: Exception handling in java

Using a parameter in catch blockpublic class Throw_Statement{ static void age(int a)

{ /* * throwing an exception with catch */if(a<26){ try{throw new ArithmeticException("less than age");}catch(ArithmeticException e){System.out.println("number is less than specifyed ::::"+e);}}elseSystem.out.println("Correct age");}//age()public static void main(String[] args) { age(10); }//main

}//class

####output####number is less than specifyed ::::java.lang.ArithmeticException: less than age

Page 11: Exception handling in java

Throws• The Java throws keyword is used to declare an exception• It gives an information to the programmer that there may occur an

exception so it is better for the programmer to provide the exception handling code

Syntax• Return_type Method_name() throws Exception_Class_Name

• Throws key word example

Page 12: Exception handling in java

User Defined Exceptions • User defined exceptions in java are also known as Custom exceptions.import java.io.IOException;class My_Exception_Class extends Exception{ String output;

public My_Exception_Class(String s) { output="the result is ="+s; }/*public String toString(){ return(output); }*/

}public class Throw_Statement{ public static void main(String[] args)

{ try{throw new My_Exception_Class("demo User Defined Exception");}catch(My_Exception_Class e){ System.out.println(e.output); }

}//main}//class####output###the result is =demo User Defined Exception