Sponsored Content
Top Forums Programming C++ - 'try, throw, catch' compare to regular C-style 'if' - advantages? Post 302901331 by Corona688 on Tuesday 13th of May 2014 11:26:31 AM
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.
 

8 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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
Dancer::Exception::Base(3pm)				User Contributed Perl Documentation			      Dancer::Exception::Base(3pm)

NAME
Dancer::Exception::Base - the base class of all Dancer exceptions DESCRIPTION
Dancer::Exception::Base is the base class of all Dancer exception. All core exceptions, and all custom exception registered using "Dancer::Exception::register_exception" inherits of "Dancer::Exception::Base". METHODS
throw Throws an exception. It's what "raise" (from Dancer::Exception) uses. Any arguments is set as raising parameters. You should not use this method directly, but instead, use "raise" from Dancer::Exception. Warning, if you want to rethrow an exception, use "rethrow". rethrow Re-throw the exception, without touching its parameters. Useful if you've caught and exception but don't want to handle it, and want to rethrow it. try { ... } catch { my ($e) = @_; $e->does('InvalidLogin') or $e->rethrow; ... }; does Given an exception type, returns true if the exception is of the same type. try { raise InvalidLogin => 'foo'; } catch { my ($e) = @_; $e->does('InvalidLogin') # true ... }; It can receive more than one type, useful for composed exception, or checking multiple types at once. "does" performs a logical OR between them: try { raise InvalidPassword => 'foo'; } catch { my ($e) = @_; $e->does('InvalidLogin', 'InvalidPassword') # true ... }; get_composition Returns the composed types of an exception. As every exception inherits of Dancer::Exception::Base, the returned list contains at least 'Base', and the exception class name. Warning, the result is a list, so you should call this method in list context. try { raise InvalidPassword => 'foo'; } catch { my ($e) = @_; my @list = $e->get_composition() # @list contains ( 'InvalidPassword', 'Base', ... ) }; message Computes and returns the message associated to the exception. It'll apply the parameters that were set at throw time to the message pattern of the exception. STRINGIFICATION
string overloading All Dancer exceptions properly stringify. When evaluated to a string, they return their message, concatenated with their stack trace (see below). cmp overloading The "cmp" operator is also overloaded, thus all the string operations can be done on Dancer's exceptions, as they will all be based on the overloaded "cmp" operator. Dancer exceptions wil be compared without their stacktraces. STACKTRACE
Similarly to Carp, Dancer exceptions stringification appends a string stacktrace to the exception message. The stacktrace can be a short one, or a long one. Actually the implementation internally uses Carp. To enable long stack trace (for debugging purpose), you can use the global variable "Dancer::Exception::Verbose" (see below). The short and long stacktrace snippets are stored within "$self-"{_shortmess}> and "$self-"{_longmess}>. Don't touch them or rely on them, they are internals, and will change soon. GLOBAL VARIABLE
$Dancer::Exception::Verbose When set to 1, exceptions will stringify with a long stack trace. This variable is similar to $Carp::Verbose. I recommend you use it like that: local $Dancer::Exception::Verbose; $Dancer::Exception::Verbose = 1; All the Carp global variables can also be used to alter the stacktrace generation. perl v5.14.2 2012-03-31 Dancer::Exception::Base(3pm)
All times are GMT -4. The time now is 09:56 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy