[c#] C# try catch continue execution

I have a question that might seem fairly simple (of course if you know the answer).

A certain function I have calls another function but I want to continue execution from the caller even though the callee has thrown an exception. Let me give you an example:

something function1()
{
    try
    {
        //some code
        int idNumber = function2();
        //other code that need to execute even if function2 fails
        return something;
    }
    catch(Exception e)
    {//... perhaps something here}
}

EDIT: function1 also has a return statement so nothing can in fact crash on the way

In function2 I need to do stuff but I only need to log if anything fails, example:

int function2()
{
    try
    {
        //dostuff
    }
    catch(Exception e)
    {
        //Log stuff to db
    }
}

ok, now my question is, what should I do if I wanted to continue execution in function1 even if function 2 throws an error?

Sometimes I mix up if I should do throw; or throw e; or throw nothing at all (leave catch block empty)

This question is related to c# try-catch

The answer is


Why cant you use the finally block?

Like

try {

} catch (Exception e) {

  // THIS WILL EXECUTE IF THERE IS AN EXCEPTION IS THROWN IN THE TRY BLOCK

} finally { 

 // THIS WILL EXECUTE IRRESPECTIVE OF WHETHER AN EXCEPTION IS THROWN WITHIN THE TRY CATCH OR NOT

}

EDIT after question amended:

You can do:

int? returnFromFunction2 = null;
    try {
        returnFromFunction2 = function2();
        return returnFromFunction2.value;
        } catch (Exception e) {

          // THIS WILL EXECUTE IF THERE IS AN EXCEPTION IS THROWN IN THE TRY BLOCK

        } finally { 

        if (returnFromFunction2.HasValue) { // do something with value }

         // THIS WILL EXECUTE IRRESPECTIVE OF WHETHER AN EXCEPTION IS THROWN WITHIN THE TRY CATCH OR NOT

        }

Or you can encapsulate the looping logic itself in a try catch e.g.

for(int i = function2(); i < 100 /*where 100 is the end or another function call to get the end*/; i = function2()){

    try{
     //ToDo
    }
    catch { continue; }    

}

Or...

try{ 
    for(int i = function2(); ; ;) {
        try { i = function2(); return; } 
        finally { /*decide to break or not :P*/continue; } }
} catch { /*failed on first try*/ } finally{ /*afterwardz*/ }

Do you mean you want to execute code in function1 regardless of whether function2 threw an exception or not? Have you looked at the finally-block? http://msdn.microsoft.com/en-us/library/zwc8s4fz.aspx


just do this

    try
    {
        //some code
     try
     {
          int idNumber = function2();

     }
     finally
     {
       do stuff here....
     }
    }
    catch(Exception e)
    {//... perhaps something here}

For all intents and purposes the finally block will always execute. Now there are a couple of exceptions where it won't actually execute: task killing the program, and there is a fast fail security exception which kills the application instantly. Other than that, an exception will be thrown in function 2, the finally block will execute the needed code and then catch the exception in the outer catch block.


In your second function remove the e variable in the catch block then add throw.

This will carry over the generated exception the the final function and output it.

Its very common when you dont want your business logic code to throw exception but your UI.