Danh mục tài liệu

Lecture Java: Chapter 11

Số trang: 26      Loại file: pptx      Dung lượng: 657.56 KB      Lượt xem: 19      Lượt tải: 0    
Xem trước 3 trang đầu tiên của tài liệu này:

Thông tin tài liệu:

Lecture Java: Chapter 11 (Exception) focuses on the purpose of exceptions, exception messages, the try-catch statement, propagating exceptions, the exception class hierarchy.
Nội dung trích xuất từ tài liệu:
Lecture Java: Chapter 11Chapter 11Exception Java Software Solutions Foundations of Program Design Seventh Edition John Lewis William LoftusCopyright © 2012 Pearson Education, Inc.Exceptions• Exception handling is an important aspect of object- oriented design• Chapter 11 focuses on: – the purpose of exceptions – exception messages – the try-catch statement – propagating exceptions – the exception class hierarchy 10-2Outline Exception Handling The try-catch Statement Exception Classes I/O Exceptions 10-3Exceptions• An exception is an object that describes an unusual or erroneous situation• Exceptions are thrown by a program, and may be caught and handled by another part of the program• A program can be separated into a normal execution flow and an exception execution flow• An error is also represented as an object in Java, but usually represents a unrecoverable situation and should not be caught 10-4Exception Handling• Java has a predefined set of exceptions and errors that can occur during execution• A program can deal with an exception in one of three ways: – ignore it – handle it where it occurs – handle it an another place in the program• The manner in which an exception is processed is an important design consideration 10-5Exception Handling • If an exception is ignored by the program, the program will terminate abnormally and produce an appropriate message • The message includes a call stack trace that: – indicates the line on which the exception occurred – shows the method call trail that lead to the attempted execution of the offending line • See Zero.java (page 533) 10-6Outline Exception Handling The try-catch Statement Exception Classes I/O Exceptions 10-7The try Statement• To handle an exception in a program, the line that throws the exception is executed within a try block• A try block is followed by one or more catch clauses• Each catch clause has an associated exception type and is called an exception handler• When an exception occurs, processing continues at the first catch clause that matches the exception type• See ProductCodes.java (page 536) 10-8The finally Clause• A try statement can have an optional clause following the catch clauses, designated by the reserved word finally• The statements in the finally clause always are executed• If no exception is generated, the statements in the finally clause are executed after the statements in the try block complete• If an exception is generated, the statements in the finally clause are executed after the statements in the appropriate catch clause complete 10-9The finally Clause 10-Exception Propagation 10-11Exception• Bất cứ khi nào một lỗi xuất hiện trong khi thi hành chương trình, nghĩa là một ngoại lệ đã xuất hiện.• Ngoại lệ phát sinh vào lúc thực thi chương trình theo trình tự mã.• Mỗi ngoại lệ phát sinh ra phải bị bắt giữ , nếu không ứng dụng sẽ bị ngắt.• Việc xử lý ngoại lệ cho phép bạn kết hợp tất cả tiến trình xử lý lỗi trong một nơi. Lúc đó đoạn mã của bạn sẽ rõ ràng hơn.• Java sử dụng các khối ‘try’ và ‘catch’ để xử lý các ngoại lệ. Các câu lệnh trong khối ‘try’ chặn ngoại lệ còn khối ‘catch’ xử lý ngoại lệ.• Các khối chứa nhiều catch có thể được sử dụng để xử lý các kiểu ngoại lệ khác nhau theo cách khác nhau.• Từ khoá ‘throws’ liệt kê các ngoại lệ mà phương thức chặn.• Từ khoá ‘throw’ chỉ ra một ngoại lệ vừa xuất hiện.• Khối ‘finally’ khai báo các câu lệnh trả về nguồn tài nguyên cho hệ thống và in những câu thông báo 10-Exception Propagation• An exception can be handled at a higher level if it is not appropriate to handle it where it occurs• Exceptions propagate up through the method calling hierarchy until they are caught and handled or until they reach the level of the main method• A try block that contains a call to a method in which an exception is thrown can be used to catch that exception• See Propagation.java (page 539)• See ExceptionScope.java (page 540) 10-13Object Serialization Ngoại lệ Lớp cha của thứ tự phân cấp ngoại lệRuntimeException Lớp cơ sở cho nhiều ngoại lệ java.langArthmeticException Trạng thái lỗi về số, ví dụ như ‘chia cho 0’IllegalAccessException Lớp không thể truy cậpIllegalArgumentException Phương thức nhận một đối số không hợp lệArrayIndexOutOfBoundsExeption Kích thước của mảng lớn hơn 0 hay lớn hơn kích thước thật sự của mảngNullPointerException Khi muốn truy cập đối tượng nullSecurityException Việc thiết lập cơ chế bảo mật không được hoạt độngClassNotFoundException Không thể nạp lớp yêu cầuNumberFormatException Việc chuyển đối không thành công từ chuỗi sang số ...