Why are Pokemon Exceptions considered code smell?

  • Thread starter Thread starter SlurrerOfSpeech
  • Start date Start date
  • Tags Tags
    Code Smell
AI Thread Summary
Catching all exceptions in code, often referred to as "Pokemon exceptions," is considered a code smell because it can lead to poor error handling and obscure the actual issues that arise. While it may seem practical to catch all exceptions from a third-party library without documentation, this approach can result in ignoring specific exceptions that require different handling. Java's distinction between checked and unchecked exceptions highlights the importance of managing exceptions effectively, as unchecked exceptions allow for cleaner code without excessive checks. Best practices suggest wrapping lower-level exceptions in more meaningful higher-level exceptions to avoid polluting higher-level classes with implementation details. Ultimately, while general exception handling can simplify code, it risks masking underlying problems that should be addressed directly.
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 jedishrfu
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...
Back
Top