-
-
Save Danack/049ba570e899a1f90468 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Okay, so we have this low-level library that is calling some code that handles numbers, and the functions it calls may generate PHP exceptions. | |
function lowLevelLibraryFunction() { | |
//Call bar1 that may throw CastException | |
//Call bar2 that may throw IntegerOutOfRangeException | |
//Call bar3 that may throw FloatOutOfRangeException | |
} | |
And in our business domain we have function foo() which calls highLevelLbiraryFunction() which calls mediumLevelLibraryFunction() cwhich alls lowLevelLibraryFunction(). | |
You either have to do this: | |
try { | |
foo(); | |
} | |
catch (CastException $ce) { throw new BusinessLogicException($ce) } | |
catch (IntegerOutOfRangeException $ioor) {throw new BusinessLogicException($ioor)} | |
catch (FloatOutOfRangeException $foor ) { throw new BusinessLogicException($foor) } | |
As it's only at the application level that you can turn a 'functional' exception into a 'business logic' or domain specific exception. And it's this which makes people catch \Exception rather than individual exceptions. | |
If all of the number casting/checking functions threw the same exception then the low-level function would look like: | |
function lowLevelLibraryFunction() { | |
//Call bar1 that may throw NumberException | |
//Call bar2 that may throw NumberException | |
//Call bar3 that may throw NumberException | |
} | |
And you could just this in your application level: | |
try { | |
foo(); | |
} | |
catch (NumberException $ce) { | |
throw new BusinessLogicException($ce) | |
} | |
If you really wanted to have specific errors, you are still able to do so by catering for them in your lowLevel function even with a more generic exception being thrown. | |
function lowLevelLibraryFunction() { | |
try { | |
//Call bar1 that may throw NumberException | |
} | |
catch(NumberException $e) { | |
throw new Bar1SpecificException | |
} | |
try { | |
//Call bar2 that may throw NumberException | |
} | |
catch(NumberException $e) { | |
throw new Bar2SpecificException | |
} | |
try { | |
//Call bar3 that may throw NumberException | |
} | |
catch(NumberException $e) { | |
throw new Bar3SpecificException | |
} | |
} | |
And yes this is why I agree with Levi, having to catch exceptions is not as nice as being able to just check if the casting is going to work or not. | |
function lowLevelLibraryFunction() { | |
if (CastableToInt($x) == false) { | |
throw new Bar1SpecificException | |
} | |
if (inIntNumberRange($y) == false) { | |
throw new Bar2SpecificException(); | |
} | |
if (CastableToFloat($z) == false) { | |
throw new Bar3SpecificException | |
} | |
//Numbers are safe. | |
bar1($x); | |
bar2($y); | |
bar3($z) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment