Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do good programmers sometimes silently swallow exceptions? [closed]

I know it's evil, but I've seen swallowed exceptions in code written by a good programmer. So I'm wondering if this bad practice could have at least one positive point.

In other words, it is bad but why do good programmers, on rare occasions, use it?

try {     //Some code } catch(Exception){} 
like image 589
JiBéDoublevé Avatar asked Jul 26 '10 13:07

JiBéDoublevé


People also ask

Why do programmers want to handle exceptions?

Many programmers solve this problem by simply ignoring it — errors are reported when their programs crash. Exceptions enable you to write the main flow of your code and to deal with the exceptional cases elsewhere.

What is swallowing exception?

In computer programming, error hiding (or error swallowing) is the practice of catching an error or exception, and then continuing without logging, processing, or reporting the error to other parts of the software. Handling errors in this manner is considered bad practice and an anti-pattern in computer programming.

What is the point of catching exceptions?

Exceptions are your way of letting your consumers know that something went wrong that you can't properly recover from. You're giving them the chance to either correct the issue, log the error, or pass the Exception up the chain until something useful can be done with it.

When should you trap for exceptions?

You should catch the exception when you are in the method that knows what to do. For example, forget about how it actually works for the moment, let's say you are writing a library for opening and reading files. Here, the programmer knows what to do, so they catch the exception and handle it.

Are swallowed exceptions a thing of the past?

Let’s make swallowed exceptions a thing of the past. No more empty catch blocks, and no more unknown errors. Does the scenario we described sounds familiar? Did you have other experiences with swallowed exceptions? Let us know in the comments section below! Alex is the Director of Product Marketing at OverOps.

Are You swallowing exceptions without any trace?

An empty block. Swallowing the exception without any trace. And looks like it even happens at least as often as logging it. That’s… quite alarming to say the least. Boom! Busted. Swallowed exceptions are a major factor that’s causing errors to go unnoticed. We found our killer.

What do developers do in exception catch blocks?

Documenting what happened, through logging, printing a stack trace or printing out information to the console. Rethrowing an exception, probably a wider abstraction that one of the methods further up the call stack would know how to handle. And….. Unfortunately…. Nothing. An empty block.

Why do so many exceptions go unnoticed?

Busted. Swallowed exceptions are a major factor that’s causing errors to go unnoticed. We found our killer. Instead of doing the right thing by documenting exceptions with a logged error or warning, sometimes developers choose to ignore them.


1 Answers

Looking in my own code, I found a place in my logging code where, after failing to write to a file and failing to write to the event log, it swallows the error as there is no place left to report it. That's one example: there aren't many others.

like image 80
Steven Sudit Avatar answered Sep 20 '22 13:09

Steven Sudit