Exceptions. Outline 7/31/2012. Exceptions. Exception handling is an important aspect of objectoriented. Chapter 10 focuses on:

Similar documents
Khối: Cao Đẳng nghề và Trung Cấp Năm 2009

Chương 5. Network Layer. Phần 1 - Địa chỉ IPv4. Tài liệu : Forouzan, Data Communication and Networking

Chương 5. Network Layer 19/09/2016 1

SIEMENS INDUSTRIAL NETWORKS

TÀI LIỆU THỰC HÀNH MÔN CƠ SỞ DỮ LIỆU NÂNG CAO

Bộ môn HTMT&TT, Khoa Công Nghệ Thông Tin và TT, ĐH Cần Thơ

Tài liệu hướng dẫn: Stored Procedure

Internet Protocol. Bởi: Phạm Nguyễn Bảo Nguyên

Cụ thể những công việc mà AndroidManifest.xml thực hiện: - Đặt tên cho Java package của ứng dụng.

Nội dung chính của chương. Các công nghệ đĩa cứng Cấu tạo vật lý của đĩa cứng Cấu tạo logic của đĩa cứng Cài đặt đĩa cứng như thế nào?

HƯỚNG DẪN SỬ DỤNG HỆ THỐNG CẬP NHẬT CHỨNG THƯ SỐ HOTLINE:

HƯỚNG DẪN SỬ DỤNG PLESK PANEL

LÂ P TRI NH WEB ASP.NET

LAB IP SLA Bài 1. Bùi Quốc Kỳ ***

Tröôøng Ñaïi Hoïc Baùch Khoa Tp.HCM LẬP TRÌNH MINICHAT. Bài giảng 3. Trang1. Khoa Khoa Học Và Kỹ Thuật Máy Tính

Cài đặt và cấu hình StarWind iscsi trên Windows. iscsi SAN là gì?

HƯỚNG DẪN CÀI ĐẶT PHẦN MỀM DIỆT VIRUS AVIRA

Bài Thực hành Asp.Net - Buổi 1 - Trang: 1

Entity Framework (EF)

LỖI LÚC THỰC THI và QUÁ TRÌNH GOM RÁC

CẤU TRÚC DỮ LIỆU NÂNG CAO

Bài tập căn bản Visual Basic.Net Vòng lặp. txtn. txtketqua. btntinh. txtn. txtketqua. btntinh. Trang 1

TỔNG QUAN VỀ.NET VÀ C#

BÀI TẬP THỰC HÀNH LẬP TRÌNH WINDOWS C#

Bộ môn MMT&TT, Khoa Công Nghệ Thông Tin và TT, ĐH Cần Thơ

Hướng dẫn cài đặt FPT

Chương 7. Application Layer. Tài liệu : Forouzan, Data Communication and Networking

Khoa KH & KTMT Bộ môn Kỹ Thuật Máy Tính

Tình huống 1: PPPoE với Username và Password

ĐỌC, GHI XML VỚI C# TRONG ADO.NET --- SỬ DỤNG VISUAL STUDIO

Đa ngôn ngữ (Internationalization) trong Servlet

Chương 6. Transport Layer. Tài liệu : Forouzan, Data Communication and Networking

BẢO MẬT TRONG SQL SERVER

BELGIUM ONLINE APPOINTMENT

Online Appointment System will work better with below conditions/ Hệ thống đặt hẹn online sẽ hoạt động tốt hơn với điều kiện sau đây:

B5: Time to coding. Tới thư mục src/example.java và thay đổi nội dung file như sau: Mã: package at.exam;

BÀI 6 LÀM VIỆC VỚI THÀNH PHẦN MỞ RỘNG CỦA CSS3

I. Hướng Dẫn Đăng Nhập:

GIẢI THUẬT ĐỊNH TUYẾN (ROUTING ALGORITHM)

Tạo Project với MPLAB

STACK và QUEUE. Lấy STACK

PHÁT TRIỂN ỨNG DỤNG WEB

BÀI THỰC HÀNH SỐ 1. Quản trị tập tin: 1/ Tạo các thư mục sau: Bài tập thực hành linux Linuxlab. bt1 bt11 bt111. bt121. bt12. bh1 bh11 bh111.

HƯỚNG DẪN SỬ DỤNG ĐẦU GHI H.264 DVR VISION VS (4CH - 8CH - 16CH)

Time Picker trong Android

HƯỚNG DẪN CÁCH SỬ DỤNG WINDOWS MOVIE MAKER

Khoa Công Nghệ Thông Tin Trường Đại Học Cần Thơ. Những hỗ trợ tiên tiến khác của SQL. Đỗ Thanh Nghị

Nhấn nút New để tạo 1 biến mới Trang 17

Bài tập lớn số 1. Giả lập bộ định thời

TÀI LIỆU HƯỚNG DẪN SỬ DỤNG HOSTING PLESK PANEL

Dọn "rác" Windows 7 vào dịp cuối năm

NHẬP MÔN LẬP TRÌNH KHOA HỌC DỮ LIỆU. Bài 10: Thư viện Pandas (2)

