0

I gone through this question and I know about standard exception objects. My question is, What is the benefit of using exception over simple function calling ?

For Example Code A

#include <stdexcept>

int compare( int a, int b ) {
    if ( a < 0 || b < 0 ) 
        throw std::invalid_argument( "received negative value" );
    else
        // return normal results
}

try {
    compare( -1, 3 );
}
catch( const std::invalid_argument& e ) {
    // do stuff with exception... 
}

and Code B

int compare( int a, int b ) {
    if ( a < 0 || b < 0 ) 
        throwFunc( "received negative value" );
    else {
        // Perform normal task
     }
}


compare( -1, 3 );

throwFunc(const std::string& e ) {
    // do stuff with exception... 
}

What is the benefit of Code A over Code B ? and benefit of standard exception objects ? Ultimately we just throw a variable or string.

TonyParker
  • 2,024
  • 5
  • 18
  • 27
  • With exceptions, the call-stack will be properly unwound and constructed objects properly destructed. And with exception objects it's easier to categorize different kinds of exceptions (like e.g. `std::invalid_argument` versus `std::logic_error` etc.). – Some programmer dude Jan 31 '19 at 13:05
  • 4
    In code B, what would you return in `compare`, what would you do after (erroneous) `compare` call ? – Jarod42 Jan 31 '19 at 13:06
  • I feel like in order to answer this, we need to know what `throwFunc` actually does. If it accesses some global singleton or something the answer may be different ... – Fantastic Mr Fox Jan 31 '19 at 13:06
  • 2
    C++ Core Guidelines [E.3: Use exceptions for error handling only](http://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#e3-use-exceptions-for-error-handling-only) – Robert Andrzejuk Jan 31 '19 at 13:13
  • typically you catch the exception that someone else was throwing at you or vice versa, that is something you cannot get from using `if-else` at least not in a clean way – 463035818_is_not_an_ai Jan 31 '19 at 13:18
  • I don't think this is duplicate, or at least not the duplicate of that particular question. This question is not about return codes vs exceptions at all. – Petr Jan 31 '19 at 13:36
  • For me the question is "What is the benefit of using exception over simple function calling ?". The duplicate seems a good match to me. – Bathsheba Jan 31 '19 at 13:38

2 Answers2

0

Apparently, you think that processing the exception means only some work with the exception object itself. This is indeed true if, for example, all you want to do with the exception is to log it and then terminate the program. In this case your approach with throwFunc can work as well.

But the main thing about exceptions is that you need to catch them exactly in the right place up the call stack, process then and continue executing your program. The compare function often does not have enough information on what to do with the error, so, when you throw the exception, the stack is being unwind and the program execution jumps to the nearest catch, which should be exactly the place where you can process the error.

With your trowFunc you have no way to jump up the stack to find such a place. You can call the functon, do something there, but then you return to your compare function, which still does not know what to do with the problem. And its caller may as well not know what to do there. And so on.

Petr
  • 9,812
  • 1
  • 28
  • 52
-2

if/else conditional statements have nothing to do with throw or try/catch blocks that safely handles exceptions.

try/catch:

You can throw an exception if you expect your code block/section of having any possible error/undefined/exceptional cases. Without interrupting your program control flow by abrupt errors and aborts, you safely execute your exception-possible code segment inside try block, raise an exception by throw if you already know about the characteristic of the exception that you are expecting, and then execute a catch block corresponding to exception by handling that exception. This way your program doesn't change its control flow by making abrupt jumps.

enter image description here

Whenever there is an exception, program control flow changes because the process will call an indirect procedure related to the exception, through a jump table called exception table, to the operating system subroutine called exception handler.

When the exception handler finishes processing, one of three things happens,depending on the type of event that caused the exception:

1.The handler returns control to the current instruction Icurr, the instructionthat was executing when the event occurred.

2.The handler returns control to Inext, the instruction that would have executednext had the exception not occurred.

3.The handler aborts the interrupted program.

if/else:

if/else are conditional statements. if checks for a condition to execute a code segment. else can extend if by executing a code segment in case if condition is false.

Solution Specific To Your Example:

int compare( int a, int b ) { 
if ( a < 0 || b < 0 )
 throwFunc( "received negative value" ); else {
 // Perform normal task
 } 

} 
compare( -1, 3 ); 

throwFunc(const std::string& e ) { 
// do stuff with exception... 
}

There is no guarantee that compare (-1,3); will execute or anything after confronting an unsafely handled code section would execute.

Elijah Dayan
  • 442
  • 6
  • 19