Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run code if no catch in Try/Catch

Tags:

c#

When I use Try/Catch, is there a way just like If/Else to run code if there is no error is detected and no Catch?

try
{
    //Code to check
}
catch(Exception ex)
{
    //Code here if an error
}

//Code that I want to run if it's all OK ?? 

finally
{
    //Code that runs always
}
like image 632
3D-kreativ Avatar asked Jul 31 '12 10:07

3D-kreativ


1 Answers

Add the code at the end of your try block. Obviously you will only ever get there if there wasn't an exception before:

try {
  // code to check

  // code that you want to run if it's all ok
} catch {
  // error
} finally {
  // cleanup
}

You probably should change your catch in a way that you only catch exceptions you expect and not flat-out everything, which may include exceptions thrown in »code that you want to run if it's all ok«.

like image 94
Joey Avatar answered Oct 27 '22 00:10

Joey