Java C5. Exception Handling

Exceptional event Error that occurs during runtime Cause normal program flow to be disrupted Examples Divide by zero errors Accessing the elements of an array beyond its range Invalid input Hard disk crash Opening a nonexistent file Heap memory exhausted | Chapter 5. Exception Handling ITSS Java Programming CAO Tuan-Dung, HUT What is an exception? Exceptional event Error that occurs during runtime Cause normal program flow to be disrupted Examples Divide by zero errors Accessing the elements of an array beyond its range Invalid input Hard disk crash Opening a non-existent file Heap memory exhausted Example // divide by zero class DivByZero { public static void main(String args[]) { (3/0); (“Pls. print me.”); } } // array out of bound int array[] = new int[2]; array[0] = 1; array[1] = 2; array[2] = 3; // execute method of object that does not exist String s = null; (); Exception Object ArithmeticException Exception Object ArrayIndexOutOfBoundException Exception Object NullPointerException Default exception handling Displays this error message Exception in thread "main" : / by zero at () Default exception handler Provided | Chapter 5. Exception Handling ITSS Java Programming CAO Tuan-Dung, HUT What is an exception? Exceptional event Error that occurs during runtime Cause normal program flow to be disrupted Examples Divide by zero errors Accessing the elements of an array beyond its range Invalid input Hard disk crash Opening a non-existent file Heap memory exhausted Example // divide by zero class DivByZero { public static void main(String args[]) { (3/0); (“Pls. print me.”); } } // array out of bound int array[] = new int[2]; array[0] = 1; array[1] = 2; array[2] = 3; // execute method of object that does not exist String s = null; (); Exception Object ArithmeticException Exception Object ArrayIndexOutOfBoundException Exception Object NullPointerException Default exception handling Displays this error message Exception in thread "main" : / by zero at () Default exception handler Provided by Java runtime Prints out exception description Prints the stack trace Hierarchy of methods where the exception occurred Causes the program to terminate What Happens When an Exception Occurs? When an exception occurs within a method, the method creates an exception object and hands it off to the runtime system Creating an exception object and handing it to the runtime system is called “throwing an exception” Exception object contains information about the error, including its type and the state of the program when the error occurred Lab: Exception Run this program and observe the exceptions exceptions of the type that is different according to the specified value. prompt> java Exceptions ExceptionNO:1→ArrayIndexOutOfBoundsException ExceptionNO:2→NullPointerException ExceptionNO:3→ArithmeticException class Exceptions { public static void main(String args[])throws Exception { if( != 1) { ("Usage : java Exceptions .

Không thể tạo bản xem trước, hãy bấm tải xuống
TÀI LIỆU MỚI ĐĂNG
22    70    1    01-05-2024
Đã 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.