243 votes

Why should 'boneheaded' exceptions not be caught, especially in server code?

Silent But Deadly When writing enterprise software, you will eventually learn an essential truth: the worst bug in the world is not one that causes your program to crash. The worst bug in the world ...
Lawnmower Man's user avatar
153 votes

Exception propagation: When should I catch exceptions?

As a general principle, don't catch exceptions unless you know what to do with them. If MethodC throws an exception, but MethodB has no useful way to handle it, then it should allow the exception to ...
Simon B's user avatar
  • 9,473
144 votes

Exception vs empty result set when the inputs are technically valid, but unsatisfiable

Return an Empty Set I would expect an empty set because: There are 0 combinations of 4 numbers from the set of 3 when i can only use each number once
Ewan's user avatar
  • 71k
137 votes

Exceptions, error codes and discriminated unions

but crashing your client's software is still not a good thing It most certainly is a good thing. You want anything that leaves the system in an undefined state to stop the system because an ...
candied_orange's user avatar
86 votes

Why is Arithmetic Overflow ignored?

There are 3 reasons for this: The cost of checking for overflows (for every single arithmetic operation) at run-time is excessive. The complexity of proving that an overflow check can be omitted at ...
Brendan's user avatar
  • 3,895
85 votes

Result object vs throwing exceptions

You have to distinguish between return values and errors. A return value is one of many possible outcomes of a computation. An error is an unexpected situation which needs to be reported to the caller....
helb's user avatar
  • 1,380
81 votes

Exception vs empty result set when the inputs are technically valid, but unsatisfiable

When in doubt, ask someone else. Your example function has a very similar one in Python: itertools.combinations. Let's see how it works: >>> import itertools >>> input = [1, 2, 3, ...
301_Moved_Permanently's user avatar
73 votes

Having a flag to indicate if we should throw errors

The problem with this approach is that while exceptions never get thrown (and thus, the application never crashes due to uncaught exceptions), the results returned are not necessarily correct, and the ...
mmathis's user avatar
  • 5,408
71 votes

Exception vs empty result set when the inputs are technically valid, but unsatisfiable

In layman's terms: If there is an error, you should raise an exception. That may involve doing things in steps instead of in a single chained call in order to know exactly where the error happened. ...
Tulains Córdova's user avatar
70 votes

Result object vs throwing exceptions

This is a good question that professional developers have to consider carefully. The guideline to follow is that exceptions are called exceptions because they are exceptional. If a condition can be ...
Eric Lippert's user avatar
  • 45.8k
65 votes

Why is Arithmetic Overflow ignored?

Who says it's a bad tradeoff?! I run all of my production apps with overflow checking enabled. This is a C# compiler option. I actually benchmarked this and I was not able to determine the difference....
usr's user avatar
  • 2,734
63 votes

What is a good approach to handling exceptions?

This code: try: do_something() except Exception: log_error() is dangerous. Not because you caught a generic Exception but because you suppressed the exception without doing any recovery or ...
candied_orange's user avatar
61 votes

Why do programming languages not let you use a try block without a catch block?

I don't need the catch block. But you do need to catch. The behavior of your code with a catch block is to catch any exception, and then forget that it happened. So any exception that tries to pass ...
Nicol Bolas's user avatar
  • 11.8k
59 votes

Why should 'boneheaded' exceptions not be caught, especially in server code?

Exceptions should be allowed to crash the system if the system has been left in an unrecoverable undefined state. If you can't put the system back in a defined state that ensures data integrity and ...
candied_orange's user avatar
58 votes

Should service throw exception or return when no items specified for deletion

These are two different questions. Should you accept null? That depends on your general policy about null in the code base. In my opinion, banning null everywhere except where explicitly documented ...
Kilian Foth's user avatar
55 votes

Testing the wording of an Exception message

The main point of testing the exception message content is to make sure the right exception is thrown. There may be multiple reasons for which payForCart() throws a PaymentException. So it's not ...
Michael Borgwardt's user avatar
53 votes

Exception vs empty result set when the inputs are technically valid, but unsatisfiable

I agree with Ewan's answer but want to add a specific reasoning. You are dealing with mathematical operations, so it might be a good advice to stick with the same mathematical definitions. From a ...
sigy's user avatar
  • 716
52 votes

Should I throw an exception in case of a meaningful value outside of the range or handle it myself?

If the core of your question is this... If some client code passes an argument whose value is invalid for the thing that my data structure is modeling, should I reject the value or convert it to ...
Theodoros Chatzigiannakis's user avatar
52 votes

Are exceptions for flow control best practice in Python?

The general consensus “don't use exceptions!” mostly comes from other languages and even there is sometimes outdated. In C++, throwing an exception is very costly due to “stack unwinding”. Every ...
amon's user avatar
  • 133k
52 votes

What is a good approach to handling exceptions?

we did not know what to anticipate, while the behavior would be the same for all, e.g. log and continue with execution. To me, this is your major problem. If an exception occurs and you don't know ...
Philip Kendall's user avatar
51 votes

Result object vs throwing exceptions

I'm going to take a slightly different track (I hope) than the other answers. A method should throw an exception when it's unable to fulfill its contract, which is based on how you name the method. ...
Andy's user avatar
  • 2,013
49 votes

Exceptions - "what happened" vs "what to do"

But imagine that is some specific component we do know the best course of actions for a caller. This almost always fails for at least one of your callers, for which this behaviour is incredibly ...
Lightness Races in Orbit's user avatar
47 votes

Having a flag to indicate if we should throw errors

Is this a good way of handling exceptions? No, I think this is pretty bad practice.  Throwing an exception vs. returning a value is a fundamental change in the API, changing the method's ...
Erik Eidt's user avatar
  • 33.4k
47 votes

How to treat unhandled exceptions? (Terminate the application vs. Keep it alive)

You have to expect your program to terminate for more reasons than just an unhandled exception anyway, like a power failure, or a different background process which crashes the whole system. Therefore ...
Doc Brown's user avatar
  • 201k
46 votes

Who should read Exception.Message if at all?

Those messages are for other developers Those messages are expected to be read by developers to help them debug the application. This can take two forms: Active debugging. You're actually running a ...
Arseni Mourzenko's user avatar
46 votes

Error handling considerations

CLAIM: The exception mechanism is a language semantic for handling errors exceptions are a control-flow mechanism. The motivation for this control-flow mechanism, was specifically separating error ...
Useless's user avatar
  • 12.4k
44 votes

What's the logic behind the design of exceptions?

"What value is being divided by zero?" is irrelevant - in C#, there is no legal integer value which is allowed to be divided by zero, and even if there were such a value, I can hardly ...
Doc Brown's user avatar
  • 201k
43 votes

Why should 'boneheaded' exceptions not be caught, especially in server code?

It doesn't really matter if it's a "boneheaded" exception (e.g. a Java unchecked exception) or not. The only question to ask yourself is: "Can the program sensibly continue?" A &...
Paul Draper's user avatar
  • 5,972
43 votes

How do non-RAII languages free resources during stack unwinding?

Both of the programming languages you mention (as well as many other programming languages) provide Automatic Memory Management. What this means is that the programming language is responsible for ...
Jörg W Mittag's user avatar

Only top scored, non community-wiki answers of a minimum length are eligible