Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Exception handling in java

216 views

Published on

Exception Handling in java.Try catch finally block,User Defined Exceptions ,Exception handling hirearachy

Published in: Education
  • Be the first to comment

  • Be the first to like this

Exception handling in java

  1. 1. Exception Handling Syntax for exception class handling Try catch finally block Throw Throws User Defined Exceptions Exception class hirearachy
  2. 2. 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 exceptions Runtime 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
  3. 3. Syntax for Exception Handling Try { //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.
  4. 4. 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
  5. 5. 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 input switch(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"); }//try catch(NumberFormatException e) { System.out.println("Numberformat exception"); } }/*case*/break; }//switch }//public
  6. 6. Try catch finally block• public static void main(String[] args) { System.out.println("enter you choice in 1:multiple catch blocksn 2:Example finally blockn3:try block with finally block"); int n; Scanner sc=new Scanner(System.in); n=sc.nextInt(); //taking input at runtime switch(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
  7. 7. 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.
  8. 8. 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
  9. 9. Using a parameter in catch block public 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); } } else System.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
  10. 10. 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
  11. 11. 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

×