Why use try and catch () in C ++?

I understand that try and catch() are used to handle exceptions if in some cases an error or failure occurs in the program. I also understand how they work. But why use try and catch() ? Why not just use the if() operator, which is looking for a specific case, and if this case is true, then it cout << //error code ?

+8
c ++ exception exception-handling try-catch
source share
5 answers

Exception Handling:

  • can be used with designers and operators who cannot return a separate error code (they can set the object to some error state, which implies further use of memory, but the client code must also remember to check this error status later)
    • for example: user defined type - class X - supports the notation x1 = x2 + x3 - where can the error code be returned?
  • can be used as a concrete data element of a class / structure built in the list of initializers - avoiding the further construction of data elements, which can then be doomed or wasteful; as a contrast, explicit error handling is possible only later in the constructor body
  • is more implicit - emphasizing a normal successful code stream that can make it more concise, readable, supported
  • factorized in different ways - exceptions from many places can be caught in one place, which sometimes makes error handling code more concise, readable, supported
  • The benefits of a consultation can actually lead to more reliable error handling in cases where error processing would otherwise be repeated.
  • usually use their own memory area, regardless of the stack used for local variables, function parameters, saving processor registers and return addresses, etc .; this means that the throw operator can reliably construct an exception object directly in this memory area, even if the remaining stack memory is smaller than the exception object (although this implementation detail is not guaranteed by the standard).
  • has a different performance profile, which can be faster in certain circumstances.
  • facilitates the distribution of richer error information from low-level code using intermediate code that you cannot "independently" or want / cannot modify to distribute C-style error codes, up to and including processing
+12
source share

try...catch does more. It unwinds the stack, which calls destructors for all automatically allocated objects since try was introduced. If you do this in your own way, you will have to track this object manually or you will get memory problems (leaks, overwriting, outdated pointers, double deletions)

+5
source share

Another reason: the code you write may also be used as part of a larger project by someone else. And since using built-in exception handling routines is standard, proponents of a large project expect you to handle your exceptions similarly, so that exception handling can be done correctly at the top levels - not to mention that using standard output as an error output is questionable practice (it can be suppressed, for example, or not used at all).

UPD: I misunderstood your question. The reason I described actually justifies manually throwing exceptions, but doesn't use try...catch blocks.

+1
source share

I am going to reply with a quote from one of my heroes, Martin Sustrika from the ZeroMQ fame, taken from a blog entry at http://www.250bpm.com/blog:4

However, which is important to avoid simple failures, it becomes a nightmare when your goal is to ensure that undefined behavior does not happen. The decoupling between raising the exception and handling it, which avoids the crashes that are so easy in C ++, makes it virtually impossible to guarantee that the program will never execute undefined behavior.

And then add, I believe that I use try / catch for very high levels of lever restart, more than anything else. Adding that my opinion really doesn't matter. I believe that choosing how and why to use exception handling is very similar to choosing green more than blue. Personal, and no contribution from others will ever change him.

-one
source share

The question is a false dichotomy . In Java and C # try - catch competes with if to clear on failure. In C ++, it is basically an RAII method (using destructors and a single-phase construct) that competes with if to clear on failure.

If it was a question of using exceptions in comparison with using if , then this would be more reasonable and relevant for ordinary C ++ programming.

-2
source share

All Articles