C # capture exceptions

Source: Internet
Author: User

Exception:Programming LanguageTo reduce complexity.CodeAnd reduces the number of codes written and maintained.

Try to throw an exception by value and capture the exception by reference.

For example:

 

 
Void test (item) {try {If (/* Some test failed */) {Throw _ com_error (e_fail) ;}} catch (_ com_error & comerror) {// process comerror //}}

You are familiar with this:

 
Try {...} catch (system. Exception e ){...}

The exception exists at every moment of the system. You don't notice or throw it. You should not catch errors with unspecified exceptions, such

The C # code is abnormal.

You should capture more exceptions (meaningful exceptions) that are most likely to be obtained. For example, when a null parameter may be encountered, argumentnullexception is thrown.

Instead of the exception argumentexception of its base class.

Alternatively, you can throw a general exception in the final Catch Block.

Throwing system. Exception and capturing system. Exception is always an error. You can be "lazy". Once that method is not correctly run,

You have to spend a lot of time looking for the error.

Example:

 
// Good: Try {...} catch (system. nullreferenceexception exc ){...} catch (system. argumentoutofrangeexception exc ){...} catch (system. invalidcastexception exc ){...} // bad: Try {...} catch (exception ex ){...}

When an exception is caught or thrown again, an empty throw is thrown. This is a good way to protect abnormal call stack information.

 

Example:

 
// Good: Try {... // do some reading with the file} catch {file. position = position; // unwind on failure throw; // rethrow} // bad: Try {... // do some reading with the file} catch (exception ex) {file. position = position; // unwind on failure throw ex; // rethrow}

Finally, the exceptional parade ceremony:

 

Exception: The base class of all Exception objects.
Systemexception: The base class of all errors generated during the runtime.
Indexoutofrangeexception: It is triggered when the subscript of an array exceeds the range.
Nullreferenceexception: triggered when an empty object is referenced.
Invalidoperationexception: when a call to a method is invalid for the current state of the object, it is triggered by some methods.

Argumentexception: The base class for all parameter exceptions.
Argumentnullexception: triggered by a method when the parameter is null (not allowed.
Argumentoutofrangeexception: When a parameter is not within a given range, it is triggered by a method.
Interopexception: The base class of an exception that occurs in or outside the CLR environment.
Comexception: an exception that contains the hresult information of the COM class.
Sehexception: an exception that encapsulates Win32 structure exception handling information.
Sqlexception: encapsulates SQL operation exceptions.

Common exception objects:

argumentnullexception an empty parameter is passed to the method, this method cannot accept this parameter
argumentoutofrangeexception parameter value exceeds the range
arithmeticexception causes Arithmetic overflow or underflow
arraytypemismatchexception attempts to store an error type object in the array
badimageformatexception incorrect graphic format
dividebyzeroexception except zero exception
dllnotfoundexception cannot find the referenced DLL
formatexception parameter format error
indexoutofrangeexception array index out of range
invalidcastexception used invalid class
invalidoperationexception method call time error
the method called by notsupportedexception is not implemented in the class
nullreferenceexception attempts to use an unallocated reference
outofmemoryexception memory insufficient space
stackoverflowexception Stack Overflow

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.