1. TL;DR: What happened?
I mess up all the time when I code, I’ve always wanted to do a more rigorous session on exceptions and error handling between I want to compare exception handling in three languages, P
2. Resources
- Some best practices for handling exceptions in JAVA.
- Modern C++ best practices for exceptions and error handling
- Great StackOverflow question about Java vs Python
3. Overview of Throwable and Exception Class Hierarchies from Python and Java
In Java, there is a difference between Exception
and Error
, as they are taken to mean different things.
- Exception: User program should catch. It is further subdivided into two kinds
- Checked Exception: The compiler will force you to handle the exception, or else the program will not compile. Checked exceptions is intended to represent problems outside the control of the program.
- Unchecked Exception: The compiler will not force you to the handle the exception. Unchecked exceptions is intended to cover programming problems. As such, they are not document in a method’s API, since there are innumerable ways in which one can cause an unchecked exception. The official Java documentation specifically warns against throwing unchecked exceptions because of laziness for specifying the correct checked exception.
- Error: User program should not catch. Thrown by JVM.
Assertion
,StackOverflow
, etc.
- In Python, Error and Exception do not carry the same meaning as Java. For example,
StackOverflowError
in Python is, as seen above, a sub-class ofRuntimeError
, which is itself a sub-class ofException
. - Classes which are derived from
BaseException
but notException
all relate to some kind of intented stop/calling ofexit(), stop()
.
Best practices compared
Try… Catch… Finally Usage
Creating Your Own Exception Class
The Python documentation warns against exception BaseException
. Rather, Exceptions should be extending Exception
.
Under Construction