Tham khảo tài liệu bài giảng điện tử môn tin học: developing comprehensive projects, công nghệ thông tin, tin học văn phòng phục vụ nhu cầu học tập, nghiên cứu và làm việc hiệu quả | Part IV: Developing Comprehensive Projects Chapter 10: Exception Handling Chapter 11: Multithreading Chapter 12: Multimedia Chapter 13: Input and Output Chapter 14: Networking Võ Phương Bình - ITFAC - DLU 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 Võ Phương Bình - ITFAC - DLU 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) Võ Phương Bình - ITFAC - DLU What Exceptions are For, cont To force you to handle Bad Things because return codes can be tedious and sometimes you’re lazy Võ Phương Bình - ITFAC - DLU Example File I/O public FileReader(String fileName) throws FileNotFoundException public void close() throws IOException Võ Phương Bình - ITFAC - DLU import .*; class OpenFile { public static void main(String[] args) { if ( > 0) { try { // Open a file: FileReader f = new FileReader(args[0]); (args[0] + " opened"); (); } catch (IOException x) { (x); } } } } Example File I/O, cont Võ Phương Bình - ITFAC - DLU What Exceptions are For, cont To signal errors from constructors because constructors have no return value Võ Phương Bình - ITFAC - DLU What Exceptions are NOT For NOT For Alternate Returns: ., when end-of-file is reached: while ((s = ()) != null) Exceptions are only for the exceptional! Võ Phương Bình - ITFAC - DLU 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 Võ Phương Bình - ITFAC - DLU Catching Exceptions, cont Place one or more catch-clauses after try-block runtime system | Part IV: Developing Comprehensive Projects Chapter 10: Exception Handling Chapter 11: Multithreading Chapter 12: Multimedia Chapter 13: Input and Output Chapter 14: Networking Võ Phương Bình - ITFAC - DLU 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 Võ Phương Bình - ITFAC - DLU 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) Võ Phương Bình - ITFAC - DLU What Exceptions are For, cont To force you to handle Bad Things because return codes can be tedious and sometimes you’re lazy Võ Phương Bình - ITFAC - DLU Example File I/O public FileReader(String fileName) throws FileNotFoundException public void close() throws IOException Võ .