Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is it called when I say "catch (Exception e) {}" in Java?

I am not sure about this answer. I cant find it anywhere. Is it the empty error handling?!

like image 894
Emily Myers Avatar asked Sep 19 '12 02:09

Emily Myers


People also ask

What does catch exception e do in Java?

Java try and catch The try statement allows you to define a block of code to be tested for errors while it is being executed. The catch statement allows you to define a block of code to be executed, if an error occurs in the try block.

What is E in catch exception e?

e is a reference to the instance of the Exception , like s would be a reference to an instance of String when you declare like String s = "..."; . Otherwise you won't be able to reference the exception and learn what's wrong with your code.

What is E in the catch statement catch ArithmeticException E?

'e' is just a parameter its means catch block can recieve an argument and the Data type of argument is exception datatype.

What is the type of error in catch block?

catch can only handle errors that occur in valid code. Such errors are called “runtime errors” or, sometimes, “exceptions”.


4 Answers

It is known as suppressing the exception, or swallowing the exception. May not be a very good practice, unless commented with a very good reason.

like image 160
Adeel Ansari Avatar answered Oct 13 '22 07:10

Adeel Ansari


We affectionately call this "eating the exception" at work. Basically, it means that something bad occurred, and we are burying our head in the sand and pretending it never happened. At the very least, a good practice is to have a logger.error(e) within that block:

try {
   // code here
}
catch (Exception e) { logger.error(e); }

so that you will have it recorded somewhere that an exception occurred.

like image 35
Philip Tenn Avatar answered Oct 13 '22 06:10

Philip Tenn


As far as I know, it's simply called an "empty catch clause" (or perhaps silent exception consumption), and it should generally be avoided (either handle the exception properly or don't try to catch it at all).

like image 35
arshajii Avatar answered Oct 13 '22 06:10

arshajii


This is generally called as ignoring an exception. Other terms used are Consuming an exception silently, Eating an exception etc

like image 2
Nivas Avatar answered Oct 13 '22 06:10

Nivas