-1

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?

Sean
  • 60,939
  • 11
  • 97
  • 136
Anjitha
  • 69
  • 1
  • 1
  • 7
  • 6
    It sounds like your problem isn't really to do with try-catch blocks, but that you want to return from a method then re-enter back into it. – Ben Aaronson May 27 '16 at 12:35
  • 1
    Can you show the code as you'd like it to be? There's no `finally` block in your example, so it's hard to be 100% sure what the issue is. – Sean May 27 '16 at 12:38

5 Answers5

1

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;
}
Jeroen van Langen
  • 21,446
  • 3
  • 42
  • 57
0

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.

Souvik Ghosh
  • 4,456
  • 13
  • 56
  • 78
0
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

Jeroen van Langen
  • 21,446
  • 3
  • 42
  • 57
Veeresh123
  • 87
  • 16
0

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"});
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
0

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.

PeskyToaster
  • 283
  • 6
  • 16