Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unchecked exceptions in Java: Inherit from Error or RuntimeException?

Tags:

java

exception

I would like to handle errors with (unchecked) exceptions. I heared that for each kind of exception I should create a subclass of either Error or RuntimeException. What's the difference?

like image 946
snakile Avatar asked Aug 05 '26 04:08

snakile


1 Answers

Errors should identify programmatically unrecoverable problems (e.g. out of memory). Exceptions should identify programmatically recoverable problems which are caused by unexpected conditions outside control of code (e.g. database down). RuntimeExceptions should identify programmatically recoverable problems which are caused by faults in code flow (read: developer's faults such as null pointer, illegal argument, etc).

In your case you want to inherit from RuntimeException.

like image 125
BalusC Avatar answered Aug 06 '26 17:08

BalusC