I am pretty new to using object/classes in PHP and I am curious about EXCEPTIONS, TRY, and CATCH
In the example below I have all 3 shown in use. Obviously an exception is some sort of way of triggering an error but I do not understand why? In the code below I could easily show some sort of error or something without the exception part there?
Below that example is an example using try and catch. It appears to me to be the same as using if/else. I may be wrong, this is just the way I see them without knowing anything, I realize you can code anything in PHP without using these so what is the reason, is there any benefit over using this stuff vs the traditional ways?
<?PHP
// sample of using an exception
if($something === $something_else){
//do stuff
}else if($something === $something_else_again){
//do stuff
}else{
throw new Exception('Something went wrong!');
}
try and catch
//and try and catch
try{
$thumb = PhpThumbFactory::create('/path/to/image.jpg');
}
catch (Exception $e){
// handle error here however you'd like
}
?>