Exception Handling Preventing and Correcting Errors Exceptions An

  • Slides: 17
Download presentation
Exception Handling Preventing and Correcting Errors

Exception Handling Preventing and Correcting Errors

Exceptions • An exception is an object that describes an unusual or erroneous situation

Exceptions • An exception is an object that describes an unusual or erroneous situation • Exceptions are thrown by a program, and may be caught and handled by another part of the program 2

Exceptions • A program can be separated into a normal execution flow and an

Exceptions • A program can be separated into a normal execution flow and an exception execution flow 3

Error Vs Exception • An error is also represented as an object in Java,

Error Vs Exception • An error is also represented as an object in Java, but usually represents a unrecoverable situation and should not be caught 4

Predefined Exceptions • Java has a predefined set of exceptions and errors that can

Predefined Exceptions • Java has a predefined set of exceptions and errors that can occur during execution Number. Format. Exception Input. Mismatch. Exception Index. Out. Of. Bounds. Exception Null. Pointer. Exception … 5

Dealing With Exceptions A program can deal with an exception in one of three

Dealing With Exceptions A program can deal with an exception in one of three ways: at we ions th pt § ignore it h exce t i w e Mayb § handle it where it occurs § handle it an another place in the program create redef Java p cept x e d e in ions 6

Dealing With Exceptions We are actually dealing with it in our program, not in

Dealing With Exceptions We are actually dealing with it in our program, not in th original JAVA program that is throwing the exception. s ion t p ce ex d ine f e ed r ap v Ja Exception in thread "main" java. util. Input. Mismatch. Exception at java. util. Scanner. throw. For(Scanner. java: 818) at java. util. Scanner. next(Scanner. java: 1420) at java. util. Scanner. next. Int(Scanner. java: 2029) at java. util. Scanner. next. Int(Scanner. java: 1989) at Read. WScanner. main(Read. WScanner. java: 17) The manner in which an exception is processed is an important design consideration 7

Ignored Exception • If an exception is ignored by the program, the program will

Ignored Exception • If an exception is ignored by the program, the program will • terminate abnormally and produce an appropriate message The message includes a call stack trace that: §indicates the line on which the exception occurred §shows the method call trail that lead to the attempted execution of the offending line Exception in thread "main" java. util. Input. Mismatch. Exception at java. util. Scanner. throw. For(Scanner. java: 818) at java. util. Scanner. next(Scanner. java: 1420) at java. util. Scanner. next. Int(Scanner. java: 2029) at java. util. Scanner. next. Int(Scanner. java: 1989) at Read. WScanner. main(Read. WScanner. java: 17) java/Read. Input/old. Input/Parse 1. java/Read. Input/old. Input/Parse 2. java/Read. Input/old. Input/Read. Num. java 5/Temp. Converter. java 8

The try Statement • To handle an exception in a program, the line that

The try Statement • To handle an exception in a program, the line that throws the exception is executed within a try block try{ //risky code goes here } catch(Exception. Type and name){ • A try block is followed by one or more catch clauses //preventive measures } 9

The finally Clause • A try statement can have an optional clause following the

The finally Clause • A try statement can have an optional clause following the catch clauses, designated by the reserved word finally try{ //risky code goes here } catch(Exception. Type and name){ //preventive measures } finally{ //to be executed regardless of try outcome } 10

Finally executes… • The statements in the finally clause always are executed • If

Finally executes… • The statements in the finally clause always are executed • If no exception is generated, the statements in the finally clause are executed after the statements in the try block complete • If an exception is generated, the statements in the finally clause are executed after the statements in the appropriate catch clause complete 11

Exception Propagation • Exceptions propagate up through the method calling hierarchy until they are

Exception Propagation • Exceptions propagate up through the method calling hierarchy until they are caught and handled or until they reach the level of the main method • A try block that contains a call to a method in which an exception is thrown can be used to catch that exception Exception in thread "main" java. util. Input. Mismatch. Exception at java. util. Scanner. throw. For(Scanner. java: 818) at java. util. Scanner. next(Scanner. java: 1420) at java. util. Scanner. next. Int(Scanner. java: 2029) at java. util. Scanner. next. Int(Scanner. java: 1989) at Read. WScanner. main(Read. WScanner. java: 17) 12

Your Own Exceptions • A programmer can define an exception by extending the Exception

Your Own Exceptions • A programmer can define an exception by extending the Exception class or one of its descendants • The parent class used depends on how the new exception will be used this We wi late ll co r on me in t back he c to our se 13

Checked Exceptions • An exception is either checked or unchecked • A checked exception

Checked Exceptions • An exception is either checked or unchecked • A checked exception either § must be caught by a method, or ü- § must be listed in the throws clause of any method that may throw or propagate it. 14

Checked Exceptions • A throws clause is appended to the method header Public class

Checked Exceptions • A throws clause is appended to the method header Public class My. Class{ public static void main(String arg[ ] ) throws Exception{ • The compiler will issue an error if a checked exception is not caught or asserted in a throws clause //code } } 15

Unchecked Exceptions • An unchecked exception does not require explicit handling, though it could

Unchecked Exceptions • An unchecked exception does not require explicit handling, though it could be processed that way • The only unchecked exceptions in Java are objects of type Runtime. Exception or any of its descendants • Errors are similar to Runtime. Exception and its descendants in that: § Errors should not be caught § Errors do not require a throws clause ICS 111 Blanca Polo

Questions? java/Read. Input/old. Input/Parse. I. java/Read. Input/old. Input/Parse. D. java/Read. Input/old. Input/Read. Num 3.

Questions? java/Read. Input/old. Input/Parse. I. java/Read. Input/old. Input/Parse. D. java/Read. Input/old. Input/Read. Num 3. java Java 5/Add. java ICS 111 Blanca Polo