0

Possible Duplicate:
In C++ what are the benefits of using exceptions and try / catch instead of just returning an error code?

I am wondering what's the pros & cons of using Try/Catch VS returning error code ?

Should I almost always prefere using try catch in c++ ?

Do you still use return error code in your project ? If so why ?

Community
  • 1
  • 1
RoundPi
  • 5,819
  • 7
  • 49
  • 75
  • Yes, lots of questions already exist for this topic [Exception vs. error-code vs. assert](http://stackoverflow.com/questions/1388335/exception-vs-error-code-vs-assert) – crashmstr Aug 01 '11 at 14:54
  • Thanks for all the links, I am sure I did search SO for this, maybe next time I will do search via google -> SO... – RoundPi Aug 01 '11 at 15:50

2 Answers2

0

Use error codes for incorrect functionalities of your program and try catch in places where you expect the program to crash. try/catch blocks usually make your program run slower. If you suspect an error in your code, it's better to check and return an error code rather than throwing an exception.

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
  • 2
    Checking for error codes makes the program slower than not checking... – Nobody moving away from SE Aug 01 '11 at 14:54
  • 1
    This is poor general advice. Use error codes and exceptions for the purpose they were designed for. In particular, the default error handling policy (ignore by default / propagate by default) is important. Other benefits include application-wide uniqueness of error codes enforced by the type system, etc. – André Caron Aug 01 '11 at 14:55
0

try/catch has a greater overhead than interpreting a return value but offers more flexibility that return value. I generally use both, depending on 3rd party library I use.

cprogrammer
  • 5,503
  • 3
  • 36
  • 56
  • Indeed, try/catch blocks should be avoided in tight loops if performance is critical. Note however that C++ applications that use RAII extensively need very few try/catch blocks, so this "performance hit" is often at high-level and therefore negligible. – André Caron Aug 01 '11 at 15:02