Change uncertainty to confirmation ~ Sequence of thrown exceptions

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.