Presentation is loading. Please wait.

Presentation is loading. Please wait.

© The McGraw-Hill Companies, 2006 Chapter 15. © The McGraw-Hill Companies, 2006 Exceptions an exception is an event that occurs during the life of a program.

Similar presentations

Presentation on theme: "© The McGraw-Hill Companies, 2006 Chapter 15. © The McGraw-Hill Companies, 2006 Exceptions an exception is an event that occurs during the life of a program."— Presentation transcript:

1 © The McGraw-Hill Companies, 2006 Chapter 15

2 © The McGraw-Hill Companies, 2006 Exceptions an exception is an event that occurs during the life of a program which could cause that program to behave unreliably; each type of event that could lead to an exception is associated with a pre-defined exception class in Java; when a given event occurs, the Java run-time environment determines which exception has occurred and an object of the given exception class is generated; this process is known as throwing an exception;

3 © The McGraw-Hill Companies, 2006 Pre-defined exception classes in Java

4 © The McGraw-Hill Companies, 2006 RuntimeExceptions vs IOExceptions the RuntimeException class deals with errors that arise from the logic of a program. the IOException class deals with external errors that could affect the program during input and output. Some examples of RuntimeExceptions Converting a string into a number, when the string contains non-numeric characters ; Accessing an array using an illegal index. Some examples of IOExceptions the keyboard locking; an external file being corrupted.

5 © The McGraw-Hill Companies, 2006 Checked and unchecked exceptions since nearly every Java instruction could result in a RuntimeException error, the Java compiler does not flag such instructions as potentially error-prone.; consequently these types of errors are known as unchecked exceptions; the Java compiler does flag up those instructions that may generate all other types of exception (such as IOException); consequently, these kinds of errors are known as checked exceptions.

6 © The McGraw-Hill Companies, 2006 Handling exceptions consider a simple program that: allows the user to enter an aptitude test mark at the keyboard; informs the user if he or she has passed the test and been allowed on a given course. instead of using the Scanner class, we will devise our own class, TestException, that will contain a class method called getInteger.

7 © The McGraw-Hill Companies, 2006 Outline AptitudeTest class

8 © The McGraw-Hill Companies, 2006 Outline TestException class

9 © The McGraw-Hill Companies, 2006 The read method of each character on the keyboard is represented by a Unicode number; the read method of is a bit like the next method of the Scanner class, except that it treats the String as a series of Unicode numbers; each number is considered to be of type byte, so that the String itself is an array of bytes.

10 © The McGraw-Hill Companies, 2006 Coding the getInteger method This is a first attempt, it will not compile!

11 © The McGraw-Hill Companies, 2006 A problem with the getInteger method this class will not compile because the read method of may throw a checked IOException (if the keyboard locks for example). Whenever a method that throws a checked exception, Java compiler insists that we acknowledge this exception in some way. there are always two ways to deal with an exception: deal with the exception within the method by catching it; pass on the exception out of the method by claiming it.

12 © The McGraw-Hill Companies, 2006 Claiming an exception the term claiming an exception refers to a given method having been marked to indicate that it will pass on an exception object that it might generate. to claim an exception we add a throws clause to our method header: here have made a decision to pass the exception on to any method that calls this getInteger method.

13 © The McGraw-Hill Companies, 2006 Revisiting the AptitudeTest class The problem? The main method makes a call to our getInteger method, and this method may now throw an IOException. One solution Let the main method throw this exception as well:

14 © The McGraw-Hill Companies, 2006 Another problem: NumberFormatException This exception is raised when trying to convert a string into a number when the string contains non-numeric characters. The compiler did not warn us about this because NumberFormatException is a subclass of RuntimeException and so is unchecked.

15 © The McGraw-Hill Companies, 2006 Catching an exception so for we have thrown exception objects out of the current method and up to the calling method.; another way out for an exception object is into a catch block within the method; this is known as catching an exception; in order to trap the exception object in a catch block you must surround the code that could generate the exception in a try block.

16 © The McGraw-Hill Companies, 2006 Syntax for using a try and catch block: as there may be more than one exception generated within a method, there may be more than one catch block below a try block - each dealing with a different exception.; within the catch block, programmers can interrogate the exception object using Exception methods

