News for hackers. Stuff that matters.
Re:Ask HN: Why catching specific exceptions is better? by caprock on Friday July 12, 2024 @12:54PM (#40947281)
There are two things which helped me understand this better.

1. Exception handling is a form of flow control

Once you understand exceptions are a normal part of program flow, you begin to see how they fit into the broader logic. They help encapsulate and guide handling of common error cases.

2. There's no need to be over-granular

It's not necessary to go overboard and capture every single possible exception. You can explicitly capture the most common exceptions, log them, and handle/cleanup appropriately. Then you can have a fallback global trap to log and catch any others. If you start to see something unexpected a lot, then you can add an explicit handler for it if needed. It's mostly just about having specific cleanup and good logging for awareness of runtime issues.

There are no comments on this comment yet.