Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between throws Throwable and throws Exception

Tags:

java

Any difference about those two?

like image 440
user496949 Avatar asked Mar 18 '26 21:03

user496949


1 Answers

Exceptions are for errors in the program logic. Error are used by the JVM to signal that something is wrong with the environment such as an OutOfMemoryError or IncompatibleClassChangeError. ThreadDeath is used to kill Threads. Throwable is a super class over all of these.

In normal program logic, you should never throw nor catch Throwables or Errors. The main reason I can think of for catching a Errors is this: You are using your own class loading system and want to isolate broken plugins.

The JavaDoc for ThreadDeath for example says:

An application should catch instances of this class only if it must clean up after being terminated asynchronously. If ThreadDeath is caught by a method, it is important that it be rethrown so that the thread actually dies.

like image 51
Hendrik Brummermann Avatar answered Mar 21 '26 10:03

Hendrik Brummermann



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!