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

放肆的年华 提交于 2019-12-07 06:08:43

问题


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


回答1:


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.




回答2:


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.




回答3:


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).




回答4:


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




回答5:


It's called "broken code".

(if you want to ignore an exception, then clearly document the reason.)




回答6:


From what i know, this means that in fancy talk "Eating the exception", but simply it just stops this specific error from stoping java while its running your code. Also the "e" part in the

    catch (Exception *e*) {}

is the "name" for the error object.

if you're still unsure about whats that then read this.




回答7:


I call it "exception masking" and it is not good style. It is best to catch specific exceptions or let them "bubble up". Masking exceptions will come back to bite you. It is a good idea to have the exceptions bubble up to be appropriately handled. If the exception "bubbles to the top", a proactive exception handler can be developed to notify the developer or organization that an unexpected exception has occurred.



来源:https://stackoverflow.com/questions/12487583/what-is-it-called-when-i-say-catch-exception-e-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!