Giáo trình này được biên dịch theo sách hướng dẫn của Sun Light. Vì là hướng dẫn kỹ thuật, trong này những thuật ngữ kỹ thuật bằng tiếng Anh tôi chỉ

Cập nhật ResultSet trong JDBC

Kỹ thuật thu nhỏ đối tượng trong Design (Layout)

BÀI 1: VBA LÀ GÌ? TẠO MACRO, ỨNG DỤNG CÁC HÀM TỰ TẠO (UDF), CÀI ĐẶT ADD-INS VBA là gì?

Lab01: M V C Lưu ý: Để thực hành, các bạn phải cài Visual Studio 2013 trở lên mới hỗ trợ MVC5.

LẬP TRÌNH WINDOWS FORM VỚI CÁC CONTROL NÂNG CAO (Các control trình bày dữ liệu dưới dạng danh sách)

SIMULATE AND CONTROL ROBOT

Bài 13: C++11. EE3490: Kỹ thuật lập trình HK1 2017/2018 TS. Đào Trung Kiên ĐH Bách khoa Hà Nội

Exceptions (part 2) An exception is an object that describes an unusual or erroneous situation. Quick Review of Last Lecture.

Bài thực hành số 2 QUYỀN và ROLE

B3: Bên khung Package Explore bên trái đi tới thư mục res, bạn sẽ thấy có 3 thư mục con:

CHƯƠNG 2: CÁC ĐẶC ĐIỂM VỀ MÔI TRƯỜNG PHÁT TRIỂN (IDE)

biến Global có thể được áp dụng cho tất cả các extension trong tất cả các context trong Dialplan.

HƯỚNG DẪN SỬ DỤNG DỊCH VỤ CDN

GV: Phạm Đình Sắc or

Phần 2. SỬ DỤNG POWERPOINT ĐỂ CHUẨN BỊ NỘI DUNG TRÌNH BÀY

Phần 1: Hướng dẫn sử dụng PictureBox tạo Slide Show

Lập trình chuyên nâng cao. Lập trình phân tán (Distributed computing

2.1. Upcasting. Chương 9. Đa hình (Polymophism) Chương 9. Đa hình (Polymophism) Chương 9. Đa hình (Polymophism)

HTML DOM - Forms. MSc. nguyenhominhduc

Khối: Cao Đẳng nghề và Trung Cấp Năm 2009

Môn Học: Cơ Sở Dữ Liệu 2. Chương 3 LẤY DỮ LIỆU TỪ NHIỀU NGUỒN

Kích hoạt phương thức xa (RMI- Remote Method Invocation )

HƯỚNG DẪN SỬ DỤNG DỊCH VỤ CDN

Hướng Dẫn Thực Hành Tập tin & Thư mục

NHÚNG. Vi ñiều khiển BM Kỹ Thuật ðiện Tử - ðh Bách Khoa TP.HCM 2

Ôn tập Thiết bị mạng và truyền thông DH07TT - Lưu hành nội bộ (không sao chép dưới mọi hình thức)

Mạng máy tính - Computer Network: Hệ. Giao thức - Protocol:

Parallels Cloud Server 6.0

Giao tiếp giữa các tiến trình

Bài 10: Cấu trúc dữ liệu

Tính kế thừa-2. Bởi: Thanh Hiền Vũ. Định nghĩa lại các thành viên lớp cơ sở trong một lớp dẫn xuất:

1 Bước 1: Test thử kit LaunchPad.

HỢP ĐỒNG MUA BÁN HÀNG HÓA QUỐC TẾ GV: NGUYỄN THỊ BÍCH PHƯỢNG

Mô hình thực thi ASP.NET page Xây dựng Web Form HTML Control và Web Control Bổ sung code vào Page Page Event Life Cycle Postback event

MỤC LỤC. Giáo trình Thiết kế web Trang 1

Bài 10. Cấu trúc liên nối. khác nhau được gọi là cấu trúc liên nối. nhu cầu trao đổi giữa các module.

Bài 7: Các cấu trúc điều khiển

BÀI 6a LẬP TRÌNH ĐA TUYẾN (MULTI THREAD)

LEECH Chưởng trình Chat bằng c# : Source :

Mô hình dữ liệu quan hệ (Relational data model)

HƯỚNG DẪN QUẢN TRỊ HỆ THỐNG

Tìm hiểu Group Policy Object và các ví dụ

// handle of dialog box

Chapter 4 Menus, Functions And Common Dialog

LINQ TO SQL & ASP.NET

NỘI DUNG ÔN TẬP MÔN HỆ CƠ SỞ DỮ LIỆU

LẬP TRÌNH TRÊN MÔI TRƯỜNG WINDOWS *** ADO.NET

Transcription:

Exceptions Exceptions Exception handling is an important aspect of objectoriented design Chapter 10 focuses on: the purpose of exceptions exception messages the try-catch statement propagating exceptions the exception class hierarchy 2004 Pearson Addison-Wesley. All rights reserved 10-2 Outline The try-catch Statement Exception Classes 2004 Pearson Addison-Wesley. All rights reserved 10-3 1

Exceptions 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 2004 Pearson Addison-Wesley. All rights reserved 10-4 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 2004 Pearson Addison-Wesley. All rights reserved 10-5 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) 2004 Pearson Addison-Wesley. All rights reserved 10-6 2

