EXCEPTIONS IN JAVA Whats Exception An exception is

  • Slides: 57
Download presentation
EXCEPTIONS IN JAVA

EXCEPTIONS IN JAVA

What’s Exception • An exception is an abnormal condition that occurs at run time.

What’s Exception • An exception is an abnormal condition that occurs at run time. For example divide by 0. • During execution of a statement within any method if any exceptional condition occurs the Java Runtime Environment (JRE) i. e. java interpreter creates a suitable Exception object and throws it. • Every Exception is basically an object belonging to Java’s Exception class Hierarchy. • Exceptions needs to be handled so that appropriate actions can be taken. • Programmer can also provide exception handling code. However if there is no exception handling code present during runtime and exception occurs, then java interpreter provides default exception handler. • Default Exception Handler displays the name of the exception object in string form and stops the execution of the program. • However , programmer can provide exception handling code and program’s execution can continue even after the occurrence of exception.

Exception class Hierarchy • Every Exception type is basically an object belonging to class

Exception class Hierarchy • Every Exception type is basically an object belonging to class Exception • Throwable class is the root class of Exceptions. • Throwable class has two direct subclasses named Exception, Error

Partial Exceptions Hierarchy Throwable Error Exception IOException EOFException And many, many more… Runtime. Exception

Partial Exceptions Hierarchy Throwable Error Exception IOException EOFException And many, many more… Runtime. Exception File. Not. Found Exception Arithmetic Exception Null. Pointer Exception Index. Out of. Bounds Exception Illegal Argument Exception

Types of Exceptions A. Unchecked Exceptions All Exceptions that extend the Runtime. Exception or

Types of Exceptions A. Unchecked Exceptions All Exceptions that extend the Runtime. Exception or any one of its subclass are unchecked exceptions • Unchecked Exceptions are unchecked by compiler. • Whether you catch the exception or not compiler will pass the compilation process. • If Unchecked exception is caught then exception handling code will be executed and program’s execution continues. • If Unchecked exception is not caught then java interpreter will provide the default handler. But in this case execution of the program will be stopped by displaying the name of the exceptions object.

Unchecked Exceptions Some Common Unchecked Exceptions 1. Arithmatic. Exception (Divide By 0) 2. Array.

Unchecked Exceptions Some Common Unchecked Exceptions 1. Arithmatic. Exception (Divide By 0) 2. Array. Index. Out. Of. Bounds. Exception 3. Array. Store. Exception 4. File. Not. Found. Exception 5. Null. Pointer. Exception 6. Number. Format. Exception 7. Illegal. Aruments. Exception Throwable Exception Error Run. Time. Exception All Unchecked Exceptions directly or indirectly are sub classes of Run. Time. Exception Any Class belonging to Run. Time. Exception

Internal working of java try-catch block

Internal working of java try-catch block

