Back to directory
InProgramIn order to ensure complianceCodeWe will add a try... the cache block is used to capture a known or unknown exception. This is normal. Any object-oriented language provides this basic function, and C # is no exception, in fact, this articleArticleIt is important that the order in which exceptions are thrown in a relatively deep method call.
In the following code block, the web layer calls the BLL layer method, while the BLL layer method calls the Dal layer method. In the three methods of the three layers, try is added... catch Block, while in BLL and Dal, I will intentionally let the program generate a known exception, capture it, and write the logs.
Check the Code:
1 Static Void Dal () 2 { 3 Int A = 0 ; 4 Try 5 { 6 Int B = 1 / A; 7 } 8 Catch (Exception E) 9 { 10 11 Errmsg. append (E. Message ); 12 13 } 14 } 15 16 Static Void Bll () 17 { 18 Try 19 { 20 Dal (); 21 Test T = Null ; 22 Console. writeline (T. Nam ); 23 } 24 Catch (Exception E) 25 { 26 27 Errmsg. append (E. Message ); 28 } 29 } 30 31 Static Void Web () 32 { 33 Try 34 { 35 Bll (); 36 } 37 Catch (Exception E) 38 { 39 40 Errmsg. append (E. Message ); 41 } 42 }
Call code:
Static VoidMain (String[] ARGs) {web (); console. writeline (errmsg. tostring ());}
The result is as follows:
The results show that exceptions are recorded in order of generation.
In other cases, can an exception be captured by itself if we do not capture it? The answer is no. Of course it cannot be captured!
1 Static Void Dal () 2 { 3 Int A = 0 ; 4 Try 5 { 6 Int B = 1 / A; 7 } 8 Catch (Exception E) 9 { 10 11 // Errmsg. append (E. Message ); 12 13 } 14 } 15 16 Static Void Bll () 17 { 18 Try 19 { 20 Dal (); 21 Test T = Null ; 22 Console. writeline (T. Nam ); 23 } 24 Catch (Exception E) 25 { 26 27 // Errmsg. append (E. Message ); 28 } 29 } 30 31 Static Void Web () 32 { 33 Try 34 { 35 Bll (); 36 } 37 Catch (Exception E) 38 { 39 Errmsg. append (E. Message ); 40 } 41 }
The result is as follows:
Back to directory