1 / 8

EXCEPTION HANDLING

EXCEPTION HANDLING. Overview What exceptions should be handled or thrown ? The syntax of the try statement. The semantics of the try statement. Unreachable catch clauses. The scope of a variable declared in a try block. What exceptions should be handled?. Exception. IOException.

altessa
Download Presentation

EXCEPTION HANDLING

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 HANDLING Overview • What exceptions should be handled or thrown ? • The syntax of the try statement. • The semantics of the try statement. • Unreachable catch clauses. • The scope of a variable declared in a try block.

  2. What exceptions should be handled? Exception IOException . . . . . . • In Lecture 7, we learned that Java exceptions are divided into the following major categories: • The exceptions of class Error and its subclasses describe internal errors inside the Java run-time system. There is little a programmer can do if such an error occurs. • Exceptions of class RuntimeException and its subclasses usually occur because there is a non-syntax error in a Java program. Such exceptions are avoidable by writing the program correctly. • Exceptions of class IOException and its subclasses usually occur because a bad thing, such as I/O, happened to otherwise a good program. • It is thus the exceptions of the IOExeption class and its subclasses that a Java program should deal with, either by handling them or by throwing them. Throwable Error . . . . . . RuntimeException

  3. The syntax of the try statement. • To handle an exception, a statement or statements that may throw exceptions are placed in a try block. • A catch clause, which follows a try block, defines how a particular kind of exception is handled. • A try block can have several catch clauses associated with it. • Each catch clause is called an exception handler. • The syntax of the try statement is: try { statement-list1 } catch(Exception-class1 variable1) { statement-list2 } catch(Exception-class2 variable2) { statement-list3 } . . . catch(Exception-classN variableN) { statement-listN-1 }

  4. The semantics of the try statement. • A catch statement cannot catch an exception thrown by another try statement, except in the case of nested try statements. • When a try statement is executed, there are three possible cases: 1. No exception is thrown All the statements in the try block are executed. No catch clause is executed. Processing continues with the statement following all the catch clauses for the try block. 2. An exception is thrown and there is a matching catch clause (A clause whose exception class type is the same as or is a superclass of the thrown exception). The statements in the try block following the statement that caused the exception are NOT executed. The first matching catch clause is executed. No other catch clause is executed. Processing continues with the statement following all the catch clauses for the try block.

  5. The semantics of the try statement (cont.) 3. An exception is thrown and there is no matching catch clause. Control is immediately returned to the method that called the method that caused the exception. This process is called propagating the exception. The propagation continues until the exception is caught, or until it is passed out of the Java program, where it will be caught by the default handler. What happens to a program when an exception is propagated all the way to the default handler? • For a console program, the program terminates and the handler displays an error message. • For a graphical application or for an applet, the handler displays an error message; but the program does not terminate. The program goes back to its user interface processing loop.

  6. Example using try Statement import java.io.*; public class Example1 { public static void main(String[ ] args) {BufferedReader stdin = new BufferedReader( new InputStreamReader(System.in)) ; try { System.out.println(“Enter an integer: ”) ; String inputLine = stdin.readLine( ) ; int num = Integer.parseInt(inputLine) ; System.out.println(“You entered ” + num ) ; } catch(IOException e) { System.out.println(“Input error ” + e ) ; } catch(NumberFormatException e) { System.out.println(“Invalid integer ” + e ) ; } } }

  7. Unreachable catch clauses • In multiple catch clauses, a clause whose exception class is a subclass of an exception class of another clause must appear before the other clause. • This is because a catch clause that uses a superclass will catch exceptions of that type plus any of its subclasses. • Thus if the catch clause that uses a subclass came after that using the superclass, it will be unreachable. In Java, unreachable code causes compile-time error. Example:the following code will cause compile-time error Since ArithmeticExceptionis a subclass ofException, try { int a = 0; int b = 42 / a ; } catch(Exception e) { System.out.println(e) ; } catch(ArithmeticException e) { System,out.println(“There is an arithmetic exception”) ; }

  8. Scope of a variable declared in a try block • The scope of a variable is that portion in a program where the variable can be used. • In Java, the scope of a variable is the block in which that variable is declared. • A block is a statement or a group of statements enclosed in curly braces: { } • Hence, a variable declared inside a try block has scope from the point it is declared to the end of the try block. IT CANNOT BE USED OUTSIDE THE TRY BLOCK, NOT EVEN IN A CATCH CLAUSE ASSOCIATED WITH THE TRY BLOCK.

More Related