Conceptually in Java there are two types of exceptions:
- checked exceptions
- unchecked exceptions
These are used to indicate different things. A checked exception is a special condition which may happen and you are forced to handle this. A FileNotFoundException
for example is a situation which may arise (for example you are trying to load a file that doesn't exist yet).
In this situation these are checked because your program is supposed to handle them.
On the opposite side an unchecked exception is a situation which shouldn't normally occur during the execution of a program, a NullPointerException
means that you are trying to access a null
object, which shouldn't never happen. So these exceptions are more likely bugs in software that may arise everywhere, you are not forced to declare what's throwing them and handling them is optional according to the requirements.
By following your bike analogy it's like having a FlatTireException
on your Bike
class. It could be a checked exception because it's a situation which may arise and which should be handled, while a WheelMissingException
is something that shouldn't happen.