Unchecked. Exceptions Example class Exceptiondemo 1 { public static void main(String arhs[]) { int

Unchecked. Exceptions Example class Exceptiondemo 1 { public static void main(String arhs[]) { int a=10; int b= 5; int c =5; int x = a/(b-c); System. out. println("c="+c); int y = a/(b+c); System. out. println("y="+y); Can Throw an Exception } } D: javabin>javac Exceptiondemo 1. java << Compilation Step Pass>> D: javabin>java Exceptiondemo 1 Exception in thread "main" java. lang. Arithmetic. Exception: / by zero at Exceptiondemo 1. main(Exceptiondemo 1. java: 8)

Example 2 (Unchecked Exceptions) class Exceptiondemo 2 Can throw either { Array. Index. Out.

Example 2 (Unchecked Exceptions) class Exceptiondemo 2 Can throw either { Array. Index. Out. Of. Bounds. Exception OR public static void main(String args[]) Number. Format. Exception { double a= Double. parse. Double(args[0]); } } D: javabin>javac Exceptiondemo 2. java D: javabin>java Exceptiondemo 2 Exception in thread "main" java. lang. Array. Index. Out. Of. Bounds. Exception: 0 at Exceptiondemo 2. main(Exceptiondemo 2. java: 5) D: javabin>java Exceptiondemo 2 pankaj Exception in thread "main" java. lang. Number. Format. Exception: For input string: "pankaj“ at sun. misc. Floating. Decimal. read. Java. Format. String(Floating. Decimal. java: 1 2 24) at java. lang. Double. parse. Double(Double. java: 482) at Exceptiondemo 2. main(Exceptiondemo 2. java: 5)

Put the Related/Dependent Statements in try block class extest { public static void main(String

Put the Related/Dependent Statements in try block class extest { public static void main(String args[]) { try { E: oop>javac extest. java int a = Integer. parse. Int(args[0]); extest. java: 10: cannot find } symbol catch(Exception e) {} symbol : variable a int b = a+10; location: class extest System. out. println("b="+b); int b = a+10; } ^ extest. java: 10: incompatible types } found : <nulltype> required: int b = a+10; ^ 2 errors

Cont… class extest { public static void main(String args[]) { try { int a

Cont… class extest { public static void main(String args[]) { try { int a = Integer. parse. Int(args[0]); int b = a+10; System. out. println("b="+b); } catch(Exception e) {} } }

Types of Exceptions • • B Checked Exceptions All Exceptions that extends the Exception

Types of Exceptions • • B Checked Exceptions All Exceptions that extends the Exception or any one its subclass except Run. Time. Exception class are checked exceptions Checked Exceptions are checked by the Java compiler. There are two approaches that a user can follow to deal with checked exceptions

Handling Checked Exceptions • Inform the compiler that a method can throw an Exception.

Handling Checked Exceptions • Inform the compiler that a method can throw an Exception. • Catch the checked exception in try catch block • If Checked exception is caught then exception handling code will be executed and program’s execution continues. • If Checked exception is not caught then the program will not successfully compile

Checked Exceptions Examples Some Common Checked Exceptions 1. IOException 2. Class. Not. Found. Exceptions

Checked Exceptions Examples Some Common Checked Exceptions 1. IOException 2. Class. Not. Found. Exceptions 3. Interrupted. Exception 4. No. Such. Method. Exception Throwable Exception Error Any Sub Class belonging to Exception EXCEPT Runtime. Exception

Checked Exceptions /* Program to read two integers Display their sum */ import java.

Checked Exceptions /* Program to read two integers Display their sum */ import java. io. *; WILL THIS CODE class Exceptiondemo 3 COMPILE { public static void main(String args[]) SUCCESSFULLY? ? { Buffered. Reader br = new Buffered. Reader(new Input. Stream. Reader(System. in)); int a = Integer. parse. Int(br. read. Line()); int b = Integer. parse. Int(br. read. Line()); System. out. println("Sum is : "+(a+b)); } } Exceptiondemo 3. java: 9: unreported exception java. io. IOException; must be caught or declared to be thrown int a = Integer. parse. Int(br. read. Line()); ^ Exceptiondemo 3. java: 10: unreported exception java. io. IOException; must be caugh or declared to be thrown int b = Integer. parse. Int(br. read. Line()); ^

Ways To Handle Checked Exceptions Method 1: << Mention thru throws clause>> import java.

Ways To Handle Checked Exceptions Method 1: << Mention thru throws clause>> import java. io. *; class Exceptiondemo 3 { public static void main(String args[]) throws IOException { Buffered. Reader br = new Buffered. Reader(new input. Stream. Reader(System. in)); int a = Integer. parse. Int(br. read. Line()); int b = Integer. parse. Int(br. read. Line()); System. out. println("Sum is : "+(a+b)); } } 1. throws clause is used with methods to indicate type of Exception a method can throw 2. Specifically required for Checked Exceptions [ To Pass Compilation process]. It can/may be used for unchecked exceptions also. 3. A method can throw as many exceptions.

Ways To Handle Checked Exceptions cont…. Method 2 << Put the statements in try

Ways To Handle Checked Exceptions cont…. Method 2 << Put the statements in try catch block and catch >> import java. io. *; class Exceptiondemo 3 { public static void main(String args[]) { Buffered. Reader br = new Buffered. Reader(new input. Stream. Reader(System. in)); try { int a = Integer. parse. Int(br. read. Line()); int b = Integer. parse. Int(br. read. Line()); System. out. println("Sum is : "+(a+b)); } catch(IOException e) { } } }

Exception Handling Requires the Following four steps 1. Finding the problem (Identify the statements

Exception Handling Requires the Following four steps 1. Finding the problem (Identify the statements whose execution may result in Exception. Put all those statements in a try{. . } block) 2. Inform that an exception is thrown (Throw the Exception) << Note Down throw vs throws>> 3. Receive the exception ( Catch the exception using catch{. . } block) 4. Provide exception handling code in catch block.

Exception Hadling Syntax try Important Points : { 1. try {} block may have

Exception Hadling Syntax try Important Points : { 1. try {} block may have one or multiple statements. <statements that can throw exceptions> } catch(Exception. Type<1> e 1) {…. } catch(Exception. Type<2> e 2) {…. } catch(Exception. Type<3> e 3) {…. } ………………. . catch(Exception. Type<N> e 4) {…. } 2. try{} block may throw a single type of Exception or multiple exceptions. But at a time it can throw only single type of exception. 3. There can be multiple catch() {. . } blocks associated with single try{} block. 4. If try{} block can throw multiple exceptions then user should catch all exceptions. (one catch block for each type of exception)

Catching an Exception class Exceptiondemo 1 { public static void main(String arhs[]) { int

Catching an Exception class Exceptiondemo 1 { public static void main(String arhs[]) { int a=10; int b= 5; int c =5; try { int x = a/(b-c); System. out. println("c="+c); D: javabin>java Exceptiondemo 1 } java. lang. Arithmetic. Exception: / by catch(Arithmetic. Exception e) y=1 { System. out. println(e. to. String()); } int y = a/(b+c); System. out. println("y="+y); } } zero

Catching Multiple Exceptions class Exceptiondemo 4 { public static void main(String args[]) { int

Catching Multiple Exceptions class Exceptiondemo 4 { public static void main(String args[]) { int a[]= {5, 10}; try { int b= Integer. parse. Int(args[0]); int x = 25/(b-a[0]); catch(Arithmetic. Exception e) System. out. println("x="+x); { System. out. println(e. to. String()); } This Statement is outside catch block and will be executed in any case } catch(Number. Format. Exception e) { System. out. println(e. to. String()); } catch(Array. Index. Out. Of. Bounds. Exception e) { System. out. println(e. to. String()); } System. out. println("Hello This is Exception Test"); } // End of main() method }// End of class Exceptiondemo 4

OUTPUT What will be o/p if you execute it like 1. java Exceptiondemo 4

OUTPUT What will be o/p if you execute it like 1. java Exceptiondemo 4 2. java Exceptiondemo 4 1 3. java exceptiondemo 4 oop NO COMMAND LINE ARGUMENTS PASSED AS 1 COMMAND LINE ARGUMENT PASSED oop

Nested Try Statements • Try{ } statements can be nested. One try block may

Nested Try Statements • Try{ } statements can be nested. One try block may contain another try block • In case of nested try blocks, context of that exception is pushed onto stack. • Inner try block may/or may not have catch statements associated with it. • If an exception is thrown from inner try block then first inner catch statements are matched (if present). If no match is found then outer try block are matched. If there also no match found then default handler will be invoked. • However, if outer try block throws the exception then only outer try blocks are matched.

Nested try blocks A typical Syntax try { Statement A; Statement B; try {

Nested try blocks A typical Syntax try { Statement A; Statement B; try { Statement C; Statement D; } catch(CException e) { catch(DException e) { } catch(AException e) { …. catch(BException e) { …. } } } try { Statement A; Statement B; try { Statement C; Statement D; } } catch(AException e) { catch(BException e) { catch(CException e) { catch(DException e) { …. …. } }

Nested try statements Example class nestedtry { public static void main(String args[]) { int

Nested try statements Example class nestedtry { public static void main(String args[]) { int a[] = { 2, 5, 6}; // { a[0] = 2, a[1] = 5, a[2] = 6} try // outer try { int b = Integer. parse. Int(args[0]); try // inner try { int c[] = { 4, 5, 6}; // { c[0] = 4, c[1] = 5, c[2] = 6} int d = c[b]/(c[b]-4); } // End of inner try catch(Array. Index. Out. Of. Bounds. Exception e) { System. out. println("Exception : "+ e. to. String()); System. out. println("By Inner try"); } catch(Arithmetic. Exception e) { System. out. println("Exception : "+ e. to. String()); System. out. println("By Inner try"); } } // End of outer try

// Catch Blocks for outer try catch(Array. Index. Out. Of. Bounds. Exception e) {

// Catch Blocks for outer try catch(Array. Index. Out. Of. Bounds. Exception e) { System. out. println("Exception : "+ e. to. String()); System. out. println("By Outr try"); } catch(Number. Format. Exception e) { System. out. println("Exception : "+ e. to. String()); System. out. println("By Outer try"); } } // End of main } // End of class D: javabin>java nestedtry Exception : java. lang. Array. Index. Out. Of. Bounds. Exception: 0 By Outer try D: javabin>java nestedtry 4 Exception : java. lang. Array. Index. Out. Of. Bounds. Exception: 4 By Inner try D: javabin>java nestedtry 0 Exception : java. lang. Arithmetic. Exception: / by zero By Inner try

Writing Your Own Exceptions • Programmers Can write their own Exception classes apart from

Writing Your Own Exceptions • Programmers Can write their own Exception classes apart from java’s library Exceptions. • Programmer can write either checked Exception OR Unchecked Exception. • To make a checked exception , make your exception class a subclass of Exception OR any one of its subclass EXCEPT Run. Time. Exception. class AException extends Exception { …} Checked Exception class BException extends IOException {. . } Checked Exception • To make a Unchecked exception , make your exception class a subclass of Run. Time. Exception OR any one of its subclass XException YException ZException extends Run. Time. Exception { … } Aritmetic. Exception { … } Array. Index. Out. Of. Exception { … } Index. Out. Of. Bounds. Exception { … }

Throwing Unchecked Exception 1. Create an Invalid. BOXException which will be thrown by the

Throwing Unchecked Exception 1. Create an Invalid. BOXException which will be thrown by the constructor of the BOX class whenever an attempt will be made to create an invalid BOX object. (Any Dimension = 0 or < 0). 2. Create an Invalid. Triangle. Exception which will be thrown whenever an attempt will be made to create an invalid Triangle object. (In Triangle sum of two sides must be > third side).

EXAMPLE 1: class Invalid. BOXException extends Runtime. Exception { Invalid. BOXException(String msg) { super(msg);

EXAMPLE 1: class Invalid. BOXException extends Runtime. Exception { Invalid. BOXException(String msg) { super(msg); System. out. println("An attempt is made to create an Invalid BOx object "); } } class BOX Optional as Invalid. BOXException { is Unchecked private double length; private double width; private double height; BOX(double l, double w, double h) throws Invalid. BOXException { if( l <=0 || w <= 0 || h <= 0) throw new Invalid. BOXException("Invalid BOX Object creation"); length = l; width = w; height = h; }

double get. Length() { return length; } double get. Width() { return width; }

double get. Length() { return length; } double get. Width() { return width; } double get. Height() { return height; } double Area() { return 2*(length*width + width*height + height*length); } double Volume() { return length*width*height ; } } class exceptiontest 1 { public static void main(String args[]) { BOX b 1 = new BOX(0, 0, 0); BOX b 2 = new BOX(10, 4, 5); System. out. println(“Area of b 2: ”+b 2. Area()); } } D: javabin>java exceptiontest 1 An attempt is made to create an Invalid BOx object Exception in thread "main" Invalid. BOXException: Inavlid BOX Object creation at BOX. <init>(exceptiontest 1. java: 18) at exceptiontest 1. main(exceptiontest 1. java: 35)

Checked Exceptions • Make your exception class extends Exception class or any one of

Checked Exceptions • Make your exception class extends Exception class or any one of its subclass except Rumtime. Exception. • Checked Exceptions needs to either caught or informed by use of throws clause • Note down that throw clause is used to throw the exception where as throws clause is used to inform that an exception is thrown by the method. • Throw clause is used inside method body where as throws clause is used with first line of the method. • Throws clause can be used to inform both type of exceptions. But in case a method is throwing a unchecked exception then it is not compulsory to inform. • In case a method is throwing a checked Exception, then it has either to caught the exception or informs by using throws clause or it can do both.

EXAMPLE 1: class Invalid. BOXException extends Exception Checked { Exception Invalid. BOXException(String msg) {

EXAMPLE 1: class Invalid. BOXException extends Exception Checked { Exception Invalid. BOXException(String msg) { super(msg); System. out. println("An attempt is made to create an Invalid BOx object "); } } class BOX { Any Method or constructor which throws an private double length; checked Type Exception must inform it thru private double width; throws clause private double height; BOX(double l, double w, double h) { if( l <=0 || w <= 0 || h <= 0) throw new Invalid. BOXException("Inavlid BOX Object creation"); length = l; width = w; height = h; }

double get. Length() { return length; } double get. Width() { return width; }

double get. Length() { return length; } double get. Width() { return width; } double get. Height() { return height; } double Area() { return 2*(length*width + width*height + height*length); } double Volume() { return length*width*height ; } } class exceptiontest 1 { public static void main(String args[]) { BOX b 1 = new BOX(0, 0, 0); BOX b 2 = new BOX(10, 4, 5); System. out. println(“Area of b 2: ”+b 2. Area()); } } D: javabin>javac exceptiontest 1. java < Compile Time Error> exceptiontest 1. java: 18: unreported exception Invalid. BOXException; must be caught or declared to be thrown throw new Invalid. BOXException("Inavlid BOX Object creation"); ^ 1 error

EXAMPLE 1: class Invalid. BOXException extends Exception { Invalid. BOXException(String msg) { super(msg); System.

EXAMPLE 1: class Invalid. BOXException extends Exception { Invalid. BOXException(String msg) { super(msg); System. out. println("An attempt is made to create an Invalid BOx object "); } } class BOX { private double length; private double width; private double height; BOX(double l, double w, double h) throws Invalid. BOXException { if( l <=0 || w <= 0 || h <= 0) throw new Invalid. BOXException("Inavlid BOX Object creation"); length = l; width = w; height = h; }

double get. Length() { return length; } double get. Width() { return width; }

double get. Length() { return length; } double get. Width() { return width; } double get. Height() { return height; } double Area() { return 2*(length*width + width*height + height*length); } double Volume() { return length*width*height ; } } class exceptiontest 1 { public static void main(String args[]) throws Invalid. BOXException { BOX b 1 = new BOX(0, 0, 0); BOX b 2 = new BOX(10, 4, 5); System. out. println(“Area of b 2: ”+b 2. Area()); } } D: javabin>java exceptiontest 1 An attempt is made to create an Invalid BOx object Exception in thread "main" Invalid. BOXException: Inavlid BOX Object creation at BOX. <init>(exceptiontest 1. java: 18) at exceptiontest 1. main(exceptiontest 1. java: 36)

Use of finally Clause • finally block may be added immediately after try block

Use of finally Clause • finally block may be added immediately after try block or after the last catch block. • finally block in general used to perform house keeping operations such as closing files or releasing system resources. • Finally block when present is guaranteed to execute regardless of whether an exception is thrown or not.

Example(finally clause) class ex 10 { public static void main(String args[]) { int a=10;

Example(finally clause) class ex 10 { public static void main(String args[]) { int a=10; int b = 20; try { int b 1=Integer. parse. Int(args[0]); int x = a/(a-b 1); try { int y = b/(b-b 1); } finally { System. out. println("Inner Block executed"); finally } { } System. out. println("Outer Block executed"); } } }

Output D: javabin>java ex 10 Outer Block executed Exception in thread "main" java. lang.

Output D: javabin>java ex 10 Outer Block executed Exception in thread "main" java. lang. Array. Index. Out. Of. Bounds. Exception: 0 at ex 10. main(ex 10. java: 9) D: javabin>java ex 10 45 Inner Block executed Outer Block executed D: javabin>java ex 10 10 Outer Block executed Exception in thread "main" java. lang. Arithmetic. Exception: / by zero at ex 10. main(ex 10. java: 10) D: javabin>java ex 10 20 Inner Block executed Outer Block executed Exception in thread "main" java. lang. Arithmetic. Exception: / by zero at ex 10. main(ex 10. java: 13)

Creating Hierarchy of Exceptions 1. We can create our own tree of exception classes.

Creating Hierarchy of Exceptions 1. We can create our own tree of exception classes. 2. All Exceptions classes in tree are either checked or unchecked depending upon whether the super class is checked or unchecked. Invalid. Student. Exception Invalid. Id. NOException Invalid. Id. No. Year. Exception Invalid. Name. Exception Invalid. Id. No. Descipline. Exception

Example class AException extends Runtime. Exception{} class BException extends AException{} class CException extends AException{}

Example class AException extends Runtime. Exception{} class BException extends AException{} class CException extends AException{} AException class ex 11 BException CException { public static void main(String args[]) Catch sub class Exceptions First then { super class Exceptions try { int a=10; D: javabin>javac ex 11. java } ex 11. java: 14: exception BException has already been caught catch(AException e) {} catch(BException e) {} ^ catch(CException e) {} ex 11. java: 15: exception CException has already been caught } catch(CException e) {} } ^ 2 errors

Exception Example class Student { private String name; private String idno; Exception Student. Exception

Exception Example class Student { private String name; private String idno; Exception Student. Exception Invalid. Name Exception Invalid. Id Exception Invalid. Year Exception Invalid. Number Exception

class Student. Exception extends Exception { Student. Exception(String msg) { super(msg); System. out. println(msg);

class Student. Exception extends Exception { Student. Exception(String msg) { super(msg); System. out. println(msg); } } class Invalid. Name. Exception extends Student. Exception { Invalid. Name. Exception(String msg) { super(msg); System. out. println(msg); } } class Invalid. Id. Exception extends Student. Exception { Invalid. Id. Exception(String msg) { super(msg); System. out. println(msg); }}

class Invalid. Year. Exception extends Student. Exception { Invalid. Year. Exception(String msg) { super(msg);

class Invalid. Year. Exception extends Student. Exception { Invalid. Year. Exception(String msg) { super(msg); System. out. println(msg); } } class Invalid. Number. Exception extends Student. Exception { Invalid. Number. Exception(String msg) { super(msg); System. out. println(msg); } }

class Student { private String name; private String idno; private boolean contains. Alphabets. Only(String

class Student { private String name; private String idno; private boolean contains. Alphabets. Only(String str) { for(int i=0; i<str. length(); i++) { int j = str. char. At(i); if(j < 65) return false; if(j > 125) return false; if(j > 91 && j < 96) return false; } return true; }

Student(String name, String idno) throws Student. Exception { if(!contains. Alphabets. Only(name)) throw new Invalid.

Student(String name, String idno) throws Student. Exception { if(!contains. Alphabets. Only(name)) throw new Invalid. Name. Exception("Invalid Name"); int a = Integer. parse. Int(idno. substring(0, 4)); if( a < 1995 || a > 2007) throw new Invalid. Year. Exception("Invalid Id Year"); int b = Integer. parse. Int(idno. substring(8)); if( b <= 0 || b > 999) throw new Invalid. Number. Exception("Invalid Student Number"); this. name = name; this. idno = idno; } }// End of student class

class exceptiontest { public static void main(String args[]) throws Student. Exception { Student std

class exceptiontest { public static void main(String args[]) throws Student. Exception { Student std 1 = new Student("123", "sttts"); } } E: oop>java studentexception Exception in thread "main" java. lang. No. Class. Def. Found. Error: studentexception (wrong name: Student. Exception)at java. lang. Class. Loader. define. Class 1(Native Method) at java. lang. Class. Loader. define. Class(Unknown Source) at java. security. Secure. Class. Loader. define. Class(Unknown Source)at java. net. URLClass. Loader. define. Class(Unknown Source)

Sample Example 1 class Sample { public static void main(String args[]) { try {

Sample Example 1 class Sample { public static void main(String args[]) { try { int a = 10; } catch(Exception e) {} } // End of main() }//End of class Sample NO ERROR

Example 2 import java. io. *; class Sample { public static void main(String args[])

Example 2 import java. io. *; class Sample { public static void main(String args[]) { try { int a = 10; Sample. java: 10: exception } java. io. IOException is never thrown in body catch(IOException e) {} of corresponding try statement catch(IOException e) {} }// End of main() ^ }//End of class sample 1 error

Example 3 import java. io. *; class Sample { public static void main(String args[])

Example 3 import java. io. *; class Sample { public static void main(String args[]) { try { int a = 10; } catch(Exception e) {} catch(Runtime. Exception e) {} } D: javabin>javac Sample. java }//End of class Sample. java: 11: exception java. lang. Runtime. Exception has already been caught catch(Runtime. Exception e) {} ^ 1 error

Example 4 class Exception. Demo 4 { public static void main(String args[]) throws Exception

Example 4 class Exception. Demo 4 { public static void main(String args[]) throws Exception { int a= 10; int b = a + 10; System. out. println("a="+a+"b="+b); } } E: Java Programs>javac Exception. Demo 4. java E: Java Programs>java Exception. Demo 4 a=10 b=20

Example 5 class Exception. Demo 4 { public static void main(String args[]) throws Runtime.

Example 5 class Exception. Demo 4 { public static void main(String args[]) throws Runtime. Exception { int a= 10; int b = a + 10; System. out. println("a="+a+"b="+b); } } E: Java Programs>javac Exception. Demo 4. java E: Java Programs>java Exception. Demo 4 a=10 b=20

Example 6 import java. io. *; class Exception. Demo 4 { public static void

Example 6 import java. io. *; class Exception. Demo 4 { public static void main(String args[]) throws IOException { int a= 10; int b = a + 10; System. out. println("a="+a+"b="+b); } } E: Java Programs>javac Exception. Demo 4. java E: Java Programs>java Exception. Demo 4 a=10 b=20

Example 7 class A { public void display() throws Exception { System. out. println("Hello");

Example 7 class A { public void display() throws Exception { System. out. println("Hello"); }// End of display() method is overridden in sub class B. }// End of class A A’s display throws Exception class B extends A B’s display throws Runtime. Exception { public void display() throws Runtime. Exception { System. out. println("Hi"); }// End of display() NO ERROR IN CODE. }// End of class B COMPILES SUCESSFULLY

Example 8 class A { public void display() throws Runtime. Exception { System. out.

Example 8 class A { public void display() throws Runtime. Exception { System. out. println("Hello"); display() method is overridden in sub class B. }// End of display() A’s display throws Runtime. Exception }// End of class A B’s display throws Exception class B extends A { public void display() throws Exception { System. out. println("Hi"); }// End of display() }// End of class B E: Java Programs>javac AB. java: 10: display() in B cannot override display() in A; overridden method does not throw java. lang. Exception public void display() throws Exception ^ 1 error

Example 9 import java. io. *; class A { public void display() throws Runtime.

Example 9 import java. io. *; class A { public void display() throws Runtime. Exception { display() method System. out. println("Hello"); is overridden in }// End of display() sub class B }// End of class A class B extends A { public void display() throws IOException { E: Java Programs>javac AB. java System. out. println("Hi"); AB. java: 10: display() in B cannot override }// End of display() in A; overridden method does not }// End of class B throw java. io. IOException public void display() throws IOException

Example 10 import java. io. *; class A { public void display() throws IOException

Example 10 import java. io. *; class A { public void display() throws IOException { System. out. println("Hello"); }// End of display() }// End of class A display() method is overridden in sub class B extends A { public void display() throws Runtime. Exception { System. out. println("Hi"); }// End of display() NO ERROR }// End of class B IN CODE. COMPILES SUCESSFULLY