Questions tagged [exceptions]
An exception is an occurrence in an application process that requires deviation from the program's normal flow.
631
questions
118
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 ...
1
vote
2
answers
311
views
Exception Handling in Hexagonal Architecture
how could we advise the web client about a non-recoverable exception thrown by the persistence adapter ?
At first sight, I would
define a domain exception to be thrown by the persistence adapter and
...
0
votes
1
answer
140
views
How to correctly extend runtime exception?
We have a GraphQL server which sends data to the front end client.
We have other tenants who will use our sever and host their code.
I want to create a system where they all can create any custom ...
0
votes
1
answer
97
views
Return response from controller or raise exception from service
I need some guidance on how to send error responses to client from WebAPI controller for an update operation. I need to check if data is changed and if it has duplicate data. I have service class that ...
22
votes
4
answers
11k
views
How should I provide additional information about an exception?
Everytime I need to provide additional information about an exception I wonder which way is actually the right way of doing this.
For the sake of this question I wrote an example. Let's assume there ...
2
votes
1
answer
110
views
Is returning Result types the standard way of dealing with errors in Kotlin?
Given that there are no checked exceptions in Kotlin, are Result types the correct way to indicate an exception occurred to the caller?
For example, I have the following function in my code:
suspend ...
-2
votes
3
answers
270
views
Locally throw exception and handle it
I came across a piece of legacy code, almost on the line of this (sample)
int foo() {
try {
int id = generateID();
if (isIDUsed(id)) throw id;
return id;
} catch (int ...
161
votes
13
answers
226k
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
...
28
votes
12
answers
11k
views
Is the 'finally' portion of a 'try ... catch ... finally' construct/structure even necessary?
Some languages (such as C++ and early versions of PHP) don't support the finally part of a try ... catch ... finally construct. Is finally ever necessary? Because the code in it always runs, why ...
10
votes
1
answer
690
views
Different kind of checked exceptions - Guarantee to only throw X
It is a widely held position that checked exceptions as implemented in Java are a bad idea. If you mark a method as throwing, calling code has to either catch the exception, or be marked as throwing, ...
0
votes
2
answers
500
views
Is Authentication a good use case for a checked exception according to Effective Java (Bloch)?
There's a section about use of checked exceptions in Josh Bloch Effective Java and I find it a bit abstract to understand what he means by saying "when a user can recover from it". ...
1
vote
1
answer
89
views
Throw generic exception vs front end friendly exception from util methods?
The below code sample given is for Salesforce but it is close to Java and you should be able to understand easily what I am trying to ask here.
AccountController.getAccountsThatMeetSpecificCrtieria is ...
0
votes
1
answer
74
views
Communicating unpredicted Failure from Repository implementation to Applicaiton Layer
My application follows Clean Architecture wherein the Application Layer wraps the Domain Layer. I try to adhere to DDD more-so as a "guiding light" than a strict rulebook.
Within the Domain ...
2
votes
3
answers
443
views
Exceptions vs Monads
I am curious about the utility of something like monads in the C# world.
My experience with these kinds of things is mainly through Rust but I'm a dotnet dev for work
I was thinking about the ...
36
votes
4
answers
20k
views
Are exceptions for flow control best practice in Python?
I'm reading "Learning Python" and have come across the following:
User-defined exceptions can also signal nonerror conditions. For
instance, a search routine can be coded to raise an exception ...
3
votes
1
answer
537
views
How to handle Django get-single-instance-in-view pattern?
A lot of my Django views start a little bit like this:
try:
# here request.POST could also be request.GET or a captured URL parameter
MyModel.objects.get(user = request.user, some_attr = int(...
1
vote
3
answers
6k
views
Exception Handling in Java web application
I know exception handling is a topic often discussed in the world of Java. I've read a few threads on here and SO as well.
https://stackoverflow.com/questions/2169339/exception-handling-in-a-java-...
10
votes
8
answers
1k
views
When are try/exceptions not an anti-pattern?
I just finished a discussion with colleagues regarding the use of exceptions in code. Here is the code (pseudocode) that they had written:
resp = fetch(URL)
if resp.status_code != 200:
return ...
5
votes
5
answers
2k
views
Java Exception Error Enumerations Anti-pattern
Over the years I've many times seen folks enumerate error codes for exceptions in Java. I generally have felt this leads to more tedious code and does not provide value.
For example, there will be an ...
10
votes
1
answer
3k
views
Separate namespace just for exceptions?
I was doing a code review and came across something odd which I've never seen before. The developer decided to create a sub-namespace just to contain all the assembly's exceptions.
I thought I had ...
7
votes
2
answers
3k
views
Is returning true or throwing an exception good or bad code practice?
I am focusing on learning better design and wondered if this code is good or an anti-pattern? The function Validate() returns true if the data is correct, else it will throw an exception with a ...
27
votes
14
answers
9k
views
What's the logic behind the design of exceptions?
I'm writing an application in C#. I'm facing an InvalidConstraintException, but from that Exception, I seem not to be able to access the Constraint, causing the Exception. That specific question is ...
206
votes
10
answers
139k
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 ...
75
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, ...
9
votes
2
answers
1k
views
Granularity of Exceptions
I've run into a debate between a few friends and I. They prefer general exceptions such as ClientErrorException and ServerErrorException with detail as fields of the exception, whereas I prefer making ...
3
votes
4
answers
683
views
How to avoid code duplication when else and except are the same?
A simplified version of my code looks like this:
def process( object ):
try:
if suitedForA( object ):
try:
methodA( object )
except:
...
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 ...
5
votes
7
answers
2k
views
Why have many programmers moved to using exception handling for input or output? [closed]
Why have many programmers moved to using exception handling for input or output? For these programmers, what is the motivation behind this decision?
69
votes
12
answers
45k
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 ...
0
votes
1
answer
118
views
Reuse same code for validation and runtime integrity checks?
Scenario
I have a system where a user can upload an Excel file which is validated and then used to provide data for the application.
There are two distinct operations I need to handle with this file:
...
5
votes
3
answers
5k
views
Authoritative sources for exception handling best practices [closed]
My work place suffers from a bad case of the Pokemon Exception Handling anti-pattern with constructs like the following splattered across the code base:
try {
...
} catch (Exception ex) {
// ...
2
votes
2
answers
956
views
We need a custom strategy for collecting unhandled application exceptions. What are our options?
"Unhandled exception" term
In .NET Framework, unhandled exceptions are the exceptions which were not handled by the application itself, and result in a crash. In a case of a desktop application, it ...
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 ...
26
votes
10
answers
13k
views
Error handling - Should a program fail on errors or silently ignore them
I'm writing a simple little program to transmit MIDI over a network. I know that the program will encounter transmission problems and / or other exception situations that I won't be able to predict.
...
7
votes
2
answers
1k
views
Is it bad practice to have functions whose sole purpose is to throw errors?
I have the following situation.
I'm writing a c++ program where I have a function that checks a custom equivalence of two json objects. If the check fails, I don't want the entire program to stop (...
20
votes
4
answers
4k
views
How do non-RAII languages free resources during stack unwinding?
C++ features deterministic order of calling destructors up the call stack until the exception is handled somewhere.
To my knowledge, neither Java nor (object oriented) Python provide this. In those ...
5
votes
2
answers
5k
views
What is considered best practice for custom exception classes?
Python has many strong conventions but I'm unclear on how best to manage exceptions for my module. I know it's generally good practice to define a custom exception for your module. E.g.:
class ...
2
votes
6
answers
1k
views
How assertive should I be in handling exceptions in objects?
I have been writing in C# 4.0 a lot lately and trying to write as lean as possible. As such, I have not been using the classic try/catch blocks and using statements as often.
I understand the general ...
0
votes
3
answers
3k
views
C++ and system exceptions
Why standard C++ doesn't respect system (foreign or hardware) exceptions?
E.g. when null pointer dereference occurs, stack isn't unwound, destructors aren't called, and RAII doesn't work.
The ...
10
votes
6
answers
28k
views
Throwing and catching exceptions in the same function/method
I've written a function that asks a user for input until user enters a positive integer (a natural number). Somebody said I shouldn't throw and catch exceptions in my function and should let the ...
1
vote
3
answers
223
views
Should you reuse a PHP exception's code when wrapping it?
When catching and wrapping an exception in PHP, assuming the new exception doesn't have a meaningful code of its own, should you also use the caught exception's code value? or the default value? In ...
20
votes
2
answers
5k
views
Abstract exception super type
If throwing System.Exception is considered so bad, why wasn't Exception made abstract in the first place?
That way, it would not be possible to call:
throw new Exception("Error occurred.");
This ...
-2
votes
2
answers
4k
views
What are the possible *root causes* of a SocketTimeoutException?
I understand that a SocketTimeoutException (I'm in Java, but I guess it's the same in just about every major language) happens after a server or client doesn't respond after a period of time, let say ...
9
votes
2
answers
18k
views
Exception handling in Python - Am I doing this wrong (and why?)
I've read many questions and articles on exception handling in Python (and in general), but I still think that it's the most confusing thing ever. I ended up doing something like this:
# error class ...
51
votes
4
answers
16k
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 ...
1
vote
2
answers
293
views
Should domain know about exceptions thrown from outer layer?
Let's take a hypothetical system, a state based,
exceptions thrown from persistence layer or other outer layer, should propagate to ui layer through domain layer. Domain need not know about this ...
-1
votes
1
answer
552
views
How to handle third-party libraries that can potentially throw, without knowing what kind of exceptions they may have?
I'm starting to run into this sort of dilemma while many third-party APIs, but I will use MongoDB for my examples. Consider the following code:
var settings = MongoClientSettings....
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 ...
1
vote
1
answer
356
views
Is the use of a catch block as a "rollback mechanism" a bad thing?
I have a service that performs a series of actions based on user information (name, address, etc), one of the problems is that the User entity comes from a legacy third party API (as a repository) ...