1 / 10

Exception Objects

Exception Objects. An exception is an abnormal condition that arises in a code sequence at rum time. Exception is a way of signaling serious problem. An Exception in Java is an Object that’s created when an abnormal situation arises in your program.

howarda
Télécharger la présentation

Exception Objects

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Exception Objects An exception is an abnormal condition that arises in a code sequence at rum time. Exception is a way of signaling serious problem. An Exception in Java is an Object that’s created when an abnormal situation arises in your program. The Exception Object has data members that store information about the nature of the problem.

  2. Exception Handling class Test { public static void main (String[] args) { int d=0; int a=10/d; } } Uncaught Exception. java.lang.ArithmeticException: / by zero. This Exception is caught by java default handler.

  3. Catching Exception It allow you to fix the errors. It prevents the program from automatically terminating. try and catch Blocks Use try block to identify the code that can throw exceptions. Use catch blocks to catch the exceptions.

  4. try and catch Block • Code generates ArithmeticException. • class Test { • public static void main (String[] args) { • int b=0; • try { • int a=10/b;} scope of a is limited in try block. • catch (ArithmeticException e) { • System.out.println(“ Divide by zero Exception”);} • } • }

  5. try and catch Blocks • Code generates ArithmeticException and ArrayIndexOutOfBoundsException. • class Test { • public static void main (String[] args) { • int a=10; • int b[] = new int [5]; • try { • System.out.println(a/0); • System.out.println(b[5]);} • catch (ArithmeticException e) { • System.out.println(“ Divide by zero Exception”);} • catch (ArrayIndexOutOfBoundsException e){ • System.out.println(“ Array Index Exception”);} • }}

  6. Nested try Statements class Test { public static void main (String[] args) { int a=10; int b[] = new int [5]; try{ try { System.out.println(a/0); System.out.println(b[5]);} catch (ArithmeticException e) { System.out.println(“Divide by zero Exception”);} } catch (ArrayIndexOutOfBoundsException e){ System.out.println(“ Array Index Exception”);} }}

  7. Propagation of Exception class Test { public static void main (String[] args) { Test1 obj1 = new Test1(); try{ obj1.abc();} catch (ArithmeticException e) { System.out.println(“Divide by zero Exception”);} } } class Test1 { abc() { System.out.println(10/0); } }

  8. try, catch and finally class Test { public static void main (String[] args) { Test1 obj = new Test1(); System.out.println(obj.add()); }} class Test1{ int add(){ try { System.out.println(10/10); return 1;} catch (ArithmeticException e) { System.out.println("Divide by zero Exception"); return 2;} finally { System.out.println("finally block"); return 3;} }}

  9. try and finally class Test { public static void main (String[] args) { Test1 obj = new Test1(); System.out.println(obj.add()); }} class Test1{ int add(){ try { System.out.println(10/10); return 1;} finally { System.out.println("finally block"); return 3;} }}

  10. Sequence of Catch Blocks • Illegal Sequence • class Test { • public static void main (String[] args) { • int a[] = new int [5]; • try { • System.out.println("abc".substring(3,2)); • System.out.println(a[5]); • } • catch (IndexOutOfBoundsException e) { • System.out.println(“ Index Exception");} • catch (ArrayIndexOutOfBoundsException e) { • System.out.println(“ Array Index Exception");} • catch (StringIndexOutOfBoundsException e) { • System.out.println(“ String Index Exception");} • finally { • System.out.println(“ finally block ");} • }} General Class cannot comes before Special classes.

More Related