17 © The McGraw-Hill Companies, 2006 Re-writing the AptitudeTest class

18 © The McGraw-Hill Companies, 2006 The finally clause Three courses of action may now take place in a try block: 1. The instructions within the try block are all executed successfully; 2. An exception occurs within the try block; the try block is exited and a matching catch block is found for this exception; 3. An exception occurs within the try block; the try block is exited but no matching catch block is found for this exception; so the exception is thrown from the given method. it may be the case that, no matter which of these courses of action take place, you wish to execute some additional instructions before the method terminates; often such a scenario arises when you wish to carry out some cleanup code, such as closing a file or a network connection that you have opened in the try block; the finally clause allows you to do this.

19 © The McGraw-Hill Companies, 2006 The syntax for the finally clause

20 © The McGraw-Hill Companies, 2006 Exceptions in GUI applications Exceptions do not terminate GUI applications; however they may make them behave unpredictably so should still be handled.

21 © The McGraw-Hill Companies, 2006 Using exceptions in your own classes we have managed to avoid the need for exceptions in our own classes by using if statements to monitor abnormal situations, and to send back boolean error values when appropriate; sometimes the technique of avoidance and reporting of errors in return values does not work. in such cases exception handling techniques can be used.

22 © The McGraw-Hill Companies, 2006 The Bank constructor revisited a negative value would not be a valid array size, and this would cause an exception in the program; the name of the exception that may be thrown is NegativeArraySizeException. there is no way to report an error has occurred other than to use an exception, as constructors cannot return values (such as booleans) since they have no return type.

23 © The McGraw-Hill Companies, 2006 Dealing with the problem We could check for this NegativeArraySizeException within the constructor. How would we report back that an error has occurred? usually we would make use of the return value, but constructors have no return value; the only way to report back errors from constructors is to make use of exceptions.

24 © The McGraw-Hill Companies, 2006 How to inform users of this constructor that an exception may be thrown? Problems with this attempt it is not usually considered good programming practice for methods to claim unchecked exceptions; the underlying implementation (an array) is revealed in the name of the exception. If in future a decision was made to replace the array representation with another representation, this exception name will not be valid.

25 © The McGraw-Hill Companies, 2006 A second attempt throw a general exception (of type Exception), rather than a specific exception (like NegativeArraySizeIndex for example); in order to throw a general exception object you must use the new command to generate an object of type Exception.

26 © The McGraw-Hill Companies, 2006 Problems with throwing general exceptions The name of the exception does not explain the source of the problem; the getMessage method must be used to determine that; The catch clause we used to deal with the resulting exception object will catch any exception that is thrown, but we may wish to handle other exceptions in a different way to the constructor exception. Instead of throwing general exceptions, you can throw your own user-defined exceptions.

27 © The McGraw-Hill Companies, 2006 Creating your own exception classes You can create your own exception class by inheriting from any-predefined exception class. Generally speaking, if you wish your exception class to be unchecked then it should inherit from RuntimeException (or one of its subclasses); If you wish your exception to be checked you can inherit from the general Exception class.

28 © The McGraw-Hill Companies, 2006 Our own checked NegativeSizException class

29 © The McGraw-Hill Companies, 2006 Amending the Bank constructor

30 © The McGraw-Hill Companies, 2006 Re-throwing exceptions ordinarily, when an exception is caught in a catch block, that exception has been dealt with; it is, however, possible to throw an exception from within a catch block. this technique might be useful if the condition required for the if statement were difficult to formulate, or where there were several points in a method where an exception could be thrown.

31 © The McGraw-Hill Companies, 2006 Documenting exceptions exceptions thrown by methods can be documented using the Javadoc tool; the @throws tag should be used to document the name of any exceptions that may be thrown by a method. here is the Bank constructor, documented with Javadoc comments:

Download ppt "© The McGraw-Hill Companies, 2006 Chapter 15. © The McGraw-Hill Companies, 2006 Exceptions an exception is an event that occurs during the life of a program."

Similar presentations

Ads by Google