Related Topics for exception handling

Source: Internet
Author: User
Tags finally block

Catch blocks must be arranged in the most specific to least specific order.

Finally block: The finally block executes as long as the control leaves the try block, which is best used to perform resource cleanup.

Specification:

Use try/finally instead of a try catch block to implement resource cleanup code first.

To describe why an exception occurred in one of the throws, such as Ken, and how to prevent it.

It is better to avoid catching an exception that does not have the correct action, and not to deal with the exception.

Sometimes a catch block can catch an exception, but it cannot be handled correctly or completely, at which point the catch block can be thrown back to throw an exception by using a separate throw statement without specifying any exceptions after it. The exception referenced by the exception variable is not specified, which preserves the "Call stack" information in the exception, and the throw exception replaces the previous information with the current call stack information. The original call stack is generally needed to be known while debugging.

Specification:

To use an empty throw statement when catching and re-throwing an exception, to keep the call stack.

To report an execution failure by throwing an exception instead of returning an error code.

Do not let public members use exceptions as return values or out parameters. To indicate an error through an exception, do not indicate an error by using them as the return value.

Avoid using exception handling to handle expected situations. Exceptions are designed specifically for the purpose of tracking exceptions, which are not anticipated in advance, and which can cause serious consequences.

In general, developers must assume that users will behave in unexpected ways, so they should write code in a defensive manner and plan ahead for all conceivable "foolish user behavior."

Exception handling can result in a millisecond-level performance penalty.

Use TryParse () to perform numeric conversions.

if (int. TryParse (Agetext,out Age))

{

The TryParse method returns a bool value, so it is required to return the conversion result with the Out keyword if the conversion succeeds.

}

This avoids exception handling for type conversions. This method is implemented for all numeric types in the CLI2.0 version

Related Topics for exception handling

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.