Ejecute el código si no hay captura en Try/Catch

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
}

preguntado el 31 de julio de 12 a las 11:07

Why not just add your code at the end of try? -

Because you may want any exceptions thrown by the rest of the code to be caught by outer exception handling, and not this inner try/catch block. In which case the best way to do this (that I know of) is to use a bool. -

5 Respuestas

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«.

Respondido 31 Jul 12, 12:07

I don't think this is a great solution especially for built-in error types. As you said "you only catch exceptions you expect". So if I expect a FooException venir de // code to check then I only include that code in the try block. I don't expect a FooException de // code that you want to run if it's all ok, but your solution would still incorrectly catch that. This is especially a problem with generic error types. However, your solution would be perfect if all functions had there own unique error types. - David Callanan

If you need it to always execute if your try code succeeds, put it at the end of your try block. It will run as long as the previous code in your try block runs without an exception.

try
{
    // normal code

    // code to run if try stuff succeeds
}
catch (...)
{
    // handler code
}
finally
{
    // finally code
}

If you need alternative exception handling for you "succeded" code, you can always nest your try/catches:

try
{
    // normal code

    try
    {
        // code to run if try stuff succeeds
    }
    catch (...)
    {
        // catch for the "succeded" code.
    }
}
catch (...)
{
    // handler code
    // exceptions from inner handler don't trigger this
}
finally
{
    // finally code
}

If your "succeeded" code has to execute after your finally, use a variable:

bool caught = false;
try
{
    // ...
}
catch (...)
{
    caught = true;
}
finally
{
    // ...
}

if(!caught)
{
    // code to run if not caught
}

Respondido 31 Jul 12, 12:07

Just place it after the code that may throw an exception.

If an exception is thrown it will not run, if one is not thrown, it will run.

try
{
    // Code to check
    // Code that I want to run if it's all OK ??  <-- here
}
catch(Exception ex)
{
    // Code here if an error
}
finally
{
    // Code that runs always
}

Respondido 31 Jul 12, 12:07

try {
    // Code that might fail
    // Code that gets execute if nothing failed
}
catch {
    // Code getting execute on failure
}
finally {
    // Always executed
}

Respondido 31 Jul 12, 12:07

I would write it like this: If the call to a method ran fine then the success is merely the last thing within the try

try 
{
    DoSomethingImportant();
    Logger.Log("Success happened!");
}
catch (Exception ex)
{
    Logger.LogBadTimes("DoSomethingImportant() failed", ex);
}
finally 
{
    Logger.Log("this always happens");
}

Respondido 31 Jul 12, 13:07

Man, SO has quick fingers. You guys should get over to London and compete - bisonte espacial

A million monkeys at a million keyboards with a million StackOverflow accounts are likely to produce the right answer fairly quickly :) - Polinomio

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.