All Questions
Tagged with exception-handling or exceptions
642 questions
5
votes
5
answers
590
views
Is it good practice to check exception messages in unit tests? [duplicate]
Imagine I have a function like this (written in Java):
void sayHello(String firstName, String lastName) {
if (firstName == null) {
throw new IllegalArgumentException("first name is ...
4
votes
2
answers
503
views
Control flow and communication with two separate frontends (maybe with exceptions)?
I am trying to write a backend for use with a completely text based UI for one shot operations (eg. python scriptname arg, executes that argument and exits) and a GUI using the curses library for some ...
1
vote
4
answers
547
views
Combining multiple input validations into one exception
In order to make sure methods fail fast under invalid arguments, I usually do validation at the start of a method. If I'm verifying multiple predicates, this leads to a few lines of checkNotNull or ...
0
votes
2
answers
731
views
Explain why it's bad to use a middleware to coat error messages as exceptions
We manage a backend application behind a FastAPI REST controller with multiple endpoints.
A member of our team decided to use a middleware on the application which parses the body of the response for ...
5
votes
6
answers
2k
views
When to write a custom exception handler?
A long time ago I was in a class and the professor had us write individual exception handlers for every possible error.
This seems almost impossible to do when developing large pieces of software ...
3
votes
1
answer
825
views
How to catch every exception in a multi-threaded C++ app with the minumum of code?
I have been tasked with the title above. There is currently zero exception handling. There are 100+ threads, all created from main().
Exceptions won't percolate up to main() - they won't leave the ...
0
votes
1
answer
306
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 ...
2
votes
1
answer
225
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 ...
1
vote
2
answers
684
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". ...
0
votes
1
answer
100
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 ...
10
votes
8
answers
2k
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 ...
3
votes
4
answers
1k
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:
...
21
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 ...
-2
votes
2
answers
13k
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 ...
-1
votes
1
answer
774
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....
5
votes
6
answers
531
views
Unsupported concurrent calls, throw exception or log a warning?
Context
Let's say I have a navigation service that allows me to navigate to a page.
The Navigate method is async because an animation (about 250ms) is involved.
public interface INavigator
{
...
0
votes
3
answers
203
views
Communicating error conditions in client API for remote RESTful server, what's the best way?
I'm writing an application based on a RESTful API located in a server (written in Python).
An Android app will access the API endpoints to get, post, put or delete data. The usual.
The server has a ...
1
vote
2
answers
268
views
Is nesting try-except sequence in try-else block bad form?
Ive got a boot sequence that needs to check some registry values, they may or may not be present, so each check needs to be wrapped in its own try-except. I try to avoid nesting as I think it can lead ...
8
votes
10
answers
720
views
Boneheaded exceptions should not be caught. Then how to provide fault tolerance and reliability?
I've always been taught that fatal exceptions (indicating problems that cannot be solved programmaticaly) and boneheaded exceptions (resulting from bugs in my code) should not be caught, should not be ...
4
votes
3
answers
5k
views
Business logic error handling. Should exceptions really be avoided?
C#'s primary error handling mechanism are exceptions and try pattern.
We don't have access to discriminated unions yet like in case of F# and Rust Option<T> and Result<T, E> types.
The ...
0
votes
2
answers
173
views
How can I avoid re-running code when exceptions are thrown and user re-submits?
I have a checkout process that 1) creates a user account in Stripe and my database, 2) creates a paymentMethod in Stripe and logs the last4 in the database, 3) creates the subscription in Stripe and ...
6
votes
3
answers
1k
views
How to simplify exception handling for library users?
Suppose of having a library exposing the following piece of functionality:
public static class AwesomeHelpers
{
public static async Task<int> ComputeSomethingImportAsync(CalculationInputs ...
2
votes
2
answers
3k
views
Validations and throwing exceptions in DDD?
I have a question regarding validations and exceptions in DDD.
I have a ValueObject say, PasswordText which takes a string argument in it's constructor. Checks if the string matches the password ...
1
vote
2
answers
380
views
Best Practice: Unit test coverage vs. in-method sanity checks [duplicate]
I have a code-coverage requirement of of a certain percentage, and face the following tradeoff:
Should I sacrifice in-method sanity checks and error handling for ease of (unit-) testability?
Lets ...
1
vote
2
answers
164
views
Are the exceptions used in BeanValidation/JAX-RS's ExceptionMapper an anti pattern?
I am reading a lot about patterns and code structure and something that bothers me is BeanValidation's way to handle errors. I like Java and think that BeanValidation is easy to use, but it seems to ...