When the method multi-layer is called, each layer has the corresponding CATCH process Re-throw, and the complete information that is unusually generated in the outermost layer cannot be obtained.
The initial code looks as follows:
Static void main (string [] args)
{
Try {
Method3 ();
} catch (exception e) {
Console.writeLine (E.TOString ());
}
Console.read ();
}
Static Void Method1 (int A, int b) {
Try {
INT C = A / B;
} catch (dividebyzeroException) {
Throw New CustomException ("Parameter B Can't BE 0);
}
}
Static void method2 () {
Try {
. . .
Method1 (1,0);
} catch (CustOmException E) {
. . .
Throw e;
}
}
Static void method3 () {
Try {
Method2 ();
. . .
} catch (CustOmException E) {
. . .
Throw e;
}
}
(This is just an example. When actually calling, it is certainly not too much to call so deep, it is possible to call between several classes. CustomException is a custom exception).
Output:
CustomException.custOMException: parameter b can't be 0
At customEnxception.class1.method3 () in. . . /class1.cs:Line 58
At CustOmException.class1.main (String [] args) in. . . /class1.cs:Line 19
Exceptions in Method1, the information that can be displayed is only to Method3. This kind of information is a bit not known in the case where the code is intricate.
Look at the ".NET Framework Program Design (Modified Edition)", a book is known, it is much better. Change all Throw E; you can get the following output:
CustomException.custOMException: parameter b can't be 0
AT CustomException.class1.method1 (INT32 A, INT32 B) IN. . . /class1.cs:Line 41
At customException.class1.method2 () in. . . /class1.cs:Line 50
At customEnxception.class1.method3 () in. . . /class1.cs:Line 58
At CustOmException.class1.main (String [] args) in. . . /class1.cs:Line 19
It turned out in throw e; when writing this, the CLR will reset an exception start point, so the previous information is lost.
This is obviously much better than the first time. But many times, we will not thrown the abnormality, sometimes turned into another abnormality, then throw it, in which case the same is the same as the beginning, will lose information. Although this loss is expected, this is to appear to be the same, but this situation is very annoying when you develop debugging, you can't find an exact place to find an abnormality in the first time, just zooming Yes.
At this time, it is necessary to use the constructor with the inNerexception parameter (the book saying that many exception types in the FCL do not provide the corresponding constructor, but I have encountered several people; and now there are some custom Abnormal is much), then get complete information with the toString method. At last:
Static void main (string [] args)
{
Try {
Method3 ();
} catch (exception e) {
Console.writeLine (E.TOString ());
}
Console.read ();
}
Static Void Method1 (int A, int b) {
Try {
INT C = A / B;
} catch (DivideByzeroException E) {
Throw New CustomException ("Parameter B Can't BE 0", E);
}
}
Static void method2 () {
Try {
. . .
Method1 (1,0);
} Catch (CustomException) {
Throw;
}
}
Static void method3 () {
Try {
Method2 ();
. . .
} catch (exception e) {
Throw New CustomException ("Invoke Method3 Error", E);
}
}
get:
CustomException.customException: Invoke Method3 Error ---> CustomException.custo
MEXCEPTION: Parameter B can't be 0 ---> System.diVideByzeroException: Try to divide zero
.
AT CustomException.class1.method1 (INT32 A, INT32 B) IN. . . /class1.cs:Line 36
--- The end of internal exception stack tracking ---
AT CustomException.class1.method1 (INT32 A, INT32 B) IN. . . /class1.cs:Line 44
At customException.class1.method2 () in. . . /class1.cs:Line 52
At customEnxception.class1.method3 () in. . . /class1.cs:Line 58
--- The end of internal exception stack tracking ---
At customEnxception.class1.method3 () in. . . /class1.cs:Line 60
At CustOmException.class1.main (String [] args) in. . . /class1.cs:Line 19
This is more convenient to find a mistake.