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

Source: Internet
Author: User
Tags finally block

Summary of Errors and exceptions

Because of the summary ,, Perhaps the concept of what is more , we have to learn to adapt .

The first is the exception in C # with the old processing system-level and user-level Error state , is a structured , Unified type-safe processing mechanism .

Because I began to want to go c++ So it's been a while. c++, feel c# c++

1. In c# , All exceptions must be exception An instance of the derived class type is represented by c++ You can use any value of any type to represent an exception

2. In c# , use finally block can write the terminating code that will be executed under normal execution and exception conditions c++

in 3.c# , System-level exceptions, such as overflow , are defined as exception classes that are expected to match by 0 except for meaningless, and is in the same position as the application-level error state .

( If you don't understand C + +, If you don't understand C + +, It's okay to learn ! ) It'll be done when you finish school. )

Cause of the exception :( Two conditions may cause an exception )

The first : the throw statement is used for self-interest and unconditional initiation . control will never execute Throw the following statement ( If you use the VS High version , you will find that if you Throw It's going to be an error after writing something . ).

 

Second : executing c# statements and expressions in the process of , Sometimes there are some exceptions to make certain operations unable to complete at this point, some exceptions are thrown for example in integer triggering operation If the denominator is 0, ( You write a case yourself ".

Let's talk about this. The parent class of the exception class : Exception

Exception has some notable properties that are shared by all exceptions :

The Message property is A read-only property of type string that contains the number of seconds for the cause of the exception that occurred ( Remember , How do I treat a custom exception class when I write it message ? ?).

InnerExceptionproperty isExceptionA read-only property of type.if its value is notNULL,it refers to the exception that caused the current one .,That means that the current exception is handling theinnerexceptionof theCatchis raised in the block..otherwise,it has a value ofNULL,indicates that the exception was not raised by another exception.The number of exception objects that are concatenated together in this way can be arbitrary.

The values of these two properties can be specified when invoking the instance constructor of Exception .

How exceptions are handled

First, let's introduce a concept: exception propagation. When an exception is thrown, the program transfers control to the first catch statement in a try statement that can handle the exception . This process of throwing from an exception to a proper exception-handling statement is an exception propagation.

exception propagation involves repeating the following steps until you find a match to the exception Catch sentences.

( 1 ) from the inner to the outer layer, each enclosing the throw point Try Statement

If the try contains one or more catch clauses, they are checked individually in the order in which they appear to locate the handling that is appropriate for the exception. The so-called handler for this exception is the first catch clause that defines the exception type or its base type , and the general catch clause can match any type of exception, once a match to that type is found catch clause, the exception propagation is ended, and the program control is transferred to the catch clause for execution.

If the current try block contains a finally block , the finally block is executed, and if another exception is thrown quickly, the currently processed exception is terminated. If not, The handling of the exception continues after the finally block finishes executing.

(2) If exception handling is not located in the current member function call, the call terminates.

Can you understand what I'm trying 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 aTry, with multipleCatch, after the exception appears,TryTransfer Control toCatch,Catchhas an exception variable, and its type is fromExceptioninherited from, here in the program,catch (ArithmeticException e)represents the exception that is called when there is a calculated overflow. If you change to the following, the program will 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 already included catch (ArithmeticException e) , and there is no error in the front because ArithmeticException cannot contain Exception.

That's what I want to say .

The next step is the predefined exception classes in C # : For example Arraytypemismatchexception

I think C # 's exception handling mechanism is very powerful , fast , But it is difficult to go into the following , for US beginners , first grasp a few small examples just fine , and then slowly in depth .

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

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

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.