Should I throw different types of exceptions?

With an exceptional choice of exceptions, I always used only the PHP basic Exception class, only with a different message, for example:

<?php if($a < $b){ throw new Exception('a is smaller than b'); }elseif($a > $b){ throw new Exception('a is larger than b'); } 

However, I recently noticed that some people throw different classes of exceptions, for example:

 <?php if($a < $b){ throw new aIsSmallerThanBException(); }elseif($a > $b){ throw new aIsLargerThanBException(); } 

What are the advantages / disadvantages of these two methods? Should I do it in the second style? I noticed that during unit testing with PHPUnit you can say that you expect a specific exception class using setExpectedException (). Does this mean that the second method is better in terms of code verification?

Thanks!

+4
source share
3 answers

These different exceptions can ease the flow, since you are allowed to catch several types of exceptions at the same time. Take a look at this:

 function mustBeEqual($a, $b){ if($a < $b){ throw new aIsSmallerThanBException()('a is smaller than b'); }elseif($a > $b){ throw new aIsLargerThanBException()('a is larger than b'); } else { //Everything is ok, proceed. } } try { mustBeEqual(3, 4); } catch (isLargerThanBException $e){ echo "A is greater than B" ; } catch (isSmallerThanBException $e){ echo "A is smaller than B" ; } catch (Exception e){ echo "Default exception was triggered: " . $e->getMessage() ; } 

At first you expect some special exceptions for catch, then you use the default exception to make sure you catch it. Each particular exception is a derived class of Exception and inherits methods such as getCode and getMessage .

I can also give an example of how this works:

  • A function call in a try-catch block. The function throws an exception.
  • Try to catch ConnectionTimeoutException . This type is not an exception. Go to the next catch .
  • Try to catch an AccessForbiddenException . The exception is this type. Report this issue.
  • We do not proceed to the next catch because an exception has already been found. It is he.
+2
source

Regarding the ability to check the code, you can use the second. The advantage is that you can use this exception in any script when that exception exists. If the first one, you should declare a new exception like this throw new Exception('....'); every time you want to use an exception.

+1
source

This is a matter of selectivity. Different exception classes can be handled by different catch , so you can run specific code depending on what caused the exception. This can be especially important if you have multiple nested levels of try-catch blocks.

Another thing is code readability and easy debugging. OutOfBoundsException tells the debugger something that is not in a normal Exception . SPL defines a bunch of useful exception classes that cover the most common problems. Using these SPL exceptions, you clearly state why the exception was thrown. Another person is just looking for a suitable php.net page.

+1
source

Source: https://habr.com/ru/post/1502368/


All Articles