About C # Exception Handling!

Source: Internet
Author: User
1 using system;
2
3 namespace leleapplication2
4 {
5 /// <summary>
6 // Summary of class1.
7 /// </Summary>
8 class class1
9 {
10 static string [] etypes = {"NONE", "simple", "Index", "nested Index "};
11 [stathread]
12 static void main (string [] ARGs)
13 {
14 foreach (string etype in etypes)
15 {
16 try
17 {
18 console. writeline ("Main () Try block reached .");
19 console. writeline ("throwexception (\" {0} \ ") called.", etype );
20 throwexception (etype );
21 console. writeline ("Main () Try block continues .");
22}
23 catch (system. indexoutofrangeexception E)
24 {
25 console. writeline ("Main () system. indexoutofrangeexception catch" +
26 "Block reched. Message: \ n \" {0} \ "", E. Message );
27}
28 catch
29 {
30 console. writeline ("Main () General Catch Block reached .");
31}
32 finally
33 {
34 console. writeline ("Main () Finally block reached .");
35}
36 console. writeline ();
37}
38}
39 static void throwexception (string exceptiontype)
40 {
41 console. Write ("throwexception (\" {0} \ ") reached.", exceptiontype );
42 switch (exceptiontype)
43 {
44 case "NONE ":
45 console. writeline ("not throwing an exception .");
46 break;
47 case "simple ":
48 console. writeline ("throwing system. Exception .");
49 throw (new system. Exception ());
50 break;
51 Case "Index ":
52 console. writeline ("throwing system. indexoutofrangeexception .");
53 etypes [4] = "error ";
54 break;
55 case "nested Index ":
56 try
57 {
58 console. writeline ("throwexception (\" nested Index \ "):" + "tyr block reached .");
59 console. writeline ("throwexception (\" Index \ ") called .");
60 throwexception ("Index ");
61}
62 catch
63 {
64 console. writeline ("throwexception (\" nested Index \ ") General" + "Catch Block reached .");
65}
66 finally
67 {
68 console. writeline ("throwexception (\" nested Index \ ") Finally" + "Block reached .");
69}
70 break;
71}
72}
73}
74}
75
76. Handling of this exception indicates that, after an exception is generated, if no try operation is performed, the exception is returned to the upper level. If no try operation is performed, the exception is returned to the upper level. and so on. if it is caught by any Level 1, the level 1 won't get any more.

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.