TAILIEUCHUNG - Lecture 7: Exceptions and I/O

All exceptions extend the class Throwable, which immediately splits into two branches: Error and Exception Error: internal errors and resource exhaustion inside the Java runtime system. Little you can do. Exception: splits further into two branches. | Lecture 7: Exceptions and I/O Overview of the exception hierarchy A simplified diagram of the exception hierarchy in Java Throwable Error Exception IOException Runtime Exception All exceptions extend the class Throwable, which immediately splits into two branches: Error and Exception Error: internal errors and resource exhaustion inside the Java runtime system. Little you can do. Exception: splits further into two branches. Focus on the Exception branch Two branches of Exception exceptions that derived from RuntimeException examples: a bad cast, an out-of-array access happens because errors exist in your program. Your fault. those not in the type of RuntimeException example: trying to open a malformed URL program is good, other bad things happen. Not your fault. Checked exceptions vs. unchecked exceptions Unchecked exceptions: exceptions derived from the class Error or the class RuntimeException Checked exceptions: all other exceptions that are not unchecked exceptions If they occur, they must be dealt with in some way. The compiler will check whether you provide exception handlers for checked exceptions which may occur Declaring to throw checked exceptions A java method or constructor should be declared to throw exceptions under two situations: 1. It calls another method that throws a checked exception 2. It throws a checked exception with the throw statement inside its body Declare a method or constructor to throw an exception (or exceptions) by using throws clause in its header Single exception: public FileInputStream(String s) throws FileNotFoundException Multiple exception: public Image load(String s) throws EOFException, IOException You should NOT declare an unchecked exception after throws! They are either outside of your control, or should be avoid completely by correcting your code. Using throw to throw an exception Throw an exception under some bad situations : a method named readData is reading a file whose header says it contains 700

TAILIEUCHUNG - Chia sẻ tài liệu không giới hạn
Địa chỉ : 444 Hoang Hoa Tham, Hanoi, Viet Nam
Website : tailieuchung.com
Email : tailieuchung20@gmail.com
Tailieuchung.com là thư viện tài liệu trực tuyến, nơi chia sẽ trao đổi hàng triệu tài liệu như luận văn đồ án, sách, giáo trình, đề thi.
Chúng tôi không chịu trách nhiệm liên quan đến các vấn đề bản quyền nội dung tài liệu được thành viên tự nguyện đăng tải lên, nếu phát hiện thấy tài liệu xấu hoặc tài liệu có bản quyền xin hãy email cho chúng tôi.
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.