All Questions
Tagged with exception-handling or exceptions
642 questions
2
votes
1
answer
188
views
Should methods only return an exception of its own exception class?
Consider the following code snippet:
class Foo {
Baz baz;
// ...
void bar() {
int p = this.baz.doSomething();
if (p == this.x) {
throw new FooException('Invalid values', ...
2
votes
4
answers
2k
views
Why raise an exception if python raises it for me?
I have a global position keeper of screen items so items don't need store their own positions.
class Screen_Position_Keeper:
# functions to add stuff
def get_px_row( self, item ):
...
4
votes
0
answers
463
views
Error handling paradigms [closed]
Is there a universally accepted classification of error handling strategies?
What is a general term to refer to those strategies, which can be used while searching for books and articles that compare ...
35
votes
5
answers
52k
views
Function returning true/false vs. void when succeeding and throwing an exception when failing
I'm building an API, a function that uploads a file. This function will return nothing/void if the file was uploaded correctly and throws an exception when there was some problem.
Why an exception ...
26
votes
6
answers
7k
views
Why use Either over (checked) Exception?
Not too long ago I started using Scala instead of Java. Part of the "conversion" process between the languages for me was learning to use Eithers instead of (checked) Exceptions. I've been coding this ...
9
votes
4
answers
6k
views
Should Exceptions Be Expressed on the Domain Model?
Let's say that we have a class PersonName and we throw an exception when some one try to create a instance of PersonName passing an invalid name to the constructor. Should the exception ...
8
votes
5
answers
9k
views
What does it mean to not catch an exception when we can't handle it? [duplicate]
Can anyone explain what exactly Microsoft means on the below linked page when it says:
"Do not catch an exception unless you can handle it and leave the application in a known state."
https://msdn....
4
votes
1
answer
137
views
Is it appropriate to catch all exceptions at toplevel, so that I can log the exception (and possibly save some basic state to disc) before exiting?
Suppose I am writing a program in F# for end-users. If there is an unhandled exception, the program will crash.
Now suppose that I want application-specific data in the crash report. I can get this ...
3
votes
1
answer
501
views
Modifying the tests and refactoring duplicate code in test driven development
I am trying test driven development for the first time (test first development, actually). I wrote down my specifications, then alternated writing tests, then code, writing the code to pass the latest ...
5
votes
4
answers
2k
views
IllegalStateException good practices
I know what IllegalStateException is for, but I wonder if in some cases I should actually use it or it's just a matter of design.
Let's say I have a Tournament class that models a sports tournament ...
2
votes
2
answers
1k
views
Handling exception types according to the current "layer" of appplication
Imagine this simple application use case, say, for fetching data from outisde of the app. These steps represent the "depth" of the layers, from top to bottom.
UI touch event
ViewModel handles said ...
10
votes
2
answers
3k
views
Why are checked vs. unchecked exceptions called “the controversy” in Oracle Java tutorial?
I am new to Java and was reading its documentation on exceptions., and especially the Unchecked Exceptions — The Controversy page.
The bottom-line says:
If a client can reasonably be expected to ...
4
votes
3
answers
2k
views
Unlike C++, why does uncaught exception in JavaScript not terminate the script?
As someone used to C++ and new to JavaScript, I find this behavior odd. Whether a program runs directly on the platform like C++ ones, or it runs at a higher (or deeper?) level like JavaScript ones, ...
3
votes
3
answers
719
views
In critical code, should exceptions describing a nonsense condition be handled?
Let us consider the following C# code as an example:
public static string GetCurentExecutableDirectory()
{
return System.IO.Path.GetFullPath(
System.Reflection.Assembly....
20
votes
5
answers
3k
views
Idiomatic usage of exceptions in C++
The isocpp.org exception FAQ states
Do not use throw to indicate a coding error in usage of a function. Use assert or other mechanism to either send the process into a debugger or to crash the ...
-1
votes
5
answers
5k
views
Finally block when no exceptions thrown
I'm doing some cleanup operations that could throw an exception, and I want to implement the logic of a finally block, but only if no exceptions are thrown:
The initial implementation is this:
...
3
votes
2
answers
2k
views
Adding properties to exceptions
Is it wrong to add fields to exceptions? I have a problem that happens and I'm thinking of adding considerable information to an exception, but it sounds weird: an exception with getters.
What you ...
15
votes
3
answers
30k
views
Is it a good practice to use suppress warnings in your code?
I use @SuppressWarnings("unchecked") and @SuppressWarnings("null") mostly above methods to let the code compile without any warnings but I have my doubts. Found this Stackoverflow question. Jon Skeet ...
2
votes
3
answers
812
views
Why do assertions in Java need to get enabled?
I really like the concept of assertions in Java in the way how to use them. It's much easier than to write an if and then throw an Exception/Error.
But the thing I don't understand is, why do they ...
3
votes
2
answers
156
views
Is it ok to inherit certain exceptions and implement them only through their base types?
So, I was thinking about writing custom exceptions today, and considered the invalid operation exception. This exception can mean many many things, and in some actions, the operations might be invalid ...
4
votes
3
answers
3k
views
A very basic question about whether I should check for null and throw NPE? [duplicate]
Consider the below method-
public void operationOnList(List<String> list) {
list.add(1);
}
It is obvious that if list is null this method will throw a NullPointerException.
My question is ...
1
vote
1
answer
11k
views
When we need to serialize an exception
I have been developing in Java since 8 months, and I didn't face a case where I went to serialize an exception, I'm asking because I saw the serialVersionUID and how Eclipse advise to add it ...
1
vote
2
answers
1k
views
Catching Exception and Recalling same function?
I have a function which sends data to a third-party's server using a library they've supplied. The function is very simple. It just takes a list of objects to send, loops through each item in the list,...
16
votes
5
answers
5k
views
Is indiscriminately catching exceptions (Pokemon exception handling) ever acceptable? [duplicate]
Normally, I don't anticipate exceptions and if I get them, its something that I can't fix in my code - bad user input or connectivity issues with the database.
But errors do occur so at the very ...
3
votes
5
answers
2k
views
Use Exceptions for control flow in order to increase performance?
I add a lot of elements to a list of lists. If the list of lists my element should be saved to, does not exist yet, I handle this by catching an exception and adding a new list to my list of lists.
...
6
votes
2
answers
459
views
Under what scenarios would 'functional' `Try` objects be more or less beneficial than 'rx' `Try` objects?
Definitions used by this question:
'functional' Try is what https://github.com/lambdista/try is. Success and Failure are subclasses of Try. Exceptions aren't thrown by the called function.
'rx' Try is ...
17
votes
11
answers
6k
views
What is better IllegalStateException or silent method execution? [closed]
Let's say I have a MediaPlayer class which has play() and stop() methods. What is the best strategy to use when implementing the stop method in case when the play method has not been called before. I ...
-3
votes
1
answer
218
views
Why can't be "real" custom exceptions be created? [closed]
By "real" I mean Exceptions that would work as the ones predefined by the system.
I mean for a custom exception you have to use the reserved word throw CustomException once you have checked the ...
1
vote
1
answer
694
views
Standalone library for error logging?
Background
Here is how I currently log any exceptions that occur in my code:
Pass each object instance the path to a file where I want all of the logging to happen.
Each of the objects have their own ...
2
votes
1
answer
539
views
Appropriate Exception Type for Connecting to a Device via COM port
I'm working on creating a C# wrapper library around a native C library that allows me to directly communicate with a particular piece of hardware over a serial (COM) port.
Of course, the C library ...
0
votes
1
answer
223
views
The Same Behavior for Boolean and Exception
The following code uses a boolean preference active: In the active state, it does an operation which could throw an exception. On this exception, it does fall back to the non-active state.
let active ...
-1
votes
2
answers
1k
views
How to validate Exception messages? [closed]
My superior asked me to validate each and every input field in the project that includes the Exception messages. So, am I correct in using Exceptional Handler for validating Exception message.
...
6
votes
4
answers
16k
views
Why is a Spring's HttpClientErrorException an unchecked exception?
Oracle summarises the purpose of unchecked exceptions as:
The next question might be: "If it's so good to document a method's API, including the exceptions it can throw, why not specify runtime ...
3
votes
4
answers
630
views
How to name an exception where condition is not exceptional
NOTE: This question is about finding a proper name for an exception where the condition for throwing the exception has not actually happened (eg. preventing a StackOverflow by throwing a ...
2
votes
2
answers
205
views
Java EE exceptions for validation and APMs
In my current work, we have some Java EE applications and we use exceptions and exception mappers to deal with user errors, i.e. that can't be dealt with in the frontend.
We usually reply with a 400, ...
2
votes
3
answers
1k
views
Dealing with runtime exceptions when reading from file
Let's say I read a bunch of numbers from a text file. Each line is initially a string, I need to parse it to an integer. This is where the first exception might happen - NumberFormatException thrown ...
0
votes
2
answers
1k
views
Exception Handling: When and Why?
The main languages I use are C++ and Java.
Both languages support exception handling.
I confess, I may not actually understand exception handling, at least, I certainly don't understand why you ...
3
votes
1
answer
875
views
Overflow Exception Checking Problem
Background
I have to call a method that needs to return a signed integer (see code block below) after converting from an unsigned integer. The reason for this is that I have to do bit-wise math that ...
6
votes
2
answers
2k
views
Returning Unmodifiable Collections only tees you up for runtime exceptions?
Seeing as how there are no distinct Unmodifiable Collections interfaces, aren't you just setting yourself up for runtime exceptions by returning Unmodifiable Collections from method invocations? ...
4
votes
1
answer
294
views
Throwing an exception from within a constructor of an exception
As I was TDD-ing some code, it occurred to me that one of my typed exceptions could potentially throw a NullReferenceException while creating its error message.
Here's the constructor for the typed ...
3
votes
2
answers
1k
views
Is an attempt to create a duplicate resource, e.g. a user, worthy of throwing an Exception?
Say I have an REST API for creating users in an application. The request goes to a controller, which marshals the request data into a domain object, and passes it to a service to create a user.
Now, ...
3
votes
0
answers
316
views
Design interfaces with annotated exceptions, but reduce the need for exception wrapping / re-branding?
I am designing a library with a bunch of interfaces with annotated exceptions. (The example is in (pseudo)PHP, but I am sure one could do something equivalent in Java or other.)
For now, the ...
2
votes
2
answers
4k
views
Error handling in C++
So, in the recent weeks I delved into C++ programming, and I programmed some things in SDL. Doing so, you always have to deal with a lot of (ugly) C++ code, which looks more like C than C++.
One thing ...
5
votes
4
answers
7k
views
Exception treatment with/without recursion
I've entered in an argument with a fellow colleague about a piece of code. While he thinks that it is ok, I don't. But I don't have any more arguments than that calling the same method (recursion) in ...
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 ...
7
votes
2
answers
7k
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 ...
1
vote
1
answer
95
views
How to find the cause of an exception in async code
I often see myself trying to follow my own code to find out where the exception came from.
The typical example is when some parsing fails, and I catch the execption. Then I spend a ridiculous amount ...
13
votes
2
answers
4k
views
Is using Exceptions at the highest level of a program considered bad practice? [duplicate]
I have seen programs using this strategy and I have also seen posts considering this bad practice. However, the posts considering this bad practice have been written in c# or some other programming ...
29
votes
7
answers
12k
views
Should a C++ program catch all exceptions and prevent exceptions from bubbling up past main()?
I was once advised that a C++ program should ultimately catch all exceptions. The reasoning given at the time was essentially that programs which allow exceptions to bubble up outside of main() enter ...
1
vote
1
answer
214
views
Does exception handling belong at the lowest level of the runtime?
I'm designing a fairly simplistic stack-based programming language, and implementing it in Python. (no link, because it's not fully implemented yet.)
The language itself is essentially intended to be ...