throw new Exception

Source: Internet
Author: User
Tags try catch


throw new Exception (String, Exception)

throw new Exception (String)
Throw is a definite throw exception

Msdn:exception (String, Exception) Initializes a new instance of the Exception class with a specified error message and a reference to the inner exception that is the cause of this exception.

After the exception is found, record the exception information and then throw an exception to let the customer know that there is a problem so that the technician can be notified. Because the program is abnormal after the chance of collapse is very large, first record the problem and then crash.

After the exception is caught, a new exception is thrown. Before you throw a new exception, you can do something about the previously caught exception, such as logging, getting exception information, and then writing to the new exception

int num = Convert.ToInt32 (TextBox1.Text);
Try
{
if (num = = 0)
{
throw new ArgumentNullException ("appears 0! ");
}
if (num = = 1)
{
throw new Exception ("Come on!" ");
}
}
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 is executed. Note: "When an error occurred."
But what happens if you want to throw an error without an error? You can use the throw statement to raise an error manually.

For example, the above program, you first draw a text box, TextBox1, a button button1, if you put this code in the Button1 click event.
If the text box is 0, an error is raised, and the text box text is 1 o'clock and another error is raised, and a different approach can be used.

I think there are three ways to attach exceptions to the CATCH block: One way is to do nothing, only empty statements in a catch statement, and another way to use a notification exception such as the MessageBox, and one way to throw a new exception.
Which method to use depends on how you want to handle the exception.
If you want to notify the user directly, use the MessageBox and so on.
If you want to be handled by a program that calls this function, you can throw a new
If you want your program to run with "This" exception, as if nothing happened, you can empty the statement.
In summary, try catch is meaningful and necessary, but how your program catch is your own idea.
In general, there is no need to explicitly throw out an exception, the exception should be due to the problem of running the program itself, but there are two cases:
1. When testing, for example, you can not really simulate a memory exhaustion of this exception, then had to explicitly throw one.

2. Custom exceptions need to be explicitly thrown out, because meeting this condition is often not enough for our own rules.


throw new Exception

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.