.NET Tip: Exiting a Try/Catch Block

If you use a return statement within a Try/Catch block, there's a behavior you need to be aware of in your code. Consider this block, for example:

try {
   DoSomething();
   return;
}
catch (Exception ex)
{
   // Handle exception here
}

// code continues here...

Assuming no exception is generated, the return statement will fire normally and the code flow will go back to whatever routine called this block of code. Adding a Finally section to this block of code, however, can lead to confusion. Take this code, for example:

try {
   Console.WriteLine("In try block");
   return;
}
catch (Exception ex)
{
   Console.WriteLine(ex.ToString());
}
finally
{
   Console.WriteLine("In finally block");
}

Even though the return statement normally will send you back to the calling block of code, the finally block always executes. For this snippet, the output will look like this:

In try block
In finally block

This behavior is by design, but it's just something to remember if you're using finally blocks along with return statements in your exception handling.

About the Author

Eric Smith is the owner of Northstar Computer Systems, a Web-hosting company based in Indianapolis, Indiana. He is also a MCT and MCSD who has been developing with .NET since 2001. In addition, he has written or contributed to 12 books covering .NET, ASP, and Visual Basic. Send him your questions and feedback via e-mail at questions@techniquescentral.com.



Comments

  • try catch block problem

    Posted by c++_help on 11/07/2008 07:07am

    I have written following prog in c++ #include #include using namespace std; int main() { int a,b; cout<<"Enter values of a & b\n"; cin>>a; cin>>b; int x=a-b; try { if(x !=0) { cout<<"Result(a/x)="< Reply

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: November 20, 2014 @ 2:00 p.m. ET / 11:00 a.m. PT Are you wanting to target two or more platforms such as iOS, Android, and/or Windows? You are not alone. 90% of enterprises today are targeting two or more platforms. Attend this eSeminar to discover how mobile app developers can rely on one IDE to create applications across platforms and approaches (web, native, and/or hybrid), saving time, money, and effort and introducing apps to market faster. You'll learn the trade-offs for gaining long …

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds