Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— 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.


Download ppt "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."

Similar presentations


Ads by Google