C # abnormal processing mechanism preliminary

xiaoxiao2021-03-06  60

C # abnormal processing mechanism preliminary

Today, I learned the C #'s abnormal handling mechanism, and now you will summarize the following:

First, C # exception handling is used to keywords

TRY is used to check an exception that occurs and helps send any possible exceptions.

Catch handles errors in a larger way to control, and you can have multiple CATCH clauses.

Finally's code blocks will be executed regardless of whether abnormalities have caused an exception.

Throw is used to trigger an exception, which can trigger predefined exceptions and custom exceptions.

Second, C # abnormal processing format

Try

{

Program block;

}

Catch (Exception E)

{

Exception handling code block;

}

Finally

{

The code block to be executed regardless of whether abnormalities occur;

}

Third, abnormal treatment

A simple example of a divisor and zero:

Public Class Divisoriszero

{

Private static void main ()

{

INT DIVIDEND = 10;

INT Divisor1 = 0;

INT Divisor2 = 5;

Int DivideValue;

Try

{

DivideValue = Dividend / Divisor1; // (1)

// DivideValue = Dividend / Divisor2; // (2)

System.console.writeline ("DivideValue = {0}", DiVideValue); // (3) This line will not be executed.

}

Catch

{

System.Console.writeline ("The passive exception value is: {0}", e);

}

Finally

{

System.console.writeline ("I will display anyway, I will show it.");

}

}

}

Note: (1) The line will be performed to throw an exception. If there is no Catch statement, the program will terminate, and the Catch clause without parameters can be used to capture any type of exception.

If (1) is released, enable (2) line, which means that the program does not have an exception, from output, the Finally code block will still be executed.

Multiple CATCH statements can be provided to the TRY statement to capture specific exceptions, as in the above example: 0 As the division, the DivideByzeroException type exception is triggered, and the CATCH statement in the above example can be modified as follows:

Catch (DivideByzeroExcection E)

{

System.console.writeline ("Zero cannot be used as a division! The abnormality value is: / n {0}", e);

}

Catch (Exception E)

{

System.Console.writeline ("Not / '' zero as an exception that caused by divisor /"! The exception value is: / n {0} ", e);

}

Why do you have a Catch (Exception E) clause? The reason is very simple, the Catch (DivideByzeroExcection e) clause can only capture specific exceptions, and the program code in the TRY may also produce other exceptions, which can only be captured by catch (Exception E).

The following table gives some common exceptions:

Commonly used in the SYSTEM namespace

Abnormal class name brief description

MEMBERACCESSEXCEPTION access error: Type member cannot be accessed argumentException parameter error: The parameter of the method is invalid

The argumentnullexception parameter is empty: pass an unacceptable empty parameter to the method

Arithmeticexception mathematical calculation error: Due to the abnormality caused by mathematical calculations, the coverage is wide.

ArrayTypeMatchThexception array type does not match

DivideByzeroException is zero

The format of the Formatexception parameter is incorrect

IndexOutofRangeException index is out of range, less than 0 or the index of the last element is large

INVALIDCASTEXCEPTION illegally enforced conversion, triggered during explicit conversion failure

MulticastNotSupportedException is not supported by multicast: combined two non-empty appointments fail

NotSupportedException calls are not implemented in the class

NullReferenceException triggered when references an empty reference object

OutofMemoryException cannot be caused for the new statement to allocate memory, insufficient memory

Overflowexception overflow

StackoverflowException stack overflow

TypeInitializationException Error Initiation Type: Static constructor triggered

NOTFINITENUMBEREXCEPTION 无限 无限 无限: Number is not legal

Fourth, define your own anomaly

In addition to predefined abnormalities, we can also create our own exception, the process is relatively simple:

(1) Declare an exception, the format is as follows:

Class ExceptionName: Exception {}

(Ii) Executivity:

Throw (ExceptionName);

Look at an example:

Class Iamsecondgrade: system.exception {} // declaration exception

Class Secondgrade

{

Public Static Int Mul (int first, int SECOND)

{

IF (First> 100 || SECOND> 100)

Throw new iamsecondgrade (); // triggered anomalies

Return (First * Second);

}

Public static void main ()

{

INT MUL_VALUE;

Try

{

Mul_Value = MUL (99, 56); System.Console.writeline ("99 and 56 accumulation: {0}", mul_value);

Mul_Value = MUL (101, 4);

System.console.writeline ("Exceeding is abnormal, this line is not executed.");

}

Catch (iamsecondgrade) // captures custom unusual exceptions

{

System.console.writeline ("I have the second grade, more than 100 multiplication, I won't. Hey, I have a customized exception.");

}

Catch (System.exception E)

{

System.console.writeline ("Non-customized exception. Its value is: {0}", e);

}

}

}

I learned some important features of some abnormalities today, and there are many other features, and I have to work hard later.

转载请注明原文地址:https://www.9cbs.com/read-116623.html

New Post(0)