Unchecked Exceptions OIn most cases, unchecked exceptions reflect programming logic errors that are not recoverable O For example, a NullPointerException is thrown if you access an object through a reference variable before an object is assigned to it Oan IndexoutofBounds Exception is thrown if you access an element in an array outside the bounds of the array tHese are the logic errors that should be corrected in the program OUnchecked exceptions can occur anywhere in the program. To avoid cumbersome overuse of try-catch blocks, Java does not mandate you to write code to catch unchecked exceptions Introduction to Java Programming, revised by Dai-kaiyu
Liang,Introduction to Java Programming,revised by Dai-kaiyu 11 Unchecked Exceptions ⚫In most cases, unchecked exceptions reflect programming logic errors that are not recoverable. ⚫ For example, a NullPointerException is thrown if you access an object through a reference variable before an object is assigned to it; ⚫an IndexOutOfBoundsException is thrown if you access an element in an array outside the bounds of the array. ⚫These are the logic errors that should be corrected in the program. ⚫Unchecked exceptions can occur anywhere in the program. To avoid cumbersome overuse of try-catch blocks, Java does not mandate you to write code to catch unchecked exceptions
Checked or Unchecked Exceptions Class Not FoundE IOException Arithmetic Exception Exception AWTException NullPointerException RuntimeEx IndexOutofBounds Exception Object Throwable Several more classes IllegalArgument Exception Linkageerror Several VirtualMachine Error El Unchecked e AWTError Several more classes Introduction to Java Programming, revised by Dai-kaiyu
Liang,Introduction to Java Programming,revised by Dai-kaiyu 12 Checked or Unchecked Exceptions LinkageError Error AWTError AWTException Throwable ClassNotFoundException VirtualMachineError IOException Exception RuntimeException Object ArithmeticException NullPointerException IndexOutOfBoundsException Several more classes Several more classes Several more classes IllegalArgumentException Unchecked Exceptions
Declaring, Throwing, and Catching @D Exceptions method1(( -declare exception lethod2():throws Exception invoke method2 if (an error occurs)( catch exception catch (Exception ex)i throw new Exception();throw exception Process exception Introduction to Java Programming, revised by Dai-kaiyu
Liang,Introduction to Java Programming,revised by Dai-kaiyu 13 Declaring, Throwing, and Catching Exceptions method1() { try { invoke method2; } catch (Exception ex) { Process exception; } } method2() throws Exception { if (an error occurs) { throw new Exception(); } } catch exception throw exception declare exception
Declaring Exceptions o Every method must state the types of checked exceptions it might throw This is known as declaring exceptions public void myMethodo throws IOException public void myMethodo throws IOException, Other Exception Introduction to Java Programming, revised by Dai-kaiyu
Liang,Introduction to Java Programming,revised by Dai-kaiyu 14 Declaring Exceptions ⚫Every method must state the types of checked exceptions it might throw. This is known as declaring exceptions. public void myMethod() throws IOException public void myMethod() throws IOException, OtherException