Are all exceptions captured using the try catch statement?

Source: Internet
Author: User

During application development, you must check possible errors in the Code and handle them correctly. Ideally, each line of code in the application is executed as expected, each type of resource is always usable. However, in actual development, writing code may inevitably lead to errors, or the network is interrupted or the data service stops running.

The. NET Framework provides a structured exception handling mechanism to handle various errors in the code, that is, try catch.

Here is a small example.

Object OBJ; datetime DT; try {OBJ = new object (); dt = (datetime) OBJ;} catch (invalidcastexception e) // An error occurred while converting invalid Types {console. writeline (E. message);} catch (invalidoperationexception e) // invalid operation exception {console. writeline (E. message);} catch // other exceptions {console. writeline ("program running error! ");} Finally // code block that must be executed {dt = convert. todatetime (" 1900-01-01 "); console. writeline (Dt. tostring ());}

In the preceding example, an invalid type conversion exception is thrown when the object type is converted to the datetime type.
. The structured exception handling principle used in. NET is that structured exception handling code must be added to all possible errors, because this ensures that all resources are correctly released when errors occur, however, you cannot use it blindly, rather than adding try catch to every sentence of code. Because an exception is a resource-consuming mechanism, every time an exception is thrown, the stack of the exception is created and the exception information is loaded, which puts some burden on the program. Therefore, what can be added is not blindly added.
The above code can be simplified to be replaced by is or.
Object OBJ; datetime DT; OBJ = new object (); If (obj is datetime) {dt = (datetime) OBJ;} else {console. writeline ("type conversion is invalid! ");}

This seems to be much simpler, handling exceptions, and saving resources.

 

Are all exceptions captured using the try catch statement?

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.