What is an exception in PHP and what is an attempt and catch?

I'm new to using objects / classes in PHP, and I'm curious about EXCEPTIONS , TRY and CATCH

In the example below, I have all 3 shown in use. Obviously, an exception is a kind of way to throw an error, but I don't understand why? In the code below, could I easily show some kind of error or something without exception there?

The following is an example of using try and catch. It seems to me that I am using if / else. Perhaps I am mistaken, this is the way I see them without knowing anything, I understand that you can encode anything in PHP without using them, which is the reason there is an advantage in using this material compared to traditional methods ?

<?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!');
}

//and try and catch
try{
    $thumb = PhpThumbFactory::create('/path/to/image.jpg');
}
catch (Exception $e){
    // handle error here however you'd like
}
?>
+5
2

, - " , " ( wikipedia)


( ) :

, , " php"


( ):

  • "" try
  • " " catch
  • , " " ""
  • " "
+5

- "" . , , ( try). , , , , . .

+2

All Articles