When is a catch clause empty [duplicate]

I saw lots of questions and explanations regarding empty catch clauses but I was never actually sure what “empty” means.

Is a catch clause like this empty? It clearly isn’t empty for the eye because there is one line of code. But the catch is not really handled. Instead the error is just logged.

try {
    // do some work
} catch (SomeException e) {
    logger.error("Some error", e);
}

I see this fairly often because there are a lot exceptions where you just can’t do something about it if it fails. The consequence is that the error is not fully swallowed but at least logged.

So the question is: Is this bad code if you “just” log the error and don’t do anything about?

0

This is an empty catch:

 catch (SomeException e) {
 }

It is a very bad idea, because you’re silently ignoring the exception. And even if that is the actual intention, then you should have at least a comment in there to tell future-you (or any other developer) why it should be empty.

Arguably, this one is still empty:

 catch (SomeException e) {
   // nothing to do because we frobnicated the foo before
 }

But at least this time we know that some thought went into it and the author thought that a do-nothing catch block is correct here.

Anything that adds logging or other exception handling is not “empty” in any meaningful way.

Whether or not some handling/logging code is sufficient at any given place, is an entirely different question: sometimes simply logging the exception is the right thing, at other times more drastic steps must be taken.

7

I guess it depends whether the exception is checked or is a runtime exception.

The idea behind checked exceptions (the ones you are required to “catch”) is that they are errors which the application can recover from. So when caught, you should ideally provide a logic to recover from these exceptions. While you may rethrow them up the call heirarchy, it is best to handle them as quickly as possible (never let your clients do what you can do for your clients).

Runtime exceptions on the other hand, are normally not recoverable. They leave the application in a bad state. More often than not, the only way to handle these exceptions is to log as much information to aid your investigation. Typical applications would have “catch-all” block to handle all runtime exceptions.

I think one would have to argue that it will not often be good code – its rather better than swallowing the error completely (or at least its better than doing so without clear explanation).

The basic problem is that in the same way that the code never throws, in my experience no-one will look at the logs unless there is an issue (or until the issue that its been hiding is uncovered by other means). I speak from practical experience as I have code that’s not dissimilar to the above.

That said if you’re catching and swallowing specific exception (as the code implies) and you explain why you’re doing so then it may be perfectly reasonable.

What is not reasonable is a generic catch-all that does that – because then the you’re getting into unforeseen consequences i.e. whilst it will swallow the trivial not critical error its there to deal with it will also swallow the show-stopper that’s gently corrupting your data…

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật