245
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 ...
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 ...
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
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 ...
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 ...
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....
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, ...
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 ...
72
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 ...
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.
...
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....
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 ...
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 ...
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 ...
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 ...
56
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 ...
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 ...
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 ...
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 ...
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. ...
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 ...
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 ...
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 ...
45
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 ...
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 ...
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 ...
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 &...
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 ...
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 (...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
exceptions × 766java × 146
c# × 84
error-handling × 69
c++ × 48
design × 45
python × 38
.net × 38
programming-practices × 30
design-patterns × 28
php × 26
logging × 22
object-oriented × 18
coding-style × 17
architecture × 15
language-agnostic × 14
object-oriented-design × 12
null × 12
rest × 11
api-design × 11
language-design × 11
clean-code × 11
validation × 11
unit-testing × 9
code-quality × 9