Skip to main content

Questions tagged [exceptions]

An exception is an occurrence in an application process that requires deviation from the program's normal flow.

Filter by
Sorted by
Tagged with
251 votes
11 answers
29k views

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 ...
Martin Ba's user avatar
  • 7,608
209 votes
11 answers
140k views

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 ...
Aaron Anodide's user avatar
180 votes
22 answers
58k views

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 ...
180 votes
8 answers
65k views

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 ...
shylynx's user avatar
  • 2,134
161 votes
13 answers
227k views

Is using nested try-catch blocks an anti-pattern?

Is this an antipattern? It is an acceptable practice? try { //do something } catch (Exception e) { try { //do something in the same line, but being less ambitious ...
Mister Smith's user avatar
  • 2,977
119 votes
8 answers
33k views

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 ...
Colin Mackay's user avatar
  • 1,402
116 votes
6 answers
104k views

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. ...
Daenyth's user avatar
  • 8,127
115 votes
17 answers
24k views

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 ...
gaazkam's user avatar
  • 4,459
114 votes
9 answers
180k views

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 ...
Niklas Rosencrantz's user avatar
111 votes
13 answers
12k views

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 ...
anaximander's user avatar
  • 2,285
111 votes
12 answers
21k views

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. ...
Daniel Kaplan's user avatar
107 votes
12 answers
51k views

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 ...
dumazy's user avatar
  • 1,219
106 votes
9 answers
28k views

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 ...
jmq's user avatar
  • 6,078
96 votes
8 answers
53k views

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 ...
Daniel A.A. Pelsmaeker's user avatar
85 votes
10 answers
16k views

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 ...
Clinton's user avatar
  • 1,073
85 votes
13 answers
97k views

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's user avatar
79 votes
15 answers
98k views

Is it ever ok to have an empty catch statement?

I thought about it and could not come up with an example. Why would somebody want to catch an exception and do nothing about it? Can you give an example? Maybe it is just something that should never ...
78 votes
14 answers
18k views

Why is Arithmetic Overflow ignored?

Ever tried to sum up all numbers from 1 to 2,000,000 in your favorite programming language? The result is easy to calculate manually: 2,000,001,000,000, which some 900 times larger than the maximum ...
Bernhard Hiller's user avatar
77 votes
6 answers
32k views

When to use assertions and when to use exceptions?

Often when I write a functions I want to make sure the inputs to it are valid in order to detect such errors as early as possible (I believe these are called preconditions). When a precondition fails, ...
gablin's user avatar
  • 17.4k
69 votes
12 answers
46k views

Is catching general exceptions really a bad thing?

I typically agree with most code analysis warnings, and I try to adhere to them. However, I'm having a harder time with this one: CA1031: Do not catch general exception types I understand the ...
Bob Horn's user avatar
  • 2,337
67 votes
7 answers
61k views

Why is there no 'finally' construct in C++?

Exception handling in C++ is limited to try/throw/catch. Unlike Object Pascal, Java, C# and Python, even in C++ 11, the finally construct has not been implemented. I have seen an awful lot of C++ ...
Vector's user avatar
  • 3,180
66 votes
9 answers
8k views

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

Java has "checked exceptions", which force the caller of the method to either handle an exception or to rethrow it, e.g. // requires ParseException to be handled or rethrown int i = ...
Heinzi's user avatar
  • 9,758
65 votes
12 answers
12k views

Having a flag to indicate if we should throw errors

I recently started working at a place with some much older developers (around 50+ years old). They have worked on critical applications dealing with aviation where the system could not go down. As a ...
Nicolas's user avatar
  • 707
56 votes
8 answers
21k views

Why design a modern language without an exception-handling mechanism?

Many modern languages provide rich exception handling features, but Apple's Swift programming language does not provide an exception handling mechanism. Steeped in exceptions as I am, I'm having ...
orome's user avatar
  • 713
55 votes
4 answers
46k views

Why do iterators in Python raise an exception?

Here's the syntax for iterators in Java (somewhat similar syntax in C#): Iterator it = sequence.iterator(); while (it.hasNext()) { System.out.println(it.next()); } Which makes sense. Here's the ...
NullUserException's user avatar
54 votes
4 answers
53k views

Why shouldn't a method throw multiple types of checked exceptions?

We use SonarQube to analyse our Java code and it has this rule (set to critical): Public methods should throw at most one checked exception Using checked exceptions forces method callers to ...
sdoca's user avatar
  • 669
53 votes
3 answers
22k views

Why are exception specifications bad?

Back in school some 10+ years ago, they were teaching you to use exception specifiers. Since my background is as one of them Torvaldish C programmers who stubbornly avoids C++ unless forced to, I only ...
user avatar
51 votes
4 answers
17k views

Exception propagation: When should I catch exceptions?

MethodA calls an MethodB which in turn calls MethodC. There is NO exception handling in MethodB or MethodC. But there is exception handling in MethodA. In MethodC an exception occurs. Now, that ...
Daniel Frost's user avatar
51 votes
14 answers
8k views

Why are errors named as "Exception" but not as "Error" in programming languages?

I've been thinking about that for quite a while actually. I am not a native English speaker myself but still, I have years of programming experience and I always asked myself this. Why is it named as ...
Tarik's user avatar
  • 807
51 votes
4 answers
9k views

Python Forgiveness vs. Permission and Duck Typing

In Python, I often hear that it is better to "beg forgiveness" (exception catching) instead of "ask permission" (type/condition checking). In regards to enforcing duck typing in Python, is this try: ...
darkfeline's user avatar
  • 1,243
51 votes
7 answers
24k views

Workaround for Java checked exceptions

I appreciate a lot the new Java 8 features about lambdas and default methods interfaces. Yet, I still get bored with checked exceptions. For instance, if I just want to list all the visible fields of ...
Fernando Miguel Carvalho's user avatar
50 votes
9 answers
9k views

Throw exception or let code fail

I am wondering if there are any pros and cons against this style: private void LoadMaterial(string name) { if (_Materials.ContainsKey(name)) { throw new ArgumentException("The ...
async's user avatar
  • 854
50 votes
5 answers
26k views

should I throw exception from constructor?

I know I can throw exception from constructor in PHP but should I do it? For example, if a parameter's value is not as I expected it. Or should I defer throwing an exception till a method is invoked. ...
WordsWorth's user avatar
49 votes
10 answers
16k views

Why are exceptions considered better than explicit error testing? [duplicate]

Possible Duplicate: Defensive Programming vs Exception Handling? if/else statements or exceptions I often come across heated blog posts where the author uses the argument: "exceptions vs ...
Richard Keller's user avatar
48 votes
11 answers
11k views

Are error variables an anti-pattern or good design?

In order to handle several possible errors that shouldn't halt execution, I have an error variable that clients can check and use to throw exceptions. Is this an Anti-Pattern? Is there a better way to ...
Mikayla Maki's user avatar
47 votes
8 answers
9k views

Are there legitimate reasons for returning exception objects instead of throwing them?

This question is intended to apply to any OO programming language that supports exception handling; I am using C# for illustrative purposes only. Exceptions are usually intended to be raised when an ...
stakx's user avatar
  • 2,138
46 votes
8 answers
125k views

Why doesn't "object reference not set to an instance of an object" tell us which object?

We're launching a system, and we sometimes get the famous exception NullReferenceException with the message Object reference not set to an instance of an object. However, in a method where we have ...
Saeed Neamati's user avatar
45 votes
8 answers
5k views

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

I have written a struct that represents latitude/longitude coordinates. Their values range from -180 to 180 for longtitudes and 90 to -90 for lattitudes. If a user of that struct gives me a value ...
RaidenF's user avatar
  • 1,579
40 votes
9 answers
10k views

Should I accept empty collections in my methods that iterate over them?

I have a method where all logic is performed inside a foreach loop that iterates over the method's parameter: public IEnumerable<TransformedNode> TransformNodes(IEnumerable<Node> nodes) { ...
Nick Udell's user avatar
  • 1,214
40 votes
9 answers
10k views

Testing the wording of an Exception message

Doing a code review, I ran into this assertion in a unit test: assertThatThrownBy(() -> shoppingCartService.payForCart(command)) .isInstanceOfSatisfying(PaymentException.class, exception -&...
Kricket's user avatar
  • 723
40 votes
7 answers
15k views

Maybe monad vs exceptions

I wonder what are the advantages of Maybe monad over exceptions? It looks like Maybe is just explicit (and rather space-consuming) way of try..catch syntax. update Please note that I'm intentionally ...
Vladimir's user avatar
  • 503
40 votes
11 answers
32k views

Are there any real-world cases for C++ without exceptions? [closed]

In When to use C over C++, and C++ over C? there is a statement wrt. to code size / C++ exceptions: Jerry answers (among other points): (...) it tends to be more difficult to produce truly tiny ...
Martin Ba's user avatar
  • 7,608
39 votes
6 answers
32k views

Try/Catch/Log/Rethrow - Is Anti Pattern?

I can see several post where importance of handling exception at central location or at process boundary been emphasized as a good practice rather than littering every code block around try/catch. I ...
rahulaga-msft's user avatar
39 votes
4 answers
8k views

Why are brackets required for try-catch?

In various languages (Java at least, think also C#?) you can do things like if( condition ) singleStatement; while( condition ) singleStatement; for( var; condition; increment ) ...
Svish's user avatar
  • 1,102

1
2 3 4 5
13