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


 
Thread Tools Search this Thread
Top Forums Programming C++ - 'try, throw, catch' compare to regular C-style 'if' - advantages?
# 1  
Old 05-13-2014
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?
# 2  
Old 05-13-2014
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
Code:
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
Code:
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.
# 3  
Old 05-14-2014
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!
# 4  
Old 05-14-2014
Quote:
Originally Posted by alex_5161
- 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.)
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.
# 5  
Old 05-14-2014
Quote:
Originally Posted by Corona688
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Compare files with regular expression

Readers, Reading a previous post about comparing files using awk ('awk-compare-2-columns-2-files-output-whole-line', https://www.unix.com/shell-programming-scripting/168432-awk-compare-2-columns-2-files-output-whole-line.html), it is possible to adjust this, so that regular expression can be used... (8 Replies)
Discussion started by: linuxr
8 Replies

2. UNIX for Dummies Questions & Answers

What are some advantages of Unix?

What are some advantage's of Unix (3 Replies)
Discussion started by: alvin2132
3 Replies

3. Shell Programming and Scripting

How to compare a file name with a regular expression !!

Hi, I need to compare file names in a folder with several strings(which are in regular expression format): For example: there is a file "objectMyHistoryBook" and there are several strings to compare this file name with: objectMyMaths*, objectMyEnglish*, objectMyHistory*,... (2 Replies)
Discussion started by: Lucifer_123
2 Replies

4. Shell Programming and Scripting

AWK - compare $0 to regular expression + variable

Hi, I have this script: awk -v va=45 '$0~va{print}' flo2 That returns: "4526745 1234 " (this is the only line of the file "flo2". However, I would like to get "va" to match the begining of the line, so that is "va" is different than 45 (eg. 67, 12 ...) I would not have any output. That... (3 Replies)
Discussion started by: jolecanard
3 Replies

5. Shell Programming and Scripting

compare variable against regular expression?

is it possible? if so, how? i want to check a variable whether is it a number or letter in an if-else statement (6 Replies)
Discussion started by: finalight
6 Replies

6. Shell Programming and Scripting

advantages of Perl ?

What is the advantages of Perl in Unix environnement. Is it for scripts ? Text manipulation ? Have you a Concrete exemple of perl utilisation. Thanks you (3 Replies)
Discussion started by: simquest
3 Replies

7. UNIX for Dummies Questions & Answers

Advantages of Groups

What are the advantages of putting users into groups? I understand that in a corporate environment, you should create a group for each department. ie: putting finance employees into a finance group. But are there any system advantages for doing that? How would it make it easier on the system... (3 Replies)
Discussion started by: kurtmc
3 Replies

8. UNIX for Dummies Questions & Answers

Linux advantages?

Hello to help me with my studying of unix/linux outside of work I was thinking of installing Linux at home aswell as using Windows XP. Im pretty new to Linux and Unix, could someone tell me the possible benifits or even negatives of running Linux at home as an opperating system as opposed to... (2 Replies)
Discussion started by: Loaded Gun
2 Replies
Login or Register to Ask a Question