Using Exceptions for escaping

This is a discussion on Using Exceptions for escaping within the C++ Programming forums, part of the General Programming Boards category; I'm making a kind of wrapper for easily using 2d arrays(not exactly). So to access an element I'm overloading the ...

  1. #1
    ... arjunajay's Avatar
    Join Date
    May 2005
    Posts
    203

    Question Using Exceptions for escaping

    I'm making a kind of wrapper for easily using 2d arrays(not exactly). So to access an element I'm overloading the '()' operator. But if the indices passed are out of bounds I'm doing this to get myself out of trouble.
    Code:
    Type& operator()(unsigned long col, unsigned long row) throw(const char*);
    It is technicaly alright because I throw an exception when something bad happened but I've read that using exceptions to jump around the code and using them too much esp for escaping from bottlenecks is bad practice.

    Can someone suggest another way by which
    1) I can make the user know that some thing bad happened, and
    2) return a phony reference when operator is used as LValue?
    And is this habit a bad practice that should be discontinued?
    Last edited by arjunajay; 09-07-2006 at 10:34 AM. Reason: =========

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,833
    Exceptions are - well exceptional.
    Your array class would only normally be expected to fail in a couple of cases.
    - The user had created so much data as to result in an out of memory situation
    - You as a programmer had messed up the code in some way to cause invalid indices to be used.

    The latter one should be fixed by you before letting the users loose on the code.
    The former one can be handled by some global catch-all in the main application, which basically offers to save the work so far (for example).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    Exceptions in the case of bad indices are not a bad idea. For example, the vector class uses them when you access with at(). Another option is to call it "undefined behavior" and just let whatever bad stuff happens happen when bad indices are passed. Or if you want to be nice (or mean, depending on your point of view), you could just return some default constructed value. This would be nice in that the program wouldn't crash, but mean in that it might hide the bug for a long time and cause difficult to notice data corruption.

    Also, if you just need this to use and you aren't creating it for a learning exercise, there are existing options that are probably more appropriate than trying to write your own. A vector of vectors or maybe a boost multiarray are a couple of ideas.

  4. #4
    ... arjunajay's Avatar
    Join Date
    May 2005
    Posts
    203

    Wink

    Quote Originally Posted by Daved
    Also, if you just need this to use and you aren't creating it for a learning exercise, there are existing options that are probably more appropriate than trying to write your own. A vector of vectors or maybe a boost multiarray are a couple of ideas.
    Well, It's not an Actual 2d array class. In c++ (or any other prog lang) we have to use integral indices. But my program passes double values say in the range [ 0 ... 0.1 ][ -1.0 ... 1.0] So my array wrapper just converts it to [ 0 ... X][ 0 ... Y].

    - You as a programmer had messed up the code in some way to cause invalid indices to be used.
    The double values are got from an iterated function and on each iteration the x and y values jump around (seemingly randomly). So I have no 'real' control over the 'user'. and It would be difficult to hardcode the conversion operation every time I reuse or tweak the program.
    If I make a class all i have to do is include the source file and use this function instead of the sbscript operator
    Code:
    Type& map(real x, real y) throw(const char*);
    P.S. It is an Out of Bounds exception not out of memory.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    In c++ (or any other prog lang) we have to use integral indices.
    You know that if you use a map you can use anything for the indices, like doubles, strings, other maps, etc? . . . .
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Quote Originally Posted by arjunajay
    Can someone suggest another way by which
    1) I can make the user know that some thing bad happened, and
    2) return a phony reference when operator is used as LValue?
    And is this habit a bad practice that should be discontinued?
    It would be much more appropriate to use an assert here, rather than throwing an exception. If the situation ever occurs, it is always a bug.
    Exceptions might get caught and ignored. asserts won't. They also allow you to examine the call stack at the point the problem occurred and actually solve the bug. Not to mention, they don't hurt your release build performance.

  7. #7
    ... arjunajay's Avatar
    Join Date
    May 2005
    Posts
    203
    As I said The fractal generating equations are (seemingly)unpredictable. They spit out numbers in a particular area, and, even if I'm plotting only a small part of that area the equations will and should return all the points that they normally do in the plane( or space). So I doubt whether aserts will be useful. But I'll try that any way. thanks

    I cannot use maps because there are infinitly many points that can be mapped to the same location in memory.
    eg when mapping [0.0 ... 1.0] to [0 ... 10], every thing between 0.0 to 0.1 are mapped to the first mem location in the array.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Are Exceptions effective?
    By Petike in forum C++ Programming
    Replies: 5
    Last Post: 09-13-2008, 01:23 AM
  2. Intercepting Fortran exceptions
    By MarkZWEERS in forum C++ Programming
    Replies: 2
    Last Post: 08-06-2008, 10:13 AM
  3. Debug --> Exceptions in Visual Studio 2005
    By George2 in forum C# Programming
    Replies: 1
    Last Post: 08-10-2007, 03:12 AM
  4. Need advice: catch exceptions or call methods to check bits?
    By registering in forum C++ Programming
    Replies: 1
    Last Post: 10-03-2003, 02:49 PM
  5. Throwing exceptions with constructors
    By nickname_changed in forum C++ Programming
    Replies: 14
    Last Post: 07-08-2003, 10:21 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21