Execute code if there is no catch in Try / Catch

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

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

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

finally
{
    //Code that runs always
}
+5
source share
5 answers

Add the code at the end of your block try. Obviously, you will only ever get there if there was no exception before:

try {
  // code to check

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

You should probably change your capture so that you only break the exceptions that you expect, and don't lay out anything that might include exceptions thrown into the "code you want to run if everything is ok."

+18
source

, , try , try. , try .

try
{
    // normal code

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

"succeded" , /:

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
}

"" , :

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

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

, .

, , , .

try
{
    // Code to check
    // Code that I want to run if it all OK ??  <-- here
}
catch(Exception ex)
{
    // Code here if an error
}
finally
{
    // Code that runs always
}
+5
try {
    // Code that might fail
    // Code that gets execute if nothing failed
}
catch {
    // Code getting execute on failure
}
finally {
    // Always executed
}
+1

: , - try

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

All Articles