Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 12 Exception Handling and Text IO

Similar presentations


Presentation on theme: "Chapter 12 Exception Handling and Text IO"— Presentation transcript:

1 Chapter 12 Exception Handling and Text IO

2 Motivations When a program runs into a runtime error, the program terminates abnormally. How can you handle the runtime error so that the program can continue to run or terminate gracefully? This is the subject we will introduce in this chapter. 2

3 Exception Advantages Now you see the advantages of using exception handling. It enables a method to throw an exception to its caller. Without this capability, a method must handle the exception or terminate the program. 3

4 Handling InputMismatchException
By handling InputMismatchException, your program will continuously read an input until it is correct. 4

5 Exception Types 5

6 System Errors System errors are thrown by JVM and represented in the Error class. The Error class describes internal system errors. Such errors rarely occur. If one does, there is little you can do beyond notifying the user and trying to terminate the program gracefully. 6

7 Exceptions Exception describes errors caused by your program and external circumstances. These errors can be caught and handled by your program. 7

8 Runtime Exceptions RuntimeException is caused by programming errors, such as bad casting, accessing an out-of-bounds array, and numeric errors. 8

9 Checked Exceptions vs. Unchecked Exceptions
RuntimeException, Error and their subclasses are known as unchecked exceptions. All other exceptions are known as checked exceptions, meaning that the compiler forces the programmer to check and deal with the exceptions. 9

10 Unchecked Exceptions In most cases, unchecked exceptions reflect programming logic errors that are not recoverable. For example, a NullPointerException is thrown if you access an object through a reference variable before an object is assigned to it; an IndexOutOfBoundsException is thrown if you access an element in an array outside the bounds of the array. These are the logic errors that should be corrected in the program. Unchecked exceptions can occur anywhere in the program. To avoid cumbersome overuse of try-catch blocks, Java does not mandate you to write code to catch unchecked exceptions. 10

11 Unchecked Exceptions Unchecked exception. 11

12 Declaring, Throwing, and Catching Exceptions
12

13 Declaring Exceptions Every method must state the types of checked exceptions it might throw. This is known as declaring exceptions. public void myMethod() throws IOException throws IOException, OtherException The above slide gives you an idea about how to declare an exception. If your exception is a checked exception meaning, if java forces you to handle an exception, then the method, that throws an exception using the keyword “throw”, should also declare, what exception you will be throwing. If you look at the example above then the method header says method name, parameter list and then throws keyword followed by exception name. This is a standard java convention, you don’t have to do it with unchecked exceptions. 13

14 Throwing Exceptions When the program detects an error, the program can create an instance of an appropriate exception type and throw it. This is known as throwing an exception. Here is an example, throw new TheException(); TheException ex = new TheException(); throw ex; Above is the java convention that you use when you throw an exception, basically you are declaring a new object of the exception class. Usually this line comes after a selection, if something , then throw new exceptionName. 14

15 Throwing Exceptions Example
/** Set a new radius */ public void setRadius(double newRadius) throws IllegalArgumentException { if (newRadius >= 0) radius = newRadius; else throw new IllegalArgumentException(); } Above is an example of checked exception. IllegalARgumentException, if a user enters an argument which is not legal, in our case a negative radius is illegal. Since it is checked, method header has its name. 15

