ArticleDirectory
Obtain detailed information about an exception.
Capture exceptions
// Trigger the private void test () {int I = 0; I = 12/I;} // directly catch the private void button#click (Object sender, eventargs e) exception) {try {test ();} catch (exception ex) {This. textbox1.text = ex. tostring ();}}
Error message:
System. dividebyzeroexception: try to divide by zero.
In the predictiontest. form1.test () Location C: \ Documents ents and Settings \ Administrator \ Desktop \ exceptiontest \ form1.cs: row 25
In predictiontest. form1.button1 _ click (Object sender, eventargs e) Location C: \ Documents ents and Settings \ Administrator \ Desktop \ predictiontest \ form1.cs: Row 33
The error message shows that the error line is I = 12/I.
Exception capture
In fact, it is possible that after the exception is captured, the exception will be thrown to the call.Program
// Indirect catch exception private void button2_click (Object sender, eventargs e) {try {test1 ();} catch (exception ex) {This. textbox1.text = ex. tostring () ;}} private void test1 () {try {test () ;}catch (exception ex) {// handle errors... throw ex ;}}
Error message:
System. dividebyzeroexception: try to divide by zero.
In predictiontest. form1.test1 () Location C: \ Documents ents and Settings \ Administrator \ Desktop \ predictiontest \ form1.cs: Row 63
In predictiontest. form1.button2 _ click (Object sender, eventargs e) Location C: \ Documents ents and Settings \ Administrator \ Desktop \ predictiontest \ form1.cs: row number 46
The error message indicates that the row throw ex is displayed.
Based on the above error prompts, it is difficult to find the root cause of the error. Why is I = 12/I not prompted?
BecauseCodeTry catch has been used to capture zero-division errors. This exception is thrown again after capture! After being captured by the outer function, only the error row information of throw is left in the error.
So how can we get specific exceptions?
// Solution private void button3_click (Object sender, eventargs e) {try {Test2 ();} catch (exception ex) {This. textbox1.text = ex. tostring () ;}} private void Test2 () {try {test () ;}catch (exception ex) {// custom exception throw new exception ("error! ", Ex );}}
Error message:
System. Exception: An error occurred! ---> System. dividebyzeroexception: try to divide by zero.
In the predictiontest. form1.test () Location C: \ Documents ents and Settings \ Administrator \ Desktop \ exceptiontest \ form1.cs: row 25
In predictiontest. form1.test2 () Location C: \ Documents ents and Settings \ Administrator \ Desktop \ predictiontest \ form1.cs: Row 84
--- End of the internal exception stack trace ---
In predictiontest. form1.test2 () Location C: \ Documents ents and Settings \ Administrator \ Desktop \ predictiontest \ form1.cs: Row 89
In predictiontest. form1.button3 _ click (Object sender, eventargs e) Location C: \ Documents ents and Settings \ Administrator \ Desktop \ predictiontest \ form1.cs: row number 72
Because throw new exception ("error! ", Ex); creates a new custom exception and sets the original exception as innerexception. Therefore, the exception information contains multiple levels of exception information.
Exception differences in debug release.
The above exception information is in debug mode.
Debug release gets the same result in vs debugging.
When release runs independently
No predictiontest. PDB in release Mode
System. Exception: An error occurred!
---> System. dividebyzeroexception: try to divide by zero.
In predictiontest. form1.test2 ()
--- End of the internal exception stack trace ---
In predictiontest. form1.test2 ()
In predictiontest. form1.button3 _ click (Object sender, eventargs E)
In release Mode
System. Exception: An error occurred!
---> System. dividebyzeroexception: try to divide by zero.
In the predictiontest. form1.test2 () Location C: \ Documents ents and Settings \ Administrator \ Desktop \ errorlog \ exceptiontest \ form1.cs: Row 84
--- End of the internal exception stack trace ---
In the predictiontest. form1.test2 () Location C: \ Documents ents and Settings \ Administrator \ Desktop \ errorlog \ exceptiontest \ form1.cs: row number 89
In predictiontest. form1.button3 _ click (Object sender, eventargs e) Location C: \ Documents and Settings \ Administrator \ Desktop \ errorlog \ exceptiontest \ form1.cs: Row 72
Summary
Note that you should useThrow new exception ("error! ", Ex );To obtain the location of the exception.
When you run the program in vs debugging or debug mode, you can obtain the location where the exception occurs.
However, in the standalone release mode, exceptions only record the initial call location.
When the execution directory does not contain *. PDB files, the exception information does not contain the code and row number information.
Download test code
Download: http://files.cnblogs.com/zjfree/ErrorLog.rar
Environment: win2003 + vs2005 + C #
Supplement
By yuanyou's solitary sword! If an exception is caught, write throw directly without throw ex. For example:
Try {test () ;}catch {// throw for exception handling ;}
For situations without exception handling, the statement is as follows:
Try {test ();} finally {// release resources}
the test is completely correct! I did not know before. Khan! Thank you very much for reminding me!