JavaIn2Methods of handling exceptions:
1and direct treatment in the place where the abnormal is made;
2, throw an exception to the caller, and let the tune-up handle.
Exception classification
1, Check for exceptions:Java.lang.Exception
2, Operation period Anomalies:Java.lang.RuntimeException
3Error:Java.lang.Error
The top layer isJava.lang.ThrowableClasses, inspection anomalies, operational anomalies, errors are all of this class? Sun class,
Ja Va. La N g. Exce pt I onAndJa Va. La N G. ErrorInherited?Ja Va. La N G. Th rowa ble,?
Java.lang.RuntimeExceptionInherited?Java.lang.Exception
1, Check for exceptions:
The procedure is correct, but because of external environmental conditions dissatisfaction? For example: User error and/ oProblem--Program tried to hit
Open a remote that does not existSocketor open a file that does not exist. This is not the logic of the program itself
Error, most likely a remote machine name error(user spelling error ), the software system, the program developer must
Consider and deal with this problem. The Java compiler enforces the requirement to handle such exceptions, and if such exceptions are not caught, the program will
cannot be compiled.
2. Abnormal operation period:
This means that there is a bug in the program, such as array out ofbounds, 0 is removed, the entry parameter does not meet the specification ... This type of exception requires a change of program
To avoid, the Java compiler enforces the requirement to handle such exceptions.
3. Error:
Rarely seen, but also difficult to solve through the program, it may originate from the program bug, but it is more likely to originate from environmental questions
such as memory exhaustion. Error in the program? To be processed and handled by the operating environment.
/*
Chestnut Anomaly Try...catch
*/
Import Java.util.Random;
public class Test_fanxing {
public static void Main (string[] args) {
int x = new Random (). Nextint (5);
int y = new Random (). Nextint (10);
Int[] Z = new INT[5];
try{
System.out.println ("x=" + x + "y=" + y + "y/x=" + (y/x));
System.out.println ("y=" + y + "z[y]=" + z[y]);
}
catch (ArithmeticException exc1) {
Step Capture arithmetic Operation exception information
System.out.println ("Arithmetic operation exception:" + Exc1.getmessage ());
}
catch (ArrayIndexOutOfBoundsException Exc2) {
SYSTEM.OUT.PRINTLN ("Array out-of-bounds exception:" + Exc2.getmessage ());
}
}
}
2、Finally
If you putFinallyBlock positionTry...catch ...Statement,finally block, will get hold of Universal insurance, Even if the former try block hair? No corresponding exception catch block, finally block will be on the
?
finally 1finally in block, the exception
2 3 In the code of the former? system.exit () 4, close cpu
/*
Chestnut finally
*/
Import java.io.FileNotFoundException;
Import Java.io.FileReader;
Import java.io.IOException;
Import Java.net.Socket;
public class Test_fanxing {
public static void Main (string[] args) {
FileReader FR = null;
Check for exceptions
1. Open a file that does not exist
try{
FR = new FileReader ("D:\\aa.txt");
System.exit (-1);//The Finally statement block is no longer executed with system.exit ()
Socket s = new socket ("192.168.956.1", 21);
}
/* catch (FileNotFoundException e) {
SYSTEM.OUT.PRINTLN ("File does not exist:" + e.getmessage ());//getmessage () Less than printstacktrace ()
E.printstacktrace ();//output Bug information
}*/
catch (IOException E2) {
E2.printstacktrace ();
}
finally {
Try.catch ... In a statement block, a finally statement block is typically executed regardless of the occurrence of an exception.
Generally speaking, put the resources that need to be closed. such as "file", "link", "Memory" ....
SYSTEM.OUT.PRINTLN ("Test enters finally statement block");
if (fr!=null) {
try{
Fr.close ();
}
catch (Exception e) {
E.printstacktrace ();
}
}
}
System.out.println ("OK1");
}
}
/*
Results
File does not exist: D:\aa.txt (the system cannot find the file specified.) )
Test enters finally statement block
OK1
2. If you comment out the first catch
Results
java.net.unknownhostexception:192.168.956.1
At Java.net.AbstractPlainSocketImpl.connect (abstractplainsocketimpl.java:178)
At Java.net.PlainSocketImpl.connect (plainsocketimpl.java:172)
At Java.net.SocksSocketImpl.connect (sockssocketimpl.java:392)
At Java.net.Socket.connect (socket.java:579)
At Java.net.Socket.connect (socket.java:528)
At Java.net.socket.<init> (socket.java:425)
At Java.net.socket.<init> (socket.java:208)
At Test_fanxing.main (test_fanxing.java:23)
Test enters finally statement block
OK1
*/
"Java" "Exception"