C # Exception Handling statements

Source: Internet
Author: User

An exception is an issue that occurs during program execution. Exceptions in C # are a response to special situations that occur when a program runs, such as trying to divide by 0.

Exception handling statements provide a way to transfer control of a program from one part to another. C # exception handling is built on four keywords:try,catch,finally , and throw.

First, try and catch

The try and catch are used to detect exceptions in the program, and if there is no exception the program will run normally, and if an exception occurs, it will immediately stop executing the try statement and jump to the catch statement execution.

Console.Write ("Please enter a number");
String a = Console.ReadLine ();
Try
{
int b =convert.toint32 (a);
}
Catch
{
Console.WriteLine ("Input is not a number!") ");
}

Second, finally

When try and Cathy Run, the finally statement is run,

But it runs without the finally statement, so the finally statement is quite useless.

Console.Write (" Please enter a number "= console.readline (); Try {  int b =Convert.ToInt32 (a);} Catch {Console.WriteLine (" input is not a number! ");}
Finally
{
Console.writr ("The last sentence");
}

Here is the finally dispensable, because the inside of the statement will always be executed.

Iv. Exception Handling Statement infrastructure

Try

{

Statements that may appear to be abnormal;

If the above statement has an exception, this statement does not execute, jump directly to catch;

}

Catch

{

Statements executed after an exception occurred;

}

Finally//can write not write

{

Whether there is any exception above will be executed at the end of the statement;

}

C # Exception Handling statements

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.