1 . The exception chain is provided in the Java construction method. That is, we can use the construction method to continuously concatenate the anomaly into an abnormal chain ...
The reason why you need an exception is because of the understandable nature of the code, and the maintainability of reading and programs ...
We know we need a try catch for every exception we throw ... So the code is very bloated ...
If we can concatenate the exception into an exception, and then we only catch our wrapper exception, we know that runtimeexception and its derived classes can be automatically captured and handled by the JVM without a try catch.
Of course, we can define our own exception class from RuntimeException, and then through the first level of packaging, if the exception appears jwm through our custom runtimeexception direct output cause
(cause) is our anomaly chain. So all of our exceptions are exported, which reduces the number of exception handling code ...
Only throwable----> Exception runtimeexception Error provides a mechanism for constructing a method to implement an abnormal chain ... Other anomalies need to come through initcause.
Construct an abnormal connection.
The following code is a simple example of an exception connection ... You can print exceptions that occur during the entire program.
The public class Testt {public
static void A () throws exception{ //throws an exception to the superior to handle a
try {
B ();
} catch (Excepti On e) {
throw new Exception (e);
}
}
The public static void B () throws exception{//throws an exception to the superior to handle the
try {
C ();
} catch (Exception e) {
throw NE W Exception (e);
}
}
The public static void C () throws Exception {///throws an exception to the superior to handle
the null pointer exception in the "C exception chain ..." in the try {throw new nullpointerexception);
} catch (NullPointerException e) {
throw new Exception (e);
}
}
public static void Main (String[]args) {
try {
a ();
} catch (Exception e) {
e.printstacktrace ();
}
}
}
2, try catch ... Finally, there is a flaw is the exception. For example, three try catch nesting together ... An internal 2 Try catch can omit the catch .... Direct Try finally ...
Look at the code below, we've lost 2 exception messages.
public class MyTest {public
void open () throws exception{
throw new Exception () {public
String toString () {
return This.getclass (). GetName () + "ceryimmportexception";
};
} ;
}
public void Close () throws exception{
throw new Exception () {public
String toString () {
return This.getclass (). GetName () + "Close Exception";}
;
} ;
}
public void Three () throws exception{
throw new Exception () {public
String toString () {
return This.getclass (). GetName () + "three";}
;
} ;
}
public static void Main (STRING[]AGRS) {
MyTest mt=new MyTest ();
try{
try{
try{
mt.open ();
} Finally
{
System.out.println ("delete Open");
Mt.close ();
}
finally{
System.out.println ("delete close");
Mt.three ();
}
catch (Exception ex) {
ex.printstacktrace ();}}}
This article on the Java abnormal chain and abnormal loss is a small series to share all the content, hope to give you a reference, but also hope that we support the cloud-dwelling community.