47

I have a function that generate an exception. For example the following code:

void test()
{
    ifstream test("c:/afile.txt");
    if(!test)
    { 
         throw exception("can not open file");
    }
    // other section of code that reads from file.
}

Do I need a return after throwing the exception?

What is the case in c#?

4
  • 5
    it's a void function so u don't need to return any value.
    – Vond Ritz
    May 31, 2013 at 9:40
  • 1
    No, you don't need any return
    – Andy Prowl
    May 31, 2013 at 9:40
  • 2
    After throwing an exception, you do not need to return because throw returns for you.
    – Patashu
    May 31, 2013 at 9:41
  • 4
    Throwing will bubble up the call stack to the next exception handler so returning is not required. May 31, 2013 at 9:42

5 Answers 5

65

throw usually causes the function to terminate immediately, so you even if you do put any code after it (inside the same block), it won't execute. This goes for both C++ and C#. However, if you throw an exception inside a try block and the exception gets caught, execution will continue in the appropriate catch block, and if there is a finally block (C# only), it will be executed whether an exception is thrown or not. At any rate, any code immediately after the throw will never be executed.

(Note that having a throw directly inside a try/catch is usually a design problem - exceptions are designed for bubbling errors up across functions, not for error handling within a function.)

5
  • Well, technically, you can have something there, but it will never execute. May 31, 2013 at 9:41
  • @Angew: Fair enough; I reformulated my answer. :-) May 31, 2013 at 9:45
  • 1
    Have a +1 :-) It might also be worth mentioning there's no finally in C++. May 31, 2013 at 9:47
  • however, microsoft made some ms-specific equivalents to account for the finally missing thing, i think it's __finally, but there doesnt seem to be one for gcc Jun 5, 2014 at 12:48
  • @osirisgothra: Apparently so, but it seems to be unrelated to regular C++ exceptions (and unportable, as you point out). Jun 5, 2014 at 14:18
3

No, you don't need to return, because after the exception is thrown the code after that wont' be executed.

3

Strictly speaking throwing will NOT necessarily terminate the function immediately always.... as in this case,

try {

     throw new ApplicationException();


} catch (ApplicationException ex) {
    // if not re-thrown, function will continue as normal after the try/catch block

} catch (Exception ex) {

}

and then there is the Finally block - but after that it will exit.

So no, you do not have to return.

0

After you call throw the method will return immediately and no code following it will be executed. This is also true if any exceptions are thrown and not caught in a try / catch block.

0

If it's a void method you will never need a return instruction.

Then you can't put anything after the throw instruction it will never be used if something is throw

void test()
{
    ifstream test("c:/afile.txt");
    if(!test)
    { 
         throw exception("can not open file");
         // If there is code here it will never be reach !
    }
    // other section of code that reads from file.
    //if you place code here it will be reach only if you don"t throw an exception, so only if test == true in your case
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.