C # abnormal system

zhaozj2021-02-16  59

C # abnormal system

Exceptions typically triggered by applications (user programs, etc.) or runtime (public language runtime, and application runners) .Exception is based on all exception types. When an error occurs, the system or current application is currently executing The program reports an error by triggering an exception containing information about the error. After an abnormality occurs, it will be processed by the application or the default exception handler. Several exception classes are inherited directly from the Exception class, including two main types of exception classes. : 1.ApplicationException class, which is a base class for user-defined application exception type. 2. SystemException class, this type is a base class for predefined public language runtime exception classes. These two exception classes constitute almost all The basis for the application and run library exception.

When an error (these errors are failed runtime checks, if the error caused by the array croucher) occurs, the run library will trigger an exception of SystemException or its derived type.

Instead ApplicationException type exceptions are triggered by the user program instead of being triggered by the running library. These exceptions should be derived from the ApplicationException or Exception class. It is not recommended to capture systemException, which triggered SystemException in the application. Programming practices. And usually, most of the exceptions derived directly from Exception do not need to add any functions for the Exception class.

Note that the exceptions we have told generally refers to an exception that can be interactively (that is, we can do some processing, these exceptions are usually triggered by the application), that is, unsatisfactory exceptions. And those very serious Abnormal, that is, those thus triggered by the runtime or in the case of unrecoverable cases, including

ExecutionEngineException, StackoverflowException and

OutofMemoryException.

The interactive operation is derived from SystemException and further expanded by ExternalException. For example, COMEXCEPTION is an exception thrown during the COM Interop operation. It is derived from ExternalException .Win32Exception and Sehexception also derived from ExternalException.

C # abnormal occurrence

The public language runtime is supported based on an exception object and protected code block concept. That is, when the run library occurs when an exception occurs, the run library creates an object that represents the exception. Of course, you can also pass from the appropriate base. Abnormal derive class to create its own anomaly class.

When a non-ended application error occurs, an exception of the ApplicationException class and its derivative type type will be triggered.

We can explicitly trigger an exception by using the throw statement. You can also use the throw statement to raise an exception that has been captured again. Good coding practice is to add information to the re-emerged exception to provide more information in debugging. And abnormal processing method All languages ​​that use the run library have an exception in a similar manner. Most languages ​​use the structured exception handling in the TRY / CATCH / FINALLY form.

C # Use the TRY / CATCH block to capture exceptions

The public language runtime provides an exception handling model that is based on an exception representation in the object, that is, the program code and exception processing code are divided into the TRY block and the catch block. There is one or more CATCH blocks, each block Designed to handle a specific type of exception, or design a block to capture more specific than other blocks. If you want to process an exception that occurs during a code block during execution, you must first place the code block in Try. In block. (The code in the try statement is the TRY block), and the abnormal application code caused by the TRY block is placed in the catch statement, called the Catch block. Zero or more Catch blocks related to a TRY block All CATCH blocks contain a type filter that determines the exception type of the block. When an exception occurs in the TRY block, the system searches them in the order in which the CATCH block appears in the application code until it is positioned. This exception's Catch block. If the type of CATCH block specifies the exception type T or any fault is derived from an exception type T, the Catch block processes the T type and its derived type. System is found The first CATCH block is processed to stop search. Therefore, as shown in this section, the CATCH block that handles a type of abnormality in the application code must be specified before processing the CATCH block processing its base type. Therefore, the CATCH block of System.Exception is usually processed. If all CATCH blocks associated with the TRY block do not process the exception, and the current TRY block is nestled in other TRY blocks, the search is related to the previous TRY block. Catch block. If you still find the Catch block for this exception, transfer the exception edge tuning up, search for a stack frame (ie the current method (or function) master (or function)) Find a Catch block that handles the exception and looks until the exception is derived or called no more frames in the stack. If the top of the calling stack does not find the Catch block to process the exception, the default exception handler Processing this exception, then the application terminates. Details: When an exception occurs, the exception will pass upward along the stack and find the appropriate CATCH block. It is important to see the order of the CATCH statement, should handle the Catch block for processing specific exceptions. Put in front of the process of conventional anomaly (generally specifying a base class of a certain exception), otherwise the compiler may make an error. The lookup method of the CATCH block matches the type of exception to the exception name specified in the CATCH block. If Did not find a specific Catch block, you are captured by the existing conventional CatCH block. Such as If there is no conventional abnormal Catch block, the public language runtime captures the Catch block no capture exception. According to the runtime configuration, or there is a debug dialog, or the program stops executing and a dialog box containing exception information.

The following code example uses the TRY / CATCH block capture

InvalidCastException This example creates a name

Employee's class, this class has a single property - staff level

(EMLVEL).

PromoteemPloyee acquires objects and increases staff level.

DateTime instance passes

When promoteemployee method,

INVALIDCASTEXCEPTION.

Using system;

Public Class Employee

{

// Create Employee Level Property.

Public int EMLEVEL

{

get

{

Return (Emlevel);

}

set

{

EMLVEL = Value;

}

}

Int emlevel;

}

Public Class Ex13

{

Public Static Void PromoteemPloyee (Object EMP)

{

// Cast Object to Employee.

Employee E = (EMPLOYEE) EMP;

// Increment Employee Level.

E.emLevel = E.EMLVEL 1;

}

Public static void main ()

{

Try

{

Object o = new Employee ();

DateTime newyears = new datetime (2001, 1, 1); // Promote the New Employee.

PromoteemPloyee (O);

// Promote DateTime; Results in invalidcastexception as newyears is not an an Employee Instance.

PromoteemPloyee (Newyears);

}

Catch (InvalidCastexception E)

{

Console.writeline ("Error Passing Data To PromoteemPloyee Method." E);

}

}

}

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

New Post(0)