Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 114 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
114
Dung lượng
2,33 MB
Nội dung
08/13/14 Võ Phương Bình - ITFAC - DLU 1 Part IV: Developing Comprehensive Projects Chapter 10: Exception Handling Chapter 11: Multithreading Chapter 12: Multimedia Chapter 13: Input and Output Chapter 14: Networking 08/13/14 Võ Phương Bình - ITFAC - DLU 2 Chapter 10 Exception Handling What exceptions are for What exceptions are NOT for Catching & Throwing exceptions Exception Specifications Standard Java Exceptions Exceptions and Polymorphism The finally clause Resource Management Uncaught Exceptions 08/13/14 Võ Phương Bình - ITFAC - DLU 3 What Exceptions are For To handle Bad Things I/O errors, other runtime errors when a function fails to fulfill its specification so you can restore program stability (or exit gracefully) 08/13/14 Võ Phương Bình - ITFAC - DLU 4 What Exceptions are For, cont To force you to handle Bad Things because return codes can be tedious and sometimes you’re lazy 08/13/14 Võ Phương Bình - ITFAC - DLU 5 Example File I/O public FileReader(String fileName) throws FileNotFoundException public void close() throws IOException 08/13/14 Võ Phương Bình - ITFAC - DLU 6 import java.io.*; class OpenFile { public static void main(String[] args) { if (args.length > 0) { try { // Open a file: FileReader f = new FileReader(args[0]); System.out.println(args[0] + " opened"); f.close(); } catch (IOException x) { System.out.println(x); } } } } Example File I/O, cont 08/13/14 Võ Phương Bình - ITFAC - DLU 7 What Exceptions are For, cont To signal errors from constructors because constructors have no return value 08/13/14 Võ Phương Bình - ITFAC - DLU 8 What Exceptions are NOT For NOT For Alternate Returns: e.g., when end-of-file is reached: while ((s = f.readLine()) != null) … Exceptions are only for the exceptional! 08/13/14 Võ Phương Bình - ITFAC - DLU 9 Catching Exceptions Wrap code to be checked in a try-block checking occurs all the way down the execution stack try-blocks can be nested control resumes at most enclosed matching handler 08/13/14 Võ Phương Bình - ITFAC - DLU 10 Catching Exceptions, cont Place one or more catch-clauses after try- block runtime system looks back up the call stack for a matching handler subclass types match superclass types catching Exception catches everything (almost) handlers are checked in the order they appear place most derived types first! execution resumes after last handler if you let it (could branch or throw) [...]... Same as logic_error in C++ Program logic errors e.g., bad cast, using a null handle, array index violation, etc Shouldn’t happen! Similar in spirit to C’s assert( ) macro fixed during testing mainly for debugging These are called “unchecked exceptions” 08/13/14 Võ Phương Bình - ITFAC 26 java.lang.RuntimeException Subclasses (sample) ArithmeticException (e.g., divide by 0) . 08/13/14 Võ Phương Bình - ITFAC - DLU 1 Part IV: Developing Comprehensive Projects Chapter 10: Exception Handling Chapter 11: Multithreading Chapter