Java Style: Properly handling exceptions

后端 未结 5 1481
孤独总比滥情好
孤独总比滥情好 2020-12-13 08:09

I keep getting stuck conceptually on deciding an Exception-handling structure for my project.

Suppose you have, as an example:

public abstract class          


        
5条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-13 08:42

    Use Runtime Exceptions, combined with an exploding catch-all at the top. It's a bit scary at first, but gets better once you get used to it.

    In my web applications, everything thrown is Runtime. There are almost no "throws" clauses, and I only have catchblocks in places I really can (or want) to handle the exception. At the highest level, there is a catch Throwable which renders a technicall error page, and writes a log entry.

    A Log4J mailer sends me the log entry and 10 log entries preceding it. So when the client calls, I usually already know that there was a problem.

    With proper (unit)testing and clean programming, the added cleanliness and readability outweighs the loss of checked exceptions anytime.

提交回复
热议问题