views:

504

answers:

8

In Python, there is this useful exception handling code:

try:
    # Code that could raise an exception
except Exception:
    # Exception handling
else:
    # Code to execute if the try block DID NOT fail

I think it's useful to be able to separate the code that could raise and exception from your normal code. In Python, this was possible as shown above, however I can't find anything like it in C#.

Assuming the feature or one like it doesn't exist, is it standard practice to put normal code in the try block or after the catch block?

The reason I ask is because I have the following code:

if (!IsReadOnly)
{
    T newobj;
    try
    {
        newobj = DataPortal.Update<T>(this);

        List<string> keys = new List<string>(BasicProperties.Keys);
        foreach (string key in keys)
        {
            BasicProperties[key] = newobj.BasicProperties[key];
        }
    }
    catch (DataPortalException)
    {
        // TODO: Implement DataPortal.Update<T>() recovery mechanism
    }
}

Which requires the normal code to be in the try block because otherwise if an exception was raised and subsequently handled, newobj would be unassigned, but it feels quite unnatural to have this much code in the try block which is unrelated to the DataPortalException. What to do?

Thanks

A: 

You could do something like this:

if (!IsReadOnly)
{
    T newobj = null;
    try
    {
        newobj = DataPortal.Update<T>(this);    
    }
    catch (DataPortalException)
    {
        // TODO: Implement DataPortal.Update<T>() recovery mechanism
    }
    if (newobj != null)
    {
        List<string> keys = new List<string>(BasicProperties.Keys);
        foreach (string key in keys)
        {
            BasicProperties[key] = newobj.BasicProperties[key];
        }
    }
}
Vinay Sajip
A: 

that would be the empty statement like hits

try 
{ 
    somethingThatCanThrow(); 
} 
catch(Exception ex) 
{ 
    LogException(ex); 
    return;
} 
ContinueFlow();
Henri
+5  A: 

I would prefer to see the rest of the code outside the try/catch so it is clear where the exception you are trying to catch is coming from and that you don't accidentally catch an exception that you weren't trying to catch.

I think the closest equivalent to the Python try/catch/else is to use a local boolean variable to remember whether or not an exception was thrown.

bool success;

try
{
    foo();
    success = true;
}
catch (MyException)
{
    recover();
    success = false;
}

if (success)
{
    bar();
}

But if you are doing this, I'd ask why you don't either fully recover from the exception so that you can continue as if there had been success, or else fully abort by returning an error code or even just letting the exception propagate to the caller.

Mark Byers
+1 for the 'handle the exception properly' part. If you can't recover from the exception, you should probably stop doing what you're doing immediately. If you can't get stop gracefully, refactor so you can.
kibibu
+1, but I have to ask: how is this better than putting the call to bar() right before the catch?
David Lively
@David: With the code I gave, if the `bar` function throws a MyException then it will not be caught but with your suggestion it will be caught. The method I showed more closely matches the `try: catch: else:` construct in that exceptions raised in the `else` are not caught.
Mark Byers
I use this pattern fairly often and I usually initialize `success` to `false` - saves me one line of code in the `catch` block.
Aaronaught
+1  A: 

C# does not have such a concept, so you are just left with three options,

  • put the else code inside the try.
  • put the else code outside the try catch block, use a local variable to indicate success or failure, and an if block around your else code.
  • put the else code in the finally block, use a local variable to indicate success or failure, and an if block arount you else code.
Obalix
+2  A: 

Just put your "else" block before the catch. Then, it will only execute if code execution reaches that point:

try
{
    fee();
    fi();
    foe();
    fum();

    /// put your "else" stuff here. 
    /// It will only be executed if fee-fi-foe-fum did not fail.
}
catch(Exception e)
{
    // handle exception
}

Given that, I fail to see the use of try..catch...else unless there's something vital missing from the OP's description.

David Lively
The difference is that there's 1 line that's expected to throw an exception and you're expecting that can happen and catching it. If a different line threw the exception that might be something you don't want to catch, because it may indicate an actual bug.
Davy8
@Davy8 in that case, you should either be a) catching only the specific exception type that you expect, or b) put the code that you don't expect to generate an exception **outside** of this try..catch block. In any case, you should still be catching any exceptions that occur so that you can fail gracefully.
David Lively
It's close, but it's not quite equivilent. If it's just outside the try/catch it will execute regardless of whether the first section threw an exception, but it should only execute when the first part did not throw. It is possible that the same exception type could be thrown, so merely catching a specific exception is an improvement, but isn't exactly the same.
Davy8
+2  A: 

Allow me to repeat an idea from a similar StackOverflow question. You cannot do this directly, but you can write a method that encapsulates the behavior you need. Look at the original question to see how to implement the method (if you're not familiar with lambda expressions and Func delegates). The usage could look like this:

TryExceptRaise(() => { 
    // code that can throw exception
  }, (Exception e) => { 
    // code to run in case of an exception
    return (...); 
  }, () => {
    // code to run if there is no exception
    return (...);
  });
Tomas Petricek
A: 

Barbaric solution: create an Else class derived from Exception, throw an instance of it at the end of the try block, and use catch (Else) {...} to handle the other stuff.

I feel so dirty.

Daniel Newby
I don't know what to do here - this *demands* a -1 vote, but I'm amused enough to hesitate to do so.
280Z28
A: 

This will might get downvoted but doesn't c# have goto(note I have almost no c# knowledge so I have no idea if this works).

what about something like

try 
{ 
...
} 
catch(Exception ex) 
{ 
...
goto Jump_past_tryelse
} 
...//Code to execute if the try block DID NOT fail

Jump_past_tryelse:
...
Roman A. Taycher
I almost gave you a +1 simply for offering out the whole pub with your 'goto'!
amelvin
Only almost, mind.
amelvin
"offering out the whole pub "==?
Roman A. Taycher