C #3.0 Study Notes (3) Try catch Statement of exception handling program

Source: Internet
Author: User

1. What is an exception?

An exception is an error when the program runs. It violates a system or application constraint or unexpected situation during normal operations. If you use 0 to remove a number, an exception occurs.

Ii. Try... Catch... Finally... Statement structure?

Examples:

Try

{

Statement;

}

Catch (...)

{

Statement;

}

Finally

{

Statement;

}

1. The try block contains the code that is being protected by exceptions.

2. A Catch Block is a code block for handling exceptions, also known as an exception handling program.

3. Finally blocks are the code to be executed in any case, regardless of whether an exception occurs.

3. Example:

Namespace try_catch

{

Class Program

{

Static voidmain (string [] ARGs)

{

Myclass MC = new myclass ();

Try

{

MC. ();

}

Catch (dividebyzeroexception)

{

Console. writeline ("catch clause inmain ()");

}

Finally

{

Console. writeline ("finally Clause inmain ()");

}

Console. writeline ("after try statement inmain .");

Console. writeline ("-- keep running! ");

}

}

Class myclass

{

Public void ()

{

Try

{

B ();

}

Catch (system. nullreferenceexception)

{

Console. writeline ("catch clause in ()");

}

Finally

{

Console. writeline ("finally clause in ()");

}

}

Protected void B ()

{

Int x = 10, y = 0;

Try

{

X/= y;

}

Catch (system. indexoutofrangeexception)

{

Console. writeline ("catch clause in B ()");

}

Finally

{

Console. writeline ("finally clause in B ()");

}

}

}

}

The program output result is:

Finally clause in B ()

Finally clause in ()

Catch clause inmain ()

Finally clause inmain ()

After try statement inmain.

-- Keep running.

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.