Exception Capture Try-catch-finally

Source: Internet
Author: User

Questions and Answers in this article:

When are the code in Q:catch and finally executed?

A:catch the exception is caught when an exception occurs in the try; The code in the finally executes regardless of whether the code in the try is abnormal or not, and the resource is generally recycled.

1, the root class of the exception is exception. Exception classes are generally inherited from exception

Try

{

String s = null;

S.tostring ();

}

catch (NullReferenceException ex)

{

Console.WriteLine ("Empty" +ex. Message);

}

E is the exception class object where the exception occurs, as long as the variable name does not conflict.

NullReferenceException inherit from Exception exception class

We can also create an exception class ourselves inherit from exception

To create your own exception class

Class Exceptiontest1:exception
{
Public ExceptionTest1 (String msg)
: Base (MSG)
{
}
}

Throw create custom Exception class object

static void Setage (int nianling)
{
if (nianling < 0)
{
throw new ExceptionTest1 ("age cannot be less than 0");
}
Else
{
Program.age = nianling;//static methods and static properties cannot be used with this, not valid. Using the class name point
}

}

2, in the exception handling, once the try inside a problem, discard the try code block after the exception point code, jump directly to the catch inside execution. If there is code behind the try code, execution continues after the catch is processed.

3. Handling of multiple exceptions

Try

{

int a = 10;

int b = 0;

Console.WriteLine (A/b);

Int[] arr = {1, 2, 3};

Console.WriteLine (Arr[3]);

}

catch (DivideByZeroException AE)

{

Console.WriteLine ("Divisor cannot be 0");

}

catch (IndexOutOfRangeException AE)

{

Console.WriteLine ("Array out-of-bounds exception");

}

You can catch the parent exception so that you can catch all the subclass exceptions, but it is strongly not recommended to do so, especially if you don't have a reason to catch (Exception ex)

4, good exception handling habits:

Do not just catch the exception to do nothing or just print it, this is not the normal "exception handling."

Do not know how to deal with the catch, error is better than "Hide the error message". This thought "will not be wrong", in fact, the exception "eat", will let the program into a logical chaos State, to scientific and reasonable handling of the anomaly.

can be try...catch; try...finally can be try...catch...finally.

The following example is not allowed!

Try

{

String S=null;

S.tolower ();

}

catch (Exception ex)

{

Throw ex; //catches the exception and throws the exception again. My master preached that this is called the dog-poop author.

}

Finally

{

Console.WriteLine ("Finally executed");

}

Exception Capture Try-catch-finally

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.