26
votes
Prompting for two integers and dividing, using exceptions to handle divide-by-zero errors
Exceptions are for exceptional cases
I'm trying to learn exception handling in C++.
This is a very bad example to learn exceptions. They are meant for situations where you cannot know that there ...
17
votes
Compiling and throwing simple dynamic exceptions at runtime for JVM
Is there anything that can be simplified or made even cleaner?
Yes, don't invoke the Java compiler at runtime.
From your examples in a comment:
...
11
votes
Compiling and throwing simple dynamic exceptions at runtime
Surprising
This is a gross violation of the Principle of Least Surprise. The only possible reaction I could have seeing this in the wild would be, "What the heck?" This is not a normal way of using ...
9
votes
Accepted
C++ custom exception design
You should probably inherit from std::runtime_error rather than std::exception.
The ...
7
votes
Prompting for two integers and dividing, using exceptions to handle divide-by-zero errors
Lots of bad practices exhibited here…
First of all, you probably shouldn't be using exception handling here at all, as Incomputable already pointed out in a comment. Exceptions should be for truly ...
7
votes
Self-inflicted killing utility
Interesting little plot twist in a Java program.
First up, tryNow() is dead code, it's not used, get rid of it, it makes the rest of the class harder to understand ...
7
votes
"Guess a random letter" game with command line interface
You are using exceptions for flow control, and that is considered an anti-pattern.
Exceptions are meant for unhandlable conditions that would otherwise corrupt your application. Winning the game ...
7
votes
Accepted
Wordle guesser, breaking out of loop by raising exception
Program structure
The flow of control leaves a lot to be desired.
Exceptions should be used for exceptional situations, not as a kind of break or ...
6
votes
Accepted
Reader for graph-file with ugly return and questionable use of streams
Exception Handling
What are you doing in the exceptional case?
Printing an error message (ugly stack trace, actually) and returning null. Now the caller has to ...
5
votes
Accepted
Generating unique ID of an exception
Stack trace
I think you shouldn't work with the stack string but retrieve the stack and go through each frame yourself. I find it would make the analysis more reliable and you could get the ...
5
votes
Accepted
Lightweight C Exception Library
Semantics
In the languages that implement try, throw and catch the scope is generally larger ...
5
votes
Accepted
Custom C++ exception class with stack trace generation
Exception( const char *cmessage ) :
I strongly recommend making this constructor explicit, so that you don't permit the user ...
5
votes
Accepted
Compiling and throwing simple dynamic exceptions at runtime for JVM
Setting isAccessible to true
You seem to be always settings isAccessible to true. This is only needed if you are accessing ...
5
votes
Accepted
One try with multiple catch blocks vs nested try
If you are not going to handle each exception differently just use a multicatch:
...
5
votes
Accepted
C++ custom exception class
std::string_view
Firstly, there seems to be some misusages of string_view. string_view is ...
5
votes
Accepted
Class to wrap result or exception, similar to the OneOf approach
Other solutions
There have already been several attempts to create something similar. They were called: Maybe, Failable, ...
5
votes
Write a simple, clean error message without a backtrace and exit on failure
The abort alternative sounds like a non-starter. What you wrote in the OP seems ideal.
Library code that uses raise is re-usable in many settings.
The final ...
5
votes
Accepted
Catching the timed out Exception raised by the __init__ method of the class ftplib.FTP
no name mangling
self.__username = username
I don't know what's going on with that __ dunder prefix.
But name mangling ...
5
votes
Accepted
Wrap a noexcept C++ library method with a method throwing exceptions with usable explanatory strings to stay DRY
Alternatives to exceptions
There are various ways to approach it. You don't necessarily need exceptions to ensure your code stays DRY. For example, if you just want to get an error message to print, ...
5
votes
Wrap a noexcept C++ library method with a method throwing exceptions with usable explanatory strings to stay DRY
Let's look at the switch, and in particular, the default case:
...
4
votes
Accepted
Custom exception for string that is too long
if(str1.equalsIgnoreCase("done"))
System.exit(0); //exit on first startup
Do not use System.exit() (somewhere else ...
4
votes
Accepted
Creating data objects dynamically at runtime from an interface
Cache the type so you don't build it each time. ConcurrentDictionary could do it. Just make a static field to hold the dictionary
...
4
votes
Custom C++ exception class with stack trace generation
It's very surprising that your custom exception-class can throw an exception for anything but original creation. std::exception and derived standard classes aren't ...
4
votes
Accepted
Employee database
Structure
Database does not adhere to the concept of Single Responsibility. Ignoring the test code, there are some considerations to make. I would only keep the ...
4
votes
C++ custom exception class
One problem I can see with your code is, that you can't catch MissingResource unless you know the specific Resource type used ...
4
votes
Accepted
What should a C++ error reporting exception class have to be portable across language versions? (ver 1)
The std::string constructor and copy-constructor can throw, so we can't mark the MyError constructor or copy-constructor ...
4
votes
What should a C++ error reporting exception class have to be portable across language versions? (ver 1)
Design review
If you really, really want to support C++98, then, to put it bluntly, you simply cannot support a best practices model. Just give it up. Literally impossible. Supporting C++98 is already ...
4
votes
Accepted
Generic Scanner(System.in) Parsing
While it can sometimes be useful to expose only static members and not allow instances of your class to be constructed, I don't see what you're gaining by doing that here. But by not allowing multiple ...
3
votes
Accepted
Automatic exception wrapper in java
Alas, what you would like to do is not possible. You could indeed play around with interceptors in a CDI container or with an AOP framework, and basically change the exception type at runtime. BUT ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
exception × 128error-handling × 45
java × 40
c# × 34
c++ × 20
python × 15
php × 8
validation × 8
beginner × 6
object-oriented × 6
strings × 4
c++20 × 4
ruby × 3
formatting × 3
generics × 3
io × 3
interface × 3
compiler × 3
python-3.x × 2
c × 2
programming-challenge × 2
parsing × 2
python-2.x × 2
.net × 2
reinventing-the-wheel × 2