Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Better language feature than exception handling in C++? [closed]

(not sure if it's only a C++ thing)

Exception handling is hard to learn in C++ and is certainly not a perfect solution but in most cases (other than some specific embedded software contexts) it's certainly the better solution we currently have for exception handling.

What about the future?

Are there other known ways to handle errors that are not implemented in most languages, or are only academical research?

Put another way: are there (suposedly) known better (unperfect is ok) ways to handle errors in programming languages?

like image 631
Klaim Avatar asked Sep 13 '10 15:09

Klaim


People also ask

Which is better a return code or an exception?

An application that uses exceptions is more robust than an application that uses return codes. An application that uses exceptions can also give the cleanest code, since return codes don't have to be checked after every call.

Is exception handling a good practice?

Exception Handling: It's a Good Thing. There is nothing wrong with the previous code. But overusing these patterns will cause code smells, and won't necessarily be beneficial. Likewise, misusing them can actually do a lot of harm to your code base, making it brittle, or obfuscating the cause of errors.

Should we avoid exception handling?

No you should never avoid exception handling. That's the only thing that makes your code robust.

Does C programming language supports exception handling feature?

The C programming language does not support exception handling nor error handling. It is an additional feature offered by C. In spite of the absence of this feature, there are certain ways to implement error handling in C. Generally, in case of an error, most of the functions either return a null value or -1.


1 Answers

Well, there's always been return codes, errno and the like. The general problem is that these can be ignored or forgotten about by programmers who are unaware that a particular call can fail. Exceptions are frequently ignored or missed by programmers too. The difference is that if you don't catch an exception the program dies. If you don't check a return code, the program continues on operating on invalid data. Java tried to force programmers to catch all of their exceptions by creating checked exceptions, which cause a compilation error if you don't specify exactly when they can be propagated and catch them eventually. This turned out to be insanely annoying, so programmers catch the exceptions with catch(...){/* do nothing*/} (in C++ parlence) as close to their source as possible, and the result is no better than ignoring a return code.

Besides these two error techniques, some of the functional languages support the use of various monadic return types which can encapsulate both errors and return values (e.g. Scala's Either type, Option type, or a monad that lets you return an approximate answer along with failure log). The advantage to these is that the only way to work with the successful return value is to execute code inside the monad, and the monad ensures that the code isn't run if there was a failure. (It's rather complicated to explain for someone who isn't a Haskell or Scala programmer.) I haven't worked with this model so much, but I expect it would be as annoying to some people as checked exceptions are.

Basically, IMO, error checking is a matter of attitude. You have three options:

  1. Realize you have to deal with it, accept that fact cheerfully, and take the effort to write correct error handling code. (Any of them)
  2. Use language features that force you to deal with it, and get annoyed because you don't want to deal with it, particularly when you're sure the error will never happen. (Checked Exceptions, Monads)
  3. Use langauge features that allow you to ignore it easily, and write unsafe code because you ignored it. (Unchecked Exceptions, Return Codes)
  4. Get the worst of both options 2 and 3 by using language features that force you to deal with it, but deali with every error in a way that explicitly ignores it. (Checked Exceptions, Monads)

Obviously, you should try to be a #1 type programmer.

like image 157
Ken Bloom Avatar answered Nov 03 '22 04:11

Ken Bloom