Skip to main content
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 ...
Zeta's user avatar
  • 19.3k
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: ...
Simon Forsberg's user avatar
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 ...
jpmc26's user avatar
  • 1,233
9 votes
Accepted

C++ custom exception design

You should probably inherit from std::runtime_error rather than std::exception. The ...
Loki Astari's user avatar
  • 95.7k
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 ...
Cody Gray's user avatar
  • 4,547
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 ...
rolfl's user avatar
  • 97.4k
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 ...
Greg Burghardt's user avatar
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 ...
200_success's user avatar
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 ...
AJNeufeld's user avatar
  • 33.9k
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 ...
t3chb0t's user avatar
  • 44.3k
5 votes
Accepted

Lightweight C Exception Library

Semantics In the languages that implement try, throw and catch the scope is generally larger ...
pacmaninbw's user avatar
  • 24k
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 ...
Quuxplusone's user avatar
  • 19.4k
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 ...
Ferrybig's user avatar
  • 1,357
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: ...
Joachim Rohde's user avatar
5 votes
Accepted

C++ custom exception class

std::string_view Firstly, there seems to be some misusages of string_view. string_view is ...
Mikael H's user avatar
  • 394
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, ...
t3chb0t's user avatar
  • 44.3k
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 ...
J_H's user avatar
  • 32.4k
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 ...
J_H's user avatar
  • 32.4k
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, ...
G. Sliepen's user avatar
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: ...
Toby Speight's user avatar
  • 78.8k
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 ...
Timothy Truckle's user avatar
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 ...
CharlesNRice's user avatar
  • 4,398
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 ...
Deduplicator's user avatar
  • 19.3k
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 ...
dfhwze's user avatar
  • 13.9k
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 ...
πάντα ῥεῖ's user avatar
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 ...
user673679's user avatar
  • 11.8k
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 ...
indi's user avatar
  • 12.4k
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 ...
Sara J's user avatar
  • 3,678
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 ...
mtj's user avatar
  • 4,822

Only top scored, non community-wiki answers of a minimum length are eligible