Exception ( Exception ):
occurs during the execution of the program, indicating that the is now an illegal operating condition. Many of the methods in the JDK throw an exception object when an illegal condition is detected.
Example: array out of bounds and being0In addition.
int i = 1,j = 0, k;k = i/j;
The error will be:
the purpose of exception handling is to provide different error coping strategies according to the actual situation .and means to make the procedure more stable and safer. The primary purpose of exception handling is to provide an accurate error message explaining the failedthe type of cause, location, and error, while providing some resilienceit is possible to ensure that data integrity is not compromised and that the program can continue to run. hands-on brain: please read and runAboutexception.javaexample,Program:
Importjavax.swing.*;classaboutexception { Public Static voidMain (string[] a) {intI=1, j=0, K; K=i/J; Try{k= i/j;//causes Division-by-zero Exception//throw new Exception ("hello.exception!"); } Catch(ArithmeticException e) {System.out.println ("was removed by 0. "+e.getmessage ()); } Catch(Exception e) {if(Einstanceofarithmeticexception) System.out.println ("Divide by 0"); Else{System.out.println (E.getmessage ()); } } finally{Joptionpane.showconfirmdialog (NULL, "OK"); } }}
Result: The statements in Java that are used to catch exceptions are:
Try { / /The code that may have run incorrectly }catch(/// Exception Type Exception object reference){ // code to handle the exception }finally{ // code for "aftercare" }
Java all of the catches in the exception are derived from the Exception class.
Using the Java exception handling mechanism:Put the code that could be wrongTrythe statement block. an exception object is thrown when the program detects that an error has occurred. Abnormal PlaceThis error is captured and handled by the code. Catchthe code in the statement block is used to handle errors. When an exception occurs, the program control process isTrystatement block jumps toCatchstatement block. whether or not an exception occurs,finallyStatements in a statement block are always guaranteed to be executed. If you do not provide the appropriate exception handling code,JVMwill end the entire application .program. Classification of exceptions in JavaThrowableThere are two direct subclasses of the class:Exception: Problems that arise can be captured;Error: System error, usually byJVMprocessing. The catch exceptions can be divided into two categories:(1)CheckException:derived directly fromExceptionthe exception class that must be arrestedwon or re-declared thrown(2)Runtimeexception: derived fromruntimeexceptionthe exception class. MakewithThrowstatement can throw this exception object at any time:throw new ArithmeticException (...);
Java Exception Handling