Why are Pokemon Exceptions considered code smell?

  • Context:
  • Thread starter Thread starter SlurrerOfSpeech
  • Start date Start date
  • Tags Tags
    Code Smell
Click For Summary

Discussion Overview

The discussion centers around the concept of "Pokemon Exceptions" in programming, particularly in the context of exception handling in languages like Java and C++. Participants explore the implications of catching all exceptions without specificity, debating whether this practice constitutes a code smell and under what circumstances it may be justified.

Discussion Character

  • Debate/contested
  • Technical explanation
  • Conceptual clarification

Main Points Raised

  • Some participants argue that using a catch-all exception handler can be justified when dealing with third-party libraries that lack documentation, as it may be impractical to anticipate all possible exceptions.
  • Others point out that catching all exceptions can lead to poor handling practices, especially in languages like Java where checked exceptions require explicit handling, potentially cluttering the code.
  • A participant suggests that unchecked exceptions provide a cleaner approach, allowing exceptions to propagate without mandatory handling, which can lead to better exception management.
  • There is a viewpoint that catching general exceptions may be acceptable if the system's behavior does not change based on the specific exception type, but it raises concerns about polluting higher-level classes with lower-level implementation details.
  • Another participant proposes that wrapping lower-level exceptions in more meaningful higher-level exceptions can be a better practice to avoid exposing implementation details to higher-level modules.

Areas of Agreement / Disagreement

Participants express differing opinions on the appropriateness of catching all exceptions, with no consensus reached on whether it is inherently a code smell or if there are valid scenarios for its use.

Contextual Notes

Limitations include the lack of consensus on best practices for exception handling, the dependence on specific programming languages, and the varying interpretations of what constitutes a code smell in different contexts.

SlurrerOfSpeech
Messages
141
Reaction score
11
"Gotta catch 'em all"

e.g.

Code:
try { 
  // something that might fail for reasons I don't want to anticipate
}
catch (Exception ex) { 
    // do something
}

I believe there are justifications for doing this:

  • Inside the try block I'm using a class from a 3rd party library that doesn't have any documentation. I'm not sure exactly what exceptions might be thrown and for what reason.
  • I'm aware of at least 5 types of exceptions that could be thrown and I would handle them each the same way. It seems rather stupid to write
Code:
catch (ExceptionType1 ex) {
   // do something
}
catch (ExceptionType2 ex) {
   // do same thing as before
}
 
Technology news on Phys.org
A Google search for "c++ catch all exceptions" led me to some pages (e.g. this one) that give this code:

Code:
try {
  // something that might fail for reasons I don't want to anticipate
}
catch (...) {
    // do something
}
 
Java has a notion of checked exceptions and unchecked exceptions. For checked exceptions you must add code to either catch the exception or indicate that the given method may throw this exception. As you can imagine your code gets riddled with exception check logic. This causes many programmers to handle the exception poorly when they don't quite know what to do about. By poorly I mean that they may ignore it.

Unchecked exceptions like RuntimeExceptions can happen at anytime and so Java doesn't require you to catch them but instead percolates them to the top and then exit. You then have the option to catch it or not at the appropriate spot. You now have leaner code that handles exceptions better.

One Java best practices technique is to capture checked exceptions and then recast them as RuntimeExceptions or as custom subclasses thereof, if there is no better recourse thus ending the Java compiler check tyranny and ensuring that no exception will be left behind. For large programs this strategy works well but not all Java programmers ascribe to this best practice.

Here's a discussion on the tradeoffs:

https://docs.oracle.com/javase/tutorial/essential/exceptions/runtime.html
 
Last edited:
When the call of the 3rd party library fails, catching the most general exception,
Java:
try {
  // something that might fail for reasons I don't want to anticipate
}
catch (Exception ex) {
    // do something
}
is not a bad idea, unless your system should behave differently when different exceptions are thrown. When a failure of the 3rd party method is considered to be an unexpected exception and you need to rethrow it, then why not omit the whole try-catch-block at all and let the system propagate the exception to the caller level automatically?
Because in this situation you would pollute your higher level classes with implementation details of the lower level classes (= 3rd party library in this case) and this is not a good idea. So, here the general try-catch-construction above is even a "good" smell: Just wrap the low-level exception with a higher level exception that is more appropriate to your program. For example, when reading data from a data source and your 3rd party modul is some SQL library, propagation of SQLException's to your high-level classes might not be a good idea, because in future you may switch to some other kind of data source that does not know anything about SQL, but you have polluted your higher level modules with SQLExceptions. Wrapping the SQLException into an IllegalStateException, IllegalArgumentException ... or even a general RuntimeException (or something meaningful) is a better way to handle it.
 
  • Like
Likes   Reactions: jedishrfu

Similar threads

  • · Replies 11 ·
Replies
11
Views
2K
  • · Replies 6 ·
Replies
6
Views
5K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
11
Views
3K
  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 7 ·
Replies
7
Views
4K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 0 ·
Replies
0
Views
2K
  • · Replies 28 ·
Replies
28
Views
4K