简体   繁体   中英

continue execution after try catch block

I want to show the "Successfully saved" message and then want to continue with next try-catch block. I tried 'finally' but it says 'control cannot leave the body of finally block'. Following is my code.

try
{
    //some code

    return ok(new{Message="Successfully saved"});

    try
    {
        //some code
        //return ok(new{Message="Successfully created site"});
    }
    catch(Exception ex)
    {
        //return ok(new {Message="failed to create site"});
    }
}
catch(Exception ex)
{
//return ok(new {Message="failed to save"});
}

Can anyone please help me?

Why not store the result into a variable first?

private WhatEverType MyMethod()
{
    WhatEverType result = default(WhatEverType);
    try
    {
        //some code

        result = ok(new{Message="Successfully saved"});

        try
        {
            //some code
            result = ok(new{Message="Successfully created site"});
        }
        catch(Exception ex)
        {
            result = ok(new {Message="failed to create site"});
        }
    }
    catch(Exception ex)
    {
        result = ok(new {Message="failed to save"});
    }
    return result;
}

You are returning from the first try block so your code will not execute further down other try-catch blocks. I would recommend store/append the return message values in a string (instead of returning there itself) and finally display what was successful (or errors) at the end in finally block.

public return-type method()
{
    var-type varResult;
    var-type varResult1;

    try
    {
        // code
        varResult = successfully saved

        try
        {
            //code
            varResult = unsuccessfully saved
        }
        catch(Exception ex)
        {
            varResult = successfully saved
        }
    }
    catch(Exception ex)
    {
        result = varResult = unsuccessfully saved
    }
    finally
    {
        varResult1 = success
    }

    return varResult1
}

Here varResult return according to flow of the code it depending code entering in try or catch block

But varResult1 return success irrespective code entering in try or catch block

Why not return but in the very end :

try
{
    //some code

    //DONE: no return here - we're not ready to return, but want to continue

    try
    {
        // some code
        //DONE: no return here - we're not ready to return, but want to continue
    }
    catch (Exception ex) //TODO: do not catch Exception, but more specific exception
    {
        return ok(new {Message="failed to create site"});
    }
}
catch (Exception ex) //TODO: do not catch Exception, but more specific exception
{
     return ok(new {Message="failed to save"});
}

//  but here
return ok(new{Message="Successfully saved;Successfully created site"});

The return statement is what is messing you up. It's going to take you out the function you are executing and, well, return. A finally clause will always be executed after the try-catch block (generally used to clean up) but since you have a return in your try, you'll never get out of that clause in your execution. You could use a single try-catch and then just generate a message based on the exception that was caught in the catch block. For your Message, it's not super necessary because the catch block will you tell you where you went wrong depending on the exception and reaching the return would tell you everything went right.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM