An exception is an occurrence in an application process that requires deviation from the program's normal flow.
Questions tagged [exceptions]
485 questions
252
votes
11 answers
Why do many exception messages not contain useful details?
It seems there is a certain amount of agreement that exception messages should contain useful details.
Why is it that many common exceptions from system components do not contain useful details?
A few examples:
.NET List index access…

Martin Ba
- 7,578
- 7
- 34
- 56
200
votes
10 answers
Are exceptions as control flow considered a serious antipattern? If so, Why?
Back in the late 90's I worked quite a bit with a code base that used exceptions as flow control. It implemented a finite state machine to drive telephony applications. Lately I am reminded of those days because I've been doing MVC web apps.
They…

Aaron Anodide
- 5,463
- 5
- 28
- 37
179
votes
8 answers
Exceptions: Why throw early? Why catch late?
There are many well-known best practices about exception handling in isolation. I know the "do's and don'ts" well enough, but things get complicated when it comes to best practices or patterns in larger environments. "Throw early, catch late" - I've…

shylynx
- 2,124
- 3
- 13
- 16
178
votes
22 answers
Are null references really a bad thing?
I've heard it said that the inclusion of null references in programming languages is the "billion dollar mistake". But why? Sure, they can cause NullReferenceExceptions, but so what? Any element of the language can be a source of errors if used…

Tim Goodman
- 2,644
- 2
- 28
- 25
117
votes
7 answers
How to write a good exception message
I'm currently doing a code review and one of the things I'm noticing are the number of exceptions where the exception message just seems to reiterate where the exception occurred. e.g.
throw new Exception("BulletListControl: CreateChildControls…

Colin Mackay
- 1,382
- 2
- 10
- 13
116
votes
6 answers
Is it better to use assert or IllegalArgumentException for required method parameters?
In Java, which is more highly recommended, and why? Both types will throw exceptions, so in that regard handling them is the same. assert is slightly shorter, but I'm not sure how much that matters.
public void doStuff(Object obj) {
assert obj…

Daenyth
- 8,077
- 3
- 31
- 46
115
votes
17 answers
Why should 'boneheaded' exceptions not be caught, especially in server code?
I am confused because in quite a few places I've already read that the so-called 'boneheaded' exceptions (ones that result from bugs in code) are not supposed to be caught. Instead, they must be allowed to crash the application:
Vexing exceptions,…

gaazkam
- 3,517
- 3
- 19
- 35
112
votes
9 answers
Why use try … finally without a catch clause?
The classical way to program is with try ... catch. When is it appropriate to use try without catch?
In Python the following appears legal and can make sense:
try:
#do work
finally:
#do something unconditional
However, the code didn't catch…

Niklas Rosencrantz
- 8,008
- 17
- 56
- 95
111
votes
13 answers
Exception vs empty result set when the inputs are technically valid, but unsatisfiable
I'm developing a library intended for public release. It contains various methods for operating on sets of objects - generating, inspecting, partitioning and projecting the sets into new forms. In case it's relevant, it's a C# class library…

anaximander
- 2,285
- 3
- 19
- 21
111
votes
12 answers
I've been told that Exceptions should only be used in exceptional cases. How do I know if my case is exceptional?
My specific case here is that the user can pass in a string into the application, the application parses it and assigns it to structured objects. Sometimes the user may type in something invalid. For example, their input may describe a person but…

Daniel Kaplan
- 6,776
- 5
- 32
- 46
104
votes
9 answers
Check First vs Exception Handling?
I'm working through the book "Head First Python" (it's my language to learn this year) and I got to a section where they argue about two code techniques:
Checking First vs Exception handling.
Here is a sample of the Python code:
# Checking First
for…

jmq
- 6,048
- 5
- 28
- 39
103
votes
12 answers
Result object vs throwing exceptions
When sending a request to another module and expecting a result, it seems to me there are two ways of dealing with the 'non-happy paths'.
Throw an exception
Return a result object that wraps different results (such as value and error)
I would say…

dumazy
- 1,179
- 2
- 8
- 10
96
votes
8 answers
Return magic value, throw exception or return false on failure?
I sometimes end up having to write a method or property for a class library for which it is not exceptional to have no real answer, but a failure. Something cannot be determined, is not available, not found, not currently possible or there is no…

Daniel A.A. Pelsmaeker
- 2,715
- 3
- 22
- 27
85
votes
10 answers
Exceptions, error codes and discriminated unions
I've recently started a C# programming job, but I've got quite a bit of background in Haskell.
But I understand C# is an object-orientated language, I don't want to force a round peg into a square hole.
I read the article Exception Throwing from…

Clinton
- 1,053
- 1
- 11
- 14
84
votes
13 answers
Is it good practice to catch a checked exception and throw a RuntimeException?
I read some code of a colleague and found that he often catches various exceptions and then always throws a 'RuntimeException' instead. I always thought this is very bad practice. Am I wrong?

RoflcoptrException
- 2,035
- 2
- 17
- 18