Meaning of throw new exception in Catch Block

Source: Internet
Author: User
Tags try catch

Throw new exception (string, exception)

Throw new exception (string)

Throw explicitly throws an exception.

Msdn: exception (string, exception) initializes a new instance of the exception class by specifying the error message and referencing internal exceptions that are the cause of the exception.

After an exception is detected, record the exception information and throw the exception to notify the customer of the problem. BecauseProgramIf an exception occurs, the system crashes. Record the problem and crash again.

After an exception is caught, a new exception is thrown. Before throwing a new exception, you can perform operations on the previously caught exception, such as logging, obtaining exception information, and writing it to the new exception.

Int num = convert. toint32 (textbox1.text );
Try
{
If (num = 0)
{
Throw new argumentnullexception ("Zero! ");
}
If (num = 1)
{
Throw new exception ("one! ");
}
}
Catch (argumentnullexception ex)
{
MessageBox. Show (ex. Message );
}
Catch (exception ex)
{
MessageBox. Show (ex. Message );
}

Catch is used to handle errors, that is, when an error occurs, the catch part will be executed. Note: it is "when an error occurs ".

However, what do you do if you want to raise an error when no error occurs? You can use the throw statement to manually raise an error.

For example, in the above program, you first draw a text box, textbox1, and a button button1.CodePut it in the click event of button1.

If the Chinese text box is 0, an error is thrown. If the text box is 1, another error is thrown, and different solutions can be used.

I think there are three methods to attach exceptions to a Catch Block: one is to do nothing, and the catch statement contains only null statements. The other is, A MessageBox or another way is to throw a new exception.

The method to use depends on how you want to handle this exception.

If you want to directly notify the user, use MessageBox or something.

You can throw a new

If you want your program to run normally after the "this" exception occurs, just as nothing happens, you can leave a blank statement.

In short, try catch is meaningful and necessary, but it is your own idea to catch your program.

Generally, there is no need to explicitly throw an exception. The exception should be caused by the program itself running, but there are two situations:

1. During the test, for example, if you cannot simulate a memory depletion exception, you have to throw the exception explicitly.

2. Custom Exceptions require explicit throws, because the rules we have set do not meet these conditions.

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.