Why Catching Throwable or Error is bad?

We often hear advice that catching Throwable or Error is bad practice and Java developer should avoid catching these, but have you thought Why? If the language allows you to catch anything which is an instance of java.lang.Throwable, then what is the problem of catching them or their subclass java.lang.Error? If they are bad, shouldn't Java itself has prohibited them from catching?  Well, this all looks good on theory, but not in real-world programming. As I said before in Java Exception best practices post, Ideally you should never catch Throwable or in particular Error. There are several reasons why catching an instance of java.lang.Throwable is a bad idea because in order to catch them you have to declare at your method signature e.g. public void doSomething() throws Throwable.

تعليقات

المشاركات الشائعة من هذه المدونة

Why use Underscore in Numbers from Java? Underscore in Numeric Literals Example

3 ways to convert String to Boolean in Java? Examples

Coursera's TensorFlow: Advanced Techniques Specialization Review [2022]