motivations chapter 12 exceptions and file input output
play

Motivations Chapter 12 Exceptions and File Input/Output When a - PDF document

Motivations Chapter 12 Exceptions and File Input/Output 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


  1. Motivations Chapter 12 Exceptions and File Input/Output 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. CS1: Java Programming Colorado State University Original slides by Daniel Liang Modified slides by Chris Wilcox Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All 1 2 rights reserved. rights reserved. Objectives Exception-Handling Overview To get an overview of exceptions and exception handling (§12.2). ✦ To explore the advantages of using exception handling (§12.2). ✦ To distinguish exception types: Error (fatal) vs. Exception (nonfatal) and checked vs. unchecked (§12.3). ✦ Show runtime error To declare exceptions in a method header (§12.4.1). ✦ To throw exceptions in a method (§12.4.2). ✦ To write a try-catch block to handle exceptions (§12.4.3). Run ✦ Quotient To explain how an exception is propagated (§12.4.3). ✦ To obtain information from an exception object (§12.4.4). ✦ To develop applications with exception handling (§12.4.5). ✦ Fix it using an if statement To use the finally clause in a try-catch block (§12.5). ✦ To use exceptions only for unexpected errors (§12.6). ✦ Run To rethrow exceptions in a catch block (§12.7). QuotientWithIf ✦ To create chained exceptions (§12.8). ✦ To define custom exception classes (§12.9). ✦ To discover file/directory properties, to delete and rename files/directories, and to create directories using the ✦ With a method File class (§12.10). To write data to a file using the PrintWriter class (§12.11.1). ✦ To use try-with-resources to ensure that the resources are closed automatically (§12.11.2). ✦ Run QuotientWithMethod To read data from a file using the Scanner class (§12.11.3). ✦ To understand how data is read using a Scanner (§12.11.4). ✦ To develop a program that replaces text in a file (§12.11.5). ✦ To read data from the Web (§12.12). ✦ To develop a Web crawler (§12.13). ✦ Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All 3 4 rights reserved. rights reserved.

  2. Exception Advantages Handling InputMismatchException Run Run QuotientWithException InputMismatchExceptionDemo Now you see the advantages of using exception handling. By handling InputMismatchException, your program will It enables a method to throw an exception to its caller. continuously read an input until it is correct. Without this capability, a method must handle the exception or terminate the program. Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All 5 6 rights reserved. rights reserved. Exception Types System Errors ClassNotFoundException ClassNotFoundException ArithmeticException ArithmeticException IOException IOException Exception Exception NullPointerException NullPointerException RuntimeException RuntimeException IndexOutOfBoundsException IndexOutOfBoundsException Many more classes Many more classes IllegalArgumentException IllegalArgumentException Object Throwable Object Throwable Many more classes Many more classes System errors are thrown by JVM and represented in the Error class. LinkageError LinkageError The Error class describes internal system errors. Such errors rarely Error VirtualMachineError Error VirtualMachineError occur. If one does, there is little you can do beyond notifying the Many more classes Many more classes user and trying to terminate the program gracefully. Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All 7 8 rights reserved. rights reserved.

  3. Exceptions Runtime Exceptions Exception describes errors caused by your program ClassNotFoundException ClassNotFoundException and external ArithmeticException ArithmeticException circumstances. These IOException IOException errors can be caught and Exception Exception NullPointerException NullPointerException handled by your program. RuntimeException RuntimeException IndexOutOfBoundsException IndexOutOfBoundsException Many more classes Many more classes IllegalArgumentException IllegalArgumentException Object Throwable Object Throwable Many more classes Many more classes LinkageError LinkageError RuntimeException is caused by programming errors, such as bad Error VirtualMachineError Error VirtualMachineError casting, accessing an out-of-bounds array, and numeric errors. Many more classes Many more classes Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All 9 10 rights reserved. rights reserved. Checked Exceptions vs. Unchecked Exceptions 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 RuntimeException, Error and their subclasses are through a reference variable before an object is assigned to known as unchecked exceptions . All other it; an IndexOutOfBoundsException is thrown if you access exceptions are known as checked exceptions , an element in an array outside the bounds of the array. meaning that the compiler forces the programmer These are the logic errors that should be corrected in the to check and deal with the exceptions. 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. Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All 11 12 rights reserved. rights reserved.

  4. Declaring, Throwing, and Unchecked Exceptions Catching Exceptions ClassNotFoundException ArithmeticException IOException Exception NullPointerException RuntimeException declare exception method1() { method2() throws Exception { IndexOutOfBoundsException try { if (an error occurs) { invoke method2; Many more classes } IllegalArgumentException Object Throwable catch exception throw new Exception(); throw exception catch (Exception ex) { } Process exception; } } } Many more classes LinkageError Unchecked Error VirtualMachineError exception. Many more classes Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All 13 14 rights reserved. rights reserved. Declaring Exceptions Throwing Exceptions When the program detects an error, the program Every method must state the types of checked can create an instance of an appropriate exception exceptions it might throw. This is known as type and throw it. This is known as throwing an declaring exceptions . exception . Here is an example, public void myMethod() throw new TheException(); throws IOException TheException ex = new TheException(); public void myMethod() throw ex; throws IOException, OtherException Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All 15 16 rights reserved. rights reserved.

  5. Catching Exceptions Throwing Exceptions Example try { statements; // Statements that may throw exceptions /** Set a new radius */ } public void setRadius(double newRadius) catch (Exception1 exVar1) { throws IllegalArgumentException { handler for exception1; if (newRadius >= 0) } radius = newRadius; catch (Exception2 exVar2) { else handler for exception2; throw new IllegalArgumentException( } ... "Radius cannot be negative"); catch (ExceptionN exVar3) { } handler for exceptionN; } Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All 17 18 rights reserved. rights reserved. Catch or Declare Checked Exceptions Catching Exceptions Suppose p2 is defined as follows: main method { method1 { An exception method2 { is thrown in ... ... ... try { try { try { method3 ... ... ... invoke method1; invoke method2; invoke method3; statement1; statement3; statement5; } } } catch (Exception1 ex1) { catch (Exception2 ex2) { catch (Exception3 ex3) { Process ex1; Process ex2; Process ex3; } } } statement2; statement4; statement6; } } } void p2() throws IOException { Call Stack if (a file does not exist) { method3 throw new IOException("File does not exist"); method2 method2 } method1 method1 method1 ... main method main method main method main method } Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All 19 20 rights reserved. rights reserved.

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend