C++ - 'try, throw, catch' compare to regular C-style 'if' - advantages?

(I have long gap to communicate with C++ and do not recall if I have used 'try-catch' at all; so, looking for advice...)

I am trying to understand what the benefits of using that C++ error handling style compare to regular C-style 'if-then'?

Still in the try{} block need to do some 'if()' to be in error condition and 'thtow()' it;
Still need to have separate block to act on defined error condition - so, the same as in block of 'if (error) {.... }'

Only additional funny syntax, blocks and lines of code...

Am I do not understand something or just do not see some benefits?

What the advantage?

Can anybody tell me that in short?
I would accept any: processing timing/simplifying; readability; source organizing; documenting; modularity, .... what else?
I do not see any!

Could you advise some?

Well, some would say it makes for a cleaner interface. Compare it to Java, which forces you to use it by often having nothing but throw/catch for errors. (For sockets, for example.) It can condense a long list of

if(statement fails) { do something ; go somewhere };
else if(another statement fails) { do something else ; go somewhere else }
else if(yet another statement fails) { do yet something else;  go to handler }
...

into its raw fundamentals of

try {
        some statement;
        another statement;
        yet another statement;
}
catch {
...
}

It also lets you defer errors, so something else besides your code can catch them. And it gives your code a way to describe all known errors, not just the ones you have a handy return code for. It's harder to paint yourself into a corner.

I'm just playing devil's advocate though... At best it converts if(statement) else if(statement2) into try { statement } catch { ... } try {statement2 } catch { ... } which is actually messier... At worst, try/catch amounts to a blind, targetless goto carrying a blind, typeless error code; as bad as or worse than the worst excesses of the old-fashioned spaghetti programming C++ is supposedly designed to avoid.

Thanks, Corona688, for reply and sharing your thoughts on that matter!
The point about following the Java style is reasonable (while, actually, not big deal.)
'Cleaner' ?, hmm, hard to be agree, but it is not a point to discuss: just personal opinion.
Others points I see in your review are: propagate up to where it will be decided to process.
That is, definitely, benefits: in C it could be done by special additional coding that is not pleasant to write and not nice usually.

The benefit of catching everything, even dot defined error, also is something: not handled error will be processed by system, but C error processing have no mechanism to 'prepare' any how to getting out of program.
Sure, it is useful.

And, finally, I have realized some 'coding layout' benefits:

  • initial 'strait forward' C-error handling assume checking for an error and processing it in place where it could occur.
  • the C++ style by that mechanizm is offering the syntax that provides the chance to move the error handling activity out of main business logic ( like in C having a separate function to check of any error condition where all possible errors would be defined, checked and processed when heppened.)

Sure, all those make sense to use it!

Appreciate your input and chance to realize all that!

THANKS!

In theory, definitely. In practice, moving it out from your main logic means your main logic can't recover from errors -- just throw an error elsewhere and die. For routine things like a failed connect(), this is really awkward... If you want to actually handle errors gracefully, try/catch ends up just being a wordier, messier replacement for if/else.

Yes, you are right! (Did not guess it earlier!)
That is in PL-SQL exist a way to return in processing after error handling, but not in this C++ possibility!

But, still, chance to do some preparations before dieing is useful, I guess.