Anomaly / error
1.1 Basic structure of abnormal processing
The basic structure of abnormal processing is as follows:
Try {
SomeReallyExceptionAlMethod ();
} catch (nullpointersReption n) {// a subclass of runtimeException
.
} catch (runtimeexception r) {// a subclass of exception
.
} catch (ioException i) {// a subclass of exception
.
} catch (myfirstexception m) {// ur Subclass of Exception
.
} catch (Exception E) {// a subclass of throwable
.
} catch (throwable t) {
........................
} finally {...}
The exceptions and error classes in Java are inherited from the Throwable class, that is, the Throwable class has two subclasses: ERROR class and Exception class, the instance of the Error class is an internal error in the Java running environment, which is very small and very Deadly, we cannot or rarely process these errors, or capture these errors.
The Exception class can be divided into two categories, one is the runtime Exceptions (Runtime Excetions) such as SecurityException, ArrayIndexoutofbounds, NullPointerexception, etc .; the other is except for EOFEXCeption, etc. In the Exception class hierarchy, the more the upper Exception class, the more general, and the more the following Exception is more targeted. In most Exception classes are part in the java.lang package, others exist in other packages.
Generally, in the TRY code, capture possible errors in the Catch, generally starting from a specific error, always throwable, and if you get into the code that needs to be processed, such as Exit the cycle, release resources, and so on.
1.2 Declaring Methods Methods Methods
In the declaration method, you can add keyword throws to indicate that the method may generate a certain or some exception, such as:
Public Boolean myothermethod (int x, int y)
Throws Anexception, Anotherexeption, AthirdException {
...}
With such a statement, other places where the method uses this method is preferably used to process the exception specified by the method.
1.3 Creating your own anomaly
The exceptions defined by your own are inherited from other anomaly classes. It is best to find a close anomaly class to inherit, such as defining the failed tolerance of the file format, is best inherited from ioException. If you can't find a close anomaly inherit, then it is inherited directly from the Exception class because it is the nasal ancestors of all anomaly. The following is a simple custom exception class:
Class myexception extends exception {
Public myexception () {}
Public myException (String MSG) {
Super (MSG);
}
}
Description:
(1) Abnormal class generally has two constructor, a parameter, a parameter with string type;
(2) Of course, other constructor can be defined and their own variables and method members, such as defining methods can be used to record log;
The following is a reference to this anomaly class:
Public static void g () throws myexception {
System.out.Println ("Throwing MyException from G ()");
Throw new myexception ("Originated IN G ()");
}
Description:
(3) Reference anomalies after the method is defined, and you can put this class after the THROWS;
(4) You can use Throw new myException to throw an instance of custom anomalies;
1.4 other
(1) If you use the method with the throw definition, it should be placed in the try ... catch, and then the exception is left back, it is transmitted upwards;
(2) For some predictable resolved exceptions, it is best to process it directly in the program without throwing an exception;
Using an exception takes up a lot of runtime, so as little as possible;