Question - How do I use structured exception handling

J.D. Meier, Alex Mackman, Blaine Wastell, Prashant Bansode, Andy Wigley, Kishore Gopalan

Applies to

Answer:

Use try/catch/finally structured exception handling blocks around code to avoid unhandled exceptions. Use finally blocks to execute code that runs whether an exception is trapped; this is useful for releasing resources such as closing files or disposing of objects. A structured exception handling helps you create and maintain programs with robust, comprehensive error handlers. If an exception is not handled properly it might leave your application unstable and un-usable. Also finally blocks guarantees that any resources allocated in the try block of the code will get a chance to be cleaned in the finally block which is executed at the last. Here is how you use the structured exception handling

try{
// code that might throw an exception
}
catch (knownException){
// do exception handling
}
catch{
// generic exception handler, do exception handling
}
finally{
// free any allocated resources
}

Attributes

  • Author: J.D. Meier, Alex Mackman, Blaine Wastell, Prashant Bansode, Andy Wigley, Kishore Gopalan

  • Category: Exception Management

  • filePath: ..\Libraries\patterns & practices Library\faq\c6852c86-0c93-4826-8939-8e0c93460979.xml

  • Pri: 2

  • Rule Type: Implementation

  • Source: patterns & practices Library

  • Status: Release

  • Technology: ASP.NET 2.0

  • Title: Question - How do I use structured exception handling

  • Topic: Security

  • Type: Question and Answer

  • ID: c6852c86-0c93-4826-8939-8e0c93460979