Sponsored Content
Top Forums Programming C++ - 'try, throw, catch' compare to regular C-style 'if' - advantages? Post 302901600 by Corona688 on Wednesday 14th of May 2014 01:01:38 PM
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.
 

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
ExtUtils::XSpp::Exception(3pm)				User Contributed Perl Documentation			    ExtUtils::XSpp::Exception(3pm)

NAME
ExtUtils::XSpp::Exception - Map C++ exceptions to Perl exceptions DESCRIPTION
This class is both the base class for the different exception handling mechanisms and the container for the global set of exception mappings from C++ exceptions (indicated by a C++ data type to catch) to Perl exceptions. The Perl exceptions are implemented via "croak()". The basic idea is that you can declare the C++ exception types that you want to handle and how you plan to do so by using the %exception directive in your XS++ (or better yet, in the XS++ typemap): // OutOfBoundsException would have been declared // elsewhere as: // // class OutOfBoundsException : public std::exception { // public: // OutOfBoundsException() {} // virtual const char* what() const throw() { // return "You accessed me out of bounds, fool!"; // } // } %exception{outOfBounds}{OutOfBoundsException}{stdmessage}; If you know a function or method may throw "MyOutOfBoundsException"s, you can annotate the declaration in your XS++ as follows: double get_from_array(unsigned int index) %catch{outOfBounds}; When "get_from_array" now throws an "OutOfBoundsException", the user gets a Perl croak with the message "Caught exception of type 'OutOfBoundsException': You accessed me out of bounds, fool!". There may be any number of %catch directives per method. Note: Why do we assign another name ("outOfBounds") to the existing "OutOfBoundsException"? Because you may need to catch exceptions of the same C++ type with different handlers for different methods. You can, in principle, re-use the C++ exception class name for the exception map name, but that may be confusing to posterity. Instead of adding %catch to methods, you may also specify exceptions that you wish to handle for all methods of a class: class Foo %catch{SomeException,AnotherException} { ... }; The %catch{Foo,Bar,...} syntax is shorthand for "%catch{Foo} %catch{Bar} ...". If there are exceptions to be caught both from the class and attached to a method directly, the exceptions that are attached to the method only will be handled first. No single type of exceptions will be handled more than once, therefore it is safe to use this precedence to re-order the class-global exception handling for a single method. If there are no %catch decorators on a method, exceptions derived from "std::exception" will be caught with a generic "stdmessage" handler such as above. Even if there are %catch clauses for the given method, all otherwise uncaught exceptions will be caught with a generic error message for safety. Exception handlers There are different cases of Perl exceptions that are implemented as sub-classes of "ExtUtils::XSpp::Exception": ExtUtils::XSpp::Exception::simple implements the most general case of simply throwing a generic error message that includes the name of the C++ exception type. ExtUtils::XSpp::Exception::stdmessage handles C++ exceptions that are derived from "std::exception" and which provide a "char* what()" method that will provide an error message. The Perl-level error message will include the C++ exception type name and the exception's "what()" message. ExtUtils::XSpp::Exception::code allows the user to supply custom C/C++/XS code that will be included in the exception handler verbatim. The code has access to the exception object as the variable "e". Your user supplied code is expected to propagate the exception to Perl by calling croak(). ExtUtils::XSpp::Exception::object maps C++ exceptions to throwing an instance of some Perl exception class. Syntax: %exception{myClassyException}{CppException}{object}{PerlClass}; Currently, this means just calling "PerlClass->new()" and then die()ing with that object in $@. There is no good way to pass information from the C++ exception object to the Perl object. Will change in future. ExtUtils::XSpp::Exception::unknown is the default exception handler that is added to the list of handlers automatically during code generation. It simply throws an entirely unspecific error and catches the type "..." (meaning anything). There is a special exception handler "nothing" which is always available: int foo() %catch{nothing}; It indicates that the given method (or function) is to handle no exceptions. It squishes any exception handlers that might otherwise be inherited from the method's class. METHODS
new Creates a new "ExtUtils::XSpp::Exception". Calls the "$self->init(@_)" method after construction. "init()" must be overridden in subclasses. handler_code Unimplemented in this base class, but must be implemented in all actual exception classes. Generates the "catch(){}" block of code for inclusion in the XS output. First (optional) argument is an integer indicating the number of spaces to use for the first indentation level. indent_code Given a piece of code and a number of spaces to use for global indentation, indents the code and returns it. cpp_type Fetches the C++ type of the exception from the "type" attribute and returns it. ACCESSORS
name Returns the name of the exception. This is the "myException" in %exception{myException}{char*}{handler}. type Returns the ExtUtils::XSpp::Node::Type C++ type that is used for this exception. This is the "char*" in %exception{myException}{char*}{handler}. CLASS METHODS
add_exception Given an "ExtUtils::XSpp::Exception" object, adds this object to the global registry, potentially overwriting an exception map of the same name that was in effect before. get_exception_for_name Given the XS++ name of the exception map, fetches the corresponding "ExtUtils::XSpp::Exception" object from the global registry and returns it. Croaks on error. perl v5.14.2 2011-12-20 ExtUtils::XSpp::Exception(3pm)
All times are GMT -4. The time now is 12:46 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy