C # exception handling

Source: Internet
Author: User
Tags exception handling serialization throw exception

Introduction to Exceptions

1, System.Exception class

Message property: The cause of the exception and the contents of the exception

Souce property: Throws the name of an exception assembly

StackTrace Property: Method invocation case where an exception occurred

InnerException property: Exception contained in the secondary exception

2, try{}catch{}finally{}

Handling Exceptions

1. There is a difference between catch and catch (Exception) with no parameters

catch (Exception) captures all exceptions that derive from the Exception class, and catch with no parameters captures all exceptions, regardless of whether the exception derives from the Exception class.

2, catch with catch and finally is optional, but the two must choose one. A try can correspond to multiple catch, but a try can only correspond to a finally.

3. The statement in finally is bound to be executed regardless of whether an exception occurs in a try.

Abnormal propagation

If the exception occurs and is not caught by the corresponding catch, the exception is gradually passed up the call stack until an appropriate catch statement is encountered or is passed to the lowest calling method. If no catch is found, the exception is delivered to the. NET common language runtime, and the Common language runtime pops up a dialog box to display exception information.

Throw exception Throw

1, throw variable name;

Must be a exception exception or a exception-derived type

2, throw;

This throw statement has only one throw keyword and can only be used in a catch statement block, which means that the exception caught by the current catch statement is thrown.

Custom exceptions

Follow the principles

1, avoid the use of deep exception class inheritance hierarchy

2. Custom exception classes must inherit the System.Exception class or several other basic common exception classes

3, custom exception class name to end with exception

4. Custom exception classes should be serializable

5. The custom exception class should implement at least the following four constructors that are the same as the exception class

Public myexception () {}

Public myexception (String message) {}

Public myexception (String message,exception inner) {}

Protected MyException (System.Runtime.Serialization.SerializationInfo Info,system.runtim E.serialization.streamingcontext context) {}

6. Automatically insert custom exception class frame

You can get a custom exception class frame by typing exception in the editor and pressing the <Tab> key.

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.