16 Catching Exceptions try {
statements; // Statements that may throw exceptions } catch (Exception1 exVar1) { handler for exception1; catch (Exception2 exVar2) { handler for exception2; ... catch (ExceptionN exVar3) { handler for exceptionN; You can have multiple catch statements for a single try block, because your block may cause multiple exceptions. Above is an example of how to do it. 16

17 Catching Exceptions If you look at the example above, every try block has a method invocation. If an exception is thrown in method3, think what happens. Control is transferred to catch for exception3, statement 5 is not executed, once catch is done , statement 6 is executed. Now think about , what if exception occurs in method2. Also look at the call stack for the invocations, they are self-explanatory 17

18 Catch or Declare Checked Exceptions
Java forces you to deal with checked exceptions. If a method declares a checked exception (i.e., an exception other than Error or RuntimeException), you must invoke it in a try-catch block or declare to throw the exception in the calling method. For example, suppose that method p1 invokes method p2 and p2 may throw a checked exception (e.g., IOException), you have to write the code as shown in (a) or (b). This slide is self explanatory 18

19 Example: Declaring, Throwing, and Catching Exceptions
Objective: This example demonstrates declaring, throwing, and catching exceptions by modifying the setRadius method in the Circle class defined in Chapter 8. The new setRadius method throws an exception if radius is negative. Look at this example in your textbook. 19

20 Rethrowing Exceptions
try { statements; } catch(TheException ex) { perform operations before exits; throw ex; Sometimes , once you have caught an exception, you don’t know what to do with, so you throw it back to the main method or the method that called the current method. This is called rethrowing the exception and it is illustrated in the second line of the catch block. 20

21 The finally Clause try { statements; } catch(TheException ex) {
handling ex; finally { finalStatements; you also have an optional finally clause that executes a final statement, if the exception is caught. Also it executes if the exception is not caught. It comes after catch and above is an example of it. 21

22 Trace a Program Execution
animation Trace a Program Execution Suppose no exceptions in the statements try { statements; } catch(TheException ex) { handling ex; finally { finalStatements; Next statement; Go through the program execution, finally block will execute, no matter what, ‘next Statement’ may or maynot be executed but the finally block will. 22

23 Trace a Program Execution
animation Trace a Program Execution The final block is always executed try { statements; } catch(TheException ex) { handling ex; finally { finalStatements; Next statement; 23

24 Trace a Program Execution
animation Trace a Program Execution Next statement in the method is executed try { statements; } catch(TheException ex) { handling ex; finally { finalStatements; Next statement; 24

25 Trace a Program Execution
animation Trace a Program Execution Suppose an exception of type Exception1 is thrown in statement2 try { statement1; statement2; statement3; } catch(Exception1 ex) { handling ex; finally { finalStatements; Next statement; 25

26 Trace a Program Execution
animation Trace a Program Execution The exception is handled. try { statement1; statement2; statement3; } catch(Exception1 ex) { handling ex; finally { finalStatements; Next statement; 26

27 Trace a Program Execution
animation Trace a Program Execution The final block is always executed. try { statement1; statement2; statement3; } catch(Exception1 ex) { handling ex; finally { finalStatements; Next statement; 27

28 Trace a Program Execution
animation Trace a Program Execution The next statement in the method is now executed. try { statement1; statement2; statement3; } catch(Exception1 ex) { handling ex; finally { finalStatements; Next statement; 28

29 Trace a Program Execution
animation Trace a Program Execution try { statement1; statement2; statement3; } catch(Exception1 ex) { handling ex; catch(Exception2 ex) { throw ex; finally { finalStatements; Next statement; statement2 throws an exception of type Exception2. 29

30 Trace a Program Execution
animation Trace a Program Execution try { statement1; statement2; statement3; } catch(Exception1 ex) { handling ex; catch(Exception2 ex) { throw ex; finally { finalStatements; Next statement; Handling exception 30

31 Trace a Program Execution
animation Trace a Program Execution try { statement1; statement2; statement3; } catch(Exception1 ex) { handling ex; catch(Exception2 ex) { throw ex; finally { finalStatements; Next statement; Execute the final block 31

32 Trace a Program Execution
animation Trace a Program Execution try { statement1; statement2; statement3; } catch(Exception1 ex) { handling ex; catch(Exception2 ex) { throw ex; finally { finalStatements; Next statement; Rethrow the exception and control is transferred to the caller 32

33 Cautions When Using Exceptions
Exception handling separates error-handling code from normal programming tasks, thus making programs easier to read and to modify. Be aware, however, that exception handling usually requires more time and resources because it requires instantiating a new exception object, rolling back the call stack, and propagating the errors to the calling methods. When you use exceptions , it requires more memory, more work, so use it when absolutely important. 33

34 When to Throw Exceptions
An exception occurs in a method. If you want the exception to be processed by its caller, you should create an exception object and throw it. If you can handle the exception in the method where it occurs, there is no need to throw it. When you want to end the program gracefully and transfer the control back to the main method. 34

35 When to Use Exceptions When should you use the try-catch block in the code? You should use it to deal with unexpected error conditions. Do not use it to deal with simple, expected situations. For example, the following code try { System.out.println(refVar.toString()); } catch (NullPointerException ex) { System.out.println("refVar is null"); 35

36 When to Use Exceptions is better to be replaced by if (refVar != null)
System.out.println(refVar.toString()); else System.out.println("refVar is null"); 36

37 Defining Custom Exception Classes
Use the exception classes in the API whenever possible. Define custom exception classes if the predefined classes are not sufficient. Define custom exception classes by extending Exception or a subclass of Exception. You can create your own exceptions by inheriting the properties from java provided exception class. 37

38 Custom Exception Class Example
In Listing 13.8, the setRadius method throws an exception if the radius is negative. Suppose you wish to pass the radius to the handler, you have to create a custom exception class. There is an example in textbook that explains custom exception. If you notice , you have to extend Exception class. 38

39 The File Class The File class is intended to provide an abstraction that deals with most of the machine-dependent complexities of files and path names in a machine-independent fashion. The filename is a string. The File class is a wrapper class for the file name and its directory path. Eg: c:\book\Welcome.java File class helps you deal with files that are stored in your computer. You can access them, modify them, create new ones using java code. This is easier since you dont have to deal with file paths, like you do in command prompt, it is a basic level of abstraction 39

40 Obtaining file properties and manipulating file
This is your File class, which has 3 constructors and a bunch of methods that help you deal with and manipulate files. 40

41 Text I/O A File object encapsulates the properties of a file or a path, but does not contain the methods for reading/writing data from/to a file. In order to perform I/O, you need to create objects using appropriate Java I/O classes. The objects contain the methods for reading/writing data from/to a file. This section introduces how to read/write strings and numeric values from/to a text file using the Scanner and PrintWriter classes. File class will manipulate existing files but it doesn’t have any methods to read and write from/to the files. For that we have 2 different classes printWriter and Scanner. 41

42 Writing Data Using PrintWriter
You can write data onto a file using above class. Whenever you are using File and its associated classes, you have to include throws IOException in your class declaration, since IO (input/output) is a checked exception and java forces you to deal with it. 42

43 Try-with-resources Programmers often forget to close the file. JDK 7 provides the followings new try-with-resources syntax that automatically closes the files. try (declare and create resources) { Use the resource to process the file; } If you look at the examples in the text, everytime , you open a file, whether you read from it or write to it, you have to close the file using file.close() , if you dont do this, it causes file corruption. Your program may not work. Since usually developers forget to close a file , above is an alternative to that. 43

44 Reading Data Using Scanner
You can read data from the file using above class. Go through the methods. 44

45 Reading Data from the Web
Just like you can read data from a file on your computer, you can read data from a file on the Web. This is optional 45

46 Reading Data from the Web
URL url = new URL(" After a URL object is created, you can use the openStream() method defined in the URL class to open an input stream and use this stream to create a Scanner object as follows: Scanner input = new Scanner(url.openStream()); This is optional 46

47 Lab Write a program that prompts user to enter 2 integers, calculate their sum and write it on a file called sum.txt

48 Checkpoint Can File class be used to read and write a file?
What is PrintWriter used for ? What is Scanner used for ? What happens if you don’t close a file ? When will finally clause get executed.


Download ppt "Chapter 12 Exception Handling and Text IO"

Similar presentations


Ads by Google