-5

It seems to me that this code:

public static bool InsertInventoryItem(DuckbillUtils.InventoryItem invItem)
{
    bool addSuccess = true;
    try
    {
        InventoryItemsList invItems = new InventoryItemsList();
        invItems.inventoryItems.Add(invItem);
    }
    catch (Exception)
    {
        addSuccess = false;
    }
    return addSuccess;
}

...should not compile, because there's no guarantee that the return line will be reached - if there's an exception, addSuccess is assigned to, but won't the method return from within the catch block, and the final line not be reached, in which case nothing is returned from the method?

B. Clay Shannon-B. Crow Raven
  • 8,547
  • 144
  • 472
  • 862
  • 11
    Why would the method return from within the catch block? There's no return statement there. The method will always return `addSuccess`. – dcastro Dec 05 '13 at 16:02
  • 2
    You seem to think that a try-catch means the end of the method. It doesn't, it just continues at the end of the catch. – Jeroen Vannevel Dec 05 '13 at 16:03
  • 1
    Put a breakpoint in the method. I dare you to reproduce a situation where you don't hit the return statement :). – Erik Kerber Dec 05 '13 at 16:04
  • 1
    A `catch` is not am implicit return statetement. I assume you're confusing it with an uncaught exception. – Tim Schmelter Dec 05 '13 at 16:05
  • 2
    @ErikKerber: Ok, a reset or power failure :) What do I win? – some Dec 05 '13 at 16:05
  • 1
    An exception, such as a `ThreadAbortException`, could happen when control is in the method but *outside* the `try` block. Then the exception would bubble up, and the `return` statement would not be reached. But that is the usual behavior of exceptions. – Jeppe Stig Nielsen Dec 05 '13 at 16:08
  • 1
    @ErikKerber: Can I change the method body? `return InsertInventoryItem(invItem);` will cause a StackOverflowException and not hit the breakpoint. – Austin Salonen Dec 05 '13 at 16:08
  • 1
    @AustinSalonen I also challenge *you* to try the situation you described :) – Erik Kerber Dec 05 '13 at 16:25
  • 1
    @ErikKerber: I did (that's why I posted it); you cannot catch StackOverflowExceptions. – Austin Salonen Dec 05 '13 at 16:28
  • 2
    @AustinSalonen Alright, we both win. It works on Xamarin.iOS (which I had in front of me). http://stackoverflow.com/questions/1599219/c-sharp-catch-a-stack-overflow-exception – Erik Kerber Dec 05 '13 at 16:29
  • @AustinSalonen http://imgur.com/rLteUAH – Erik Kerber Dec 05 '13 at 16:31
  • 1
    There are three possibilities. The method returns normally, the method throws, or the method never returns. The language only requires that a return value be assigned in the first case. If its not clear to you why that is, think about it until it is clear. – Eric Lippert Mar 21 '14 at 14:15

3 Answers3

7

The code will catch the (all) exception, set addSuccess to false, then proceed to the return line. If no exceptions are thrown, the return line will return true. And addSuccess is assigned a value at the first line, it will never be unassigned.

Francis Ducharme
  • 4,848
  • 6
  • 43
  • 81
4

The return will always be hit. You are swallowing the error.

If you were to throw the exception, then the return would not be hit. Even then though, it's expected behavior.

public static bool InsertInventoryItem(DuckbillUtils.InventoryItem invItem)
{
    bool addSuccess = true;
    try
    {
        InventoryItemsList invItems = new InventoryItemsList();
        invItems.inventoryItems.Add(invItem);
    }
    catch (Exception)
    {
        throw;
    }
    return addSuccess;
}
Chuck Conway
  • 16,287
  • 11
  • 58
  • 101
2

because there's no guarantee that the return line will be reached

In the code provided that does not look like this.

try/catch will catch all (possible to catch) exception in the code.

Tigran
  • 61,654
  • 8
  • 86
  • 123