The exception handling mainly involves try, catch, finally, throw, and throws keywords, and should be noted in the following points:
1. You cannot use a try, catch, or finally statement block alone, otherwise there is a compilation error, for example, the following code will fail at compile time: file File=new file ("D:/myfile.txt"); Try {flieoutputstream out=new flieoutputstream (file); Out.writte ("hello!". GetBytes ()); Compile error}
A 2.try statement block can be used to use only catch statement blocks, or only finally statement blocks. When used with a catch statement block, there can be multiple catch statement blocks, and only one for finally statement blocks. When catch and finally exist simultaneously, finally must be placed after the catch.
3. When a Try is used only with a finally statement block, you can cause the program to throw an exception after an exception has occurred and continue to execute other code in the method.
public static void Dofile () throws ioexception{//through throws throws the exception up the file File=new file ("D:/myfile.txt"); Flieoutputstream out=new flieoutputstream (file); Out.writte ("Start". GetBytes ()); Write data to MyFile.txt out.close ()//Turn off output stream Out.writte ("End". GetBytes ()); //Throw IOException exception System.out.println ("Previous line of code: Out.write (NULL)"); Do not execute the line code}
For the above code, if you want the exception to occur, throw the exception and continue executing the following code to use both the try and finally statement blocks.
public static void Dofile () throws ioexception{//through throws throws the exception up file File=new file ("D:/myfile.txt"); try{file file=new File ("D:/myfile.txt"); Flieoutputstream out=new flieoutputstream (file); Out.writte ("Start". GetBytes ()); Write data to MyFile.txt out.close ()//Turn off output stream Out.writte ("End". GetBytes ()); //Throw IOException Exception {System.out.println ("Previous line code: Out.write (NULL)");//execute the Line code}}
4. Try only catch statement blocks can use more than one catch statement block to catch multiple exceptions that may occur in a try statement block. After an exception occurs, the Java virtual opportunity is detected from up to down to detect whether the current catch statement block captures the statement that occurred in the TRY statement block, and if the match does not execute the other statement blocks. If more than one catch statement block catches the same type of exception, the catch statement block that catches the subclass exception is placed in front of the method.
File File=new file ("D:/myfile.txt"); try{file File=new file ("D:/myfile.txt"); Flieoutputstream out=new flieoutputstream (file); Out.writte ("Start". GetBytes ()); Out.close () Out.writte ("End". GetBytes ()); //Throw IOException exception}catch (ArithmeticException e) {System.out.println ("Caught ArithmeticException exception");} catch (IOException e) {//Execute code System.out.println ("Caught IOException exception") in the catch statement block;
The following code incorrectly places a catch statement block that captures the child class and the parent class, resulting in a compilation error. File File=new file ("D:/myfile.txt"); try{file File=new file ("D:/myfile.txt"); Flieoutputstream out=new flieoutputstream (file); Out.writte ("Start". GetBytes ()); Out.close () Out.writte ("End". GetBytes ()); //throws the IOException exception}catch (IOException e) {//Executes the code SYSTEM.OUT.PRINTLN in the Catch statement block ("Capture to the parent class exception exception");} catch (ArithmeticException e) {//Compilation error, the exception was first caught by the exception catch block, which never executes System.out.println (" The exception of the subclass IOException exception "); }
5. A variable declared in a try statement block is valid only in the current try statement block, and the variable cannot be accessed by a block or other location in the subsequent catch finally statement. Variables declared outside the try-catch=finally statement block, however, can be accessed in a try, catch, or finally statement block.
int age1=0; try{age1=integer.valueof ("20l");//throw numberformatexception exception int age2= integer.valueof ("24l"); Throw NumberFormatException exception}catch (ArithmeticException e) {age1=-1;//compilation succeeded Age2=0;//Compilation error, cannot parse Age2}finally{ System.out.println (AGE1); Compile successfully System.out.println (AGE2); Compilation error, unable to parse Age2} 6. For a check exception that occurred, you must either use the Try-catch statement to catch the processing, or throw it up through the throws statement, or compile incorrectly.
7. When an exception object is thrown using the throws statement, the code following the statement will not be executed, for example: File File=new file ("D:/myfile.txt"); try{flieoutputstream out=new flieoutputstream (file); Out.writte ("Start". GetBytes ());//write Data to MyFile.txt Out.close () Closes the output stream Out.writte ("End". GetBytes ()); //Throw IOException exception}catch (IOException e) {Throw e; System.out.println ("Throw e"); Compile error, code that will never be executed}