目錄表

, ,

Effective Java - Use checked exceptions for recoverable conditions and runtime exceptions for programming errors

Introduction

這個Item在強調例外的使用時機,包含

下一個問題就是怎麼定義recoverable conditions與programming errors。我分享笑談軟體工程:例外處理設計的逆襲中的觀點:

根據上面的定義,就可以知道什麼時候該拋checked exception,什麼時候該拋unchecked exception了。對應到REST API的回應結果,unchecked exception預設通常都會是5xx錯誤,而checked exception就需要交給client去識別,是屬於需要user介入處理的4xx錯誤,還是server暫時的5xx錯誤。

除此之外,這個Item還有提到幾個設計要注意的事情:

Note

Effective Java第三版Item 70。

Reference