Exception Objects An exception is an abnormal condition

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.
![Exception Handling • • class Test { public static void main (String[] args) { Exception Handling • • class Test { public static void main (String[] args) {](http://slidetodoc.com/presentation_image_h2/9dedd35cf2a8bba1a18c13f7ea86d1e2/image-2.jpg)
Exception Handling • • class Test { public static void main (String[] args) { int d=0; int a=10/d; } } Uncaught Exception. java. lang. Arithmetic. Exception: / by zero. • This Exception is caught by java default handler.

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.

try and catch Block • Code generates Arithmetic. Exception. 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 (Arithmetic. Exception e){ System. out. println(“ Divide by zero Exception”); } }

try and catch Blocks • Code generates Arithmetic. Exception and Array. Index. Out. Of. Bounds. Exception. 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 (Arithmetic. Exception e){ System. out. println(“ Divide by zero Exception”); } catch (Array. Index. Out. Of. Bounds. Exception e){ System. out. println(“ Array Index Exception”); } }}
![Nested try Statements class Test { public static void main (String[] args) { int Nested try Statements class Test { public static void main (String[] args) { int](http://slidetodoc.com/presentation_image_h2/9dedd35cf2a8bba1a18c13f7ea86d1e2/image-6.jpg)
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 (Arithmetic. Exception e) { System. out. println(“Divide by zero Exception”); } } catch (Array. Index. Out. Of. Bounds. Exception e){ System. out. println(“ Array Index Exception”); } }}
![Propagation of Exception class Test { public static void main (String[] args) { Test Propagation of Exception class Test { public static void main (String[] args) { Test](http://slidetodoc.com/presentation_image_h2/9dedd35cf2a8bba1a18c13f7ea86d1e2/image-7.jpg)
Propagation of Exception class Test { public static void main (String[] args) { Test 1 obj 1 = new Test 1(); try{ obj 1. abc(); } catch (Arithmetic. Exception e){ System. out. println(“Divide by zero Exception”); } }} class Test 1 { abc() { System. out. println(10/0); } }
![try, catch and finally class Test { public static void main (String[] args) { try, catch and finally class Test { public static void main (String[] args) {](http://slidetodoc.com/presentation_image_h2/9dedd35cf2a8bba1a18c13f7ea86d1e2/image-8.jpg)
try, catch and finally class Test { public static void main (String[] args) { Test 1 obj = new Test 1(); System. out. println(obj. add()); }} class Test 1{ int add(){ try { System. out. println(10/10); return 1; } catch (Arithmetic. Exception e) { System. out. println("Divide by zero Exception"); return 2; } finally { System. out. println("finally block"); return 3; } }}
![try and finally class Test { public static void main (String[] args) { Test try and finally class Test { public static void main (String[] args) { Test](http://slidetodoc.com/presentation_image_h2/9dedd35cf2a8bba1a18c13f7ea86d1e2/image-9.jpg)
try and finally class Test { public static void main (String[] args) { Test 1 obj = new Test 1(); System. out. println(obj. add()); }} class Test 1{ int add(){ try { System. out. println(10/10); return 1; } finally { System. out. println("finally block"); return 3; } }}

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]); General Class cannot } comes before Special classes. catch (Index. Out. Of. Bounds. Exception e) { System. out. println(“ Index Exception"); } catch (Array. Index. Out. Of. Bounds. Exception e) { System. out. println(“ Array Index Exception"); } catch (String. Index. Out. Of. Bounds. Exception e) { System. out. println(“ String Index Exception"); } finally { System. out. println(“ finally block "); } }}
- Slides: 10