C # advanced programming ---- Summary of errors and exceptions

Source: Internet
Author: User
Tags finally block

C # advanced programming ---- Summary of errors and exceptions

Summary of errors and exceptions

Because of the summary, there may be many concepts, so we need to learn to adapt.

First, exceptions in C # are handled by the old system-level and user-level error states. It is a structured and unified type of secure processing mechanism.

Because I started to want to follow the C ++ path, after a while, I felt that C # was a little different from C ++ exception handling.

1. in C #, all exceptions must be represented by instances of the class type derived from exceptions. In C ++, any value of any type can be used to indicate exceptions.

2. in C #, The finally block can be used to compile the termination code for normal execution and execution in case of exceptions. in C ++, it is difficult to write such code without repeating the code.

3. in C #, system-level exceptions such as overflow, Division by zero, and meaningless all correspond to defined exception classes that are expected to match, and are in the same status as application-level errors.

(If you do not know C ++, if you do not know C ++, it doesn't matter. Learn it! After learning)

 

 

Causes of exceptions: (two possible causes)

First, the throw statement is used to trigger an unconditional self-interest. control will never execute the statements after throw (if you use the VS high-point version, you will find that if you write something after throw, an error will be reported ).

 

Second, some exceptions may occur during the execution of C # statements and expressions, which may cause some exceptions. for example, in an integer trigger operation, if the denominator is 0, an exception is thrown (you can write a case yourself ).

 

 

The following describes the parent class of the Exception class: Exception

Exception has some notable attributes shared by all exceptions:

The Message attribute is a read-only attribute of the string type, which contains the number of seconds for the cause of the exception (remember how to treat message when I write a custom exception class ?).

 

The InnerException attribute is a read-only attribute of the Exception type. if its value is not null, it references the exception that causes the current field, that is, the current exception is thrown in the catch block that handles the InnerException. otherwise, its value is null, indicating that the exception is not caused by another exception. the number of abnormal objects connected in this way can be arbitrary.

The values of these two attributes can be specified when the Exception instance constructor is called.

 

 

Exception Handling Method

First, we will introduce the concept of exception propagation. When an exception is thrown, the program transfers control to the first catch statement in the try statement that can handle the exception. This process from throwing an exception to transferring it to a suitable Exception Processing statement is an exception propagation.

To spread an exception, repeat the following steps until you find a catch sentence that matches the exception.

(1) execute a try statement from the layer to the outer layer to throw a vertex

If try contains one or more catch clauses, they will be checked one by one according to the order in which these clauses appear to locate the appropriate handling of the exception. The exception processing method is the first catch clause that defines the exception type or its base type. Generally, a catch clause can match any type of exception, once the catch clause that matches the type is found, the Exception Propagation ends, and the program control is transferred to the catch clause for execution.

 

If the current try block contains a finally block, the finally block will be executed. If another exception is thrown, the exception being processed will be terminated. If no, after the finally block is executed, the exception processing will continue.

(2) If the current member function call fails to locate exception handling, the call ends.

Do you understand what I want to say?

Using System;

Class ExceptionTestClass
{
Public static void Main ()
{
Int x = 0;
Try
{
Int y = 100/x;
}
Catch (ArithmeticException e)
{
Console. WriteLine (ArithmeticException Handler: {0}, e. ToString ());
}
Catch (Exception e)
{
Console. WriteLine (Generic Exception Handler: {0}, e. ToString ());
}
}
}

Here is a try, and multiple catch. When an Exception occurs, try transfers control to catch. catch contains an Exception variable and its type is inherited from Exception, in the program, catch (ArithmeticException e) indicates that this exception is called when computing overflow exists. If it is changed to the following, the program reports an error,
Class ExceptionTestClass
{
Public static void Main ()
{
Int x = 0;
Try
{
Int y = 100/x;
}

Catch (Exception e)
{
Console. WriteLine (Generic Exception Handler: {0}, e. ToString ());
}
Catch (ArithmeticException e)
{
Console. WriteLine (ArithmeticException Handler: {0}, e. ToString ());
}
}
}

This is because catch (Exception e) has included catch (ArithmeticException e). There is no prior error because ArithmeticException does not include Exception.

The above is what I want to express.

 

 

The following is the predefined exception classes in C #: ArrayTypeMismatchException

 

I personally think that the exception handling mechanism of C # is very powerful and easy to handle, but it is very difficult to go deep into the following. For Beginners, we can master a few small examples first, and then go deeper.

 

 

 

Related Article

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.