What are the best practices for catching and re-throwing exceptions?

前端 未结 4 1434
旧时难觅i
旧时难觅i 2020-12-04 04:50

Should caught exceptions be re-thrown directly, or should they be wrapped around a new exception?

That is, should I do this:

try {
  $connect = new C         


        
4条回答
  •  余生分开走
    2020-12-04 05:25

    You usually think of it this way.

    A class might throw many types of exceptions that will not match. So you create an exception class for that class or type of class and throw that.

    So the code that uses the class only has to catch one type of exception.

提交回复
热议问题