Outline The try-catch Statement Exception Classes 2004 Pearson Addison-Wesley. All rights reserved 10-7 The 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) 2004 Pearson Addison-Wesley. All rights reserved 10-8 The 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 2004 Pearson Addison-Wesley. All rights reserved 10-9 3

The finally Clause 2004 Pearson Addison-Wesley. All rights reserved 10-10 Exception Propagation 2004 Pearson Addison-Wesley. All rights reserved 10-11 Exception 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 2004 Pearson Addison-Wesley. All rights reserved 10-12 4

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) 2004 Pearson Addison-Wesley. All rights reserved 10-13 Object 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.lang ArthmeticException Trạng thái lỗi về số, ví dụ như chia cho 0 IllegalAccessException Lớp không thể truy cập IllegalArgumentException 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ảng NullPointerException Khi muốn truy cập đối tượng null SecurityException Việc thiết lập cơ chế bảo mật không được hoạt động ClassNotFoundException Không thể nạp lớp yêu cầu NumberFormatException Việc chuyển đối không thành công từ chuỗi sang số thực AWTException Ngoại lệ về AWT IOException Lớp cha của các ngoại lệ I/O FileNotFoundException Không thể định vị tập tin EOFException Kết thúc một tập tin NoSuchMethodException Phương thức yêu cầu không tồn tại InterruptedException Khi một luồng bị ngắt 2004 Pearson Addison-Wesley. All rights reserved 10-14 Outline The try-catch Statement Exception Classes Tool Tips and Mnemonics Combo Boxes Scroll Panes and Split Panes 2004 Pearson Addison-Wesley. All rights reserved 10-15 5

The Exception Class Hierarchy Classes that define exceptions are related by inheritance, forming an exception class hierarchy All error and exception classes are descendents of the Throwable interface A programmer can define an exception by extending the Exception class or one of its descendants The parent class used depends on how the new exception will be used 2004 Pearson Addison-Wesley. All rights reserved 10-16 Checked Exceptions An exception is either checked or unchecked A checked exception either must be caught by a method, or must be listed in the throws clause of any method that may throw or propagate it A throws clause is appended to the method header The compiler will issue an error if a checked exception is not caught or asserted in a throws clause 2004 Pearson Addison-Wesley. All rights reserved 10-17 Unchecked Exceptions An unchecked exception does not require explicit handling, though it could be processed that way The only unchecked exceptions in Java are objects of type RuntimeException or any of its descendants Errors are similar to RuntimeException and its descendants in that: Errors should not be caught Errors do not require a throws clause 2004 Pearson Addison-Wesley. All rights reserved 10-18 6

The throw Statement Exceptions are thrown using the throw statement Usually a throw statement is executed inside an if statement that evaluates a condition to see if the exception should be thrown See CreatingExceptions.java (page 543) See OutOfRangeException.java (page 544) 2004 Pearson Addison-Wesley. All rights reserved 10-19 Outline The try-catch Statement Exception Classes Tool Tips and Mnemonics Combo Boxes Scroll Panes and Split Panes 2004 Pearson Addison-Wesley. All rights reserved 10-20 Let's examine issues related to exceptions and I/O A stream is a sequence of bytes that flow from a source to a destination In a program, we read information from an input stream and write information to an output stream A program can manage multiple streams simultaneously 2004 Pearson Addison-Wesley. All rights reserved 10-21 7

Standard I/O There are three standard I/O streams: standard output defined by System.out standard input defined by System.in standard error defined by System.err We use System.out when we execute println statements System.out and System.err typically represent a particular window on the monitor screen System.in typically represents keyboard input, which we've used many times with Scanner objects 2004 Pearson Addison-Wesley. All rights reserved 10-22 The IOException Class Operations performed by some I/O classes may throw an IOException A file might not exist Even if the file exists, a program may not be able to find it The file might not contain the kind of data we expect An IOException is a checked exception 2004 Pearson Addison-Wesley. All rights reserved 10-23 Writing Text Files In Chapter 5 we explored the use of the Scanner class to read input from a text file Let's now examine other classes that let us write data to a text file The FileWriter class represents a text output file, but with minimal support for manipulating data Therefore, we also rely on PrintStream objects, which have print and println methods defined for them 2004 Pearson Addison-Wesley. All rights reserved 10-24 8

Writing Text Files Finally, we'll also use the PrintWriter class for advanced internationalization and error checking We build the class that represents the output file by combining these classes appropriately See TestData.java (page 547) Output streams should be closed explicitly 2004 Pearson Addison-Wesley. All rights reserved 10-25 Summary Chapter 10 has focused on: the purpose of exceptions exception messages the try-catch statement propagating exceptions the exception class hierarchy 2004 Pearson Addison-Wesley. All rights reserved 10-26 9