1

I have seen many exception handling mechanisms where they simply weren't necessary. A lot of the times the problem could have been solved in a much cleaner way using simple if statements.

For example, things like:

  • Invalid input
  • Division by zero
  • Wrong type
  • Container range check
  • Null pointer
  • Uninitialized data

... and so on.

Could someone provide an example where it would be a better approach to handle exceptions?

Oleksiy
  • 37,477
  • 22
  • 74
  • 122
  • Memory allocation failure. – avakar Aug 26 '13 at 11:09
  • @avakar `bad_alloc` is already an exception, we have no choice but to try to catch it. I'm talking about user-defined code - when would it be *necessary*? – Oleksiy Aug 26 '13 at 11:11
  • Embedded programming if you detect issues with your hardware. – Sambuca Aug 26 '13 at 11:16
  • possible duplicate of [When and how should I use exception handling?](http://stackoverflow.com/questions/4506369/when-and-how-should-i-use-exception-handling) – juanchopanza Aug 26 '13 at 11:17
  • @Sambuca Could you explain please? I don't understand. – Oleksiy Aug 26 '13 at 11:17
  • 1
    Most of those don't usually give exceptions in C++. Division by zero, null pointer and uninitialised data give undefined behaviour, type checking is (nearly always) static, and standard containers give you the choice between checked and unchecked access. That only leaves "invalid input", and it's entirely up to you how to deal with that. – Mike Seymour Aug 26 '13 at 11:18
  • 1
    For example if we detect problems with our voltage supplies, sensors or actuators. Whatever possibly leads to undefined behaviour of our system. – Sambuca Aug 26 '13 at 11:20
  • 2
    The simple answer is "when the error can't be handled locally"; a full answer would take up a chapter or two of a book. – Mike Seymour Aug 26 '13 at 11:21
  • I would use exception when I want to catch error's not in the same function'A' scope but rather in some other function'B' which has dependecy with function 'A'. But both functions 'A' and 'B' are present in the same program or binary scope. Please refer to stack unwinding principle. – Santosh Sahu Aug 26 '13 at 11:33
  • How do you return an error code from a constructor? ;) – syam Aug 26 '13 at 11:44
  • syam : - I would use exception handling mechanism in constructor. There are many questions already asked for exception handling in constructor – Santosh Sahu Aug 27 '13 at 09:06

3 Answers3

1

Exceptions become more important as your program size grows.

With a simple application return codes are probably fine. But when an error condition needs to bubble up a couple levels of the stack before being handled, it starts to make sense to use exceptions instead of passing error codes from every function.

Also, when a method already returns a value, it may not be practical or possible to return an error code from the function as well.

MrSlippers
  • 459
  • 1
  • 6
  • 14
  • Although accepted as "the answer" by @Oleksiy I don't think this answers the questions. When searching a collection for a given element, throwing an exception if the element is not found is not suitable in most cases. A return code or flag stating "not found" would be more suitable in most cases. As a rule of thumb I'd not use exceptions if the result is a) not really exceptional but quite common and/or b) the failure case can be handled locally. – Onur Aug 26 '13 at 12:29
0

Sometimes using exception is cleaner. In function foo thanks to exception throwing in check function you can avoid checking what check returns what makes this function simpler:

#include<iostream>
using namespace std;

class A{
public:
    int a_foo(int index){return _tab[index];}    
private:
    static int _tab[3];
};
int A::_tab[3]={1,2,3};

void check(int index)
{
    if (index < 0 || index > 2)  
            throw string("invalid index");  
}

void foo(A a, int index){
    check(index);
    cout << a.a_foo(index) << endl;
}

int main()
{
    try 
    {   
        A a;
        foo(a,4);    
    }   
    catch(string ex) 
    {   
        cerr << ex <<'\n';
    }   
}
cpp
  • 3,743
  • 3
  • 24
  • 38
0

I would use it when the consequence of some unexpected 'event' is inability for application to continue running. When it happens and you catch it, report it nicely and close the application (or whatever you think you should do in that case).

user1764961
  • 673
  • 7
  • 21