Skip to main content
Share Your Experience: Take the 2024 Developer Survey
244 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
Accepted

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,633
144 votes
Accepted

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
  • 76.3k
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
87 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,955
86 votes
Accepted

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,390
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
Accepted

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,488
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
71 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
  • 46.2k
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,764
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.9k
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
Accepted

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

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
  • 134k
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

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,014
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
  • 34.1k
47 votes
Accepted

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
  • 208k
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.6k
44 votes

Why do "checked exceptions", i.e., "value-or-error return values", work well in Rust and Go but not in Java?

From a scientific point of view, checked exceptions can be seen as alternative return values, e.g. Exactly. They can be seen that way, and they should be but they aren't. Using an Error type like is ...
Jörg W Mittag's user avatar
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
  • 208k
43 votes
Accepted

Function returning true/false vs. void when succeeding and throwing an exception when failing

Throwing an exception is simply an additional way of making a method return a value. The caller can check for a return value just as easily as catch an exception and check that. Therefore, deciding ...
Kilian Foth's user avatar
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
  • 6,022
43 votes
Accepted

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
42 votes

Should I raise an exception/error when an optional argument is used but is not necessary?

Exceptions should be thrown in exceptional situations. If an exception is thrown that could be avoided, that’s a bug on the caller side. So you force the caller to change their code from Fetch_person (...
gnasher729's user avatar
  • 45.7k

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