Doubt about Exceptions

Asked

Viewed 35 times

0

I’m taking a course in PHP online, and I’m in a video class that talks about handling errors, in this video class were created some custom errors, and after created the code was executed so that these errors occurred to demonstrate the generated message. My doubt is as to the display of this error message that was generated, in the video class I am instructed to use:

catch(Exception $e){ $e->getMessage()}

But using this way, I do not receive any message, and in the video class correctly appears the custom error message, as well as other details about the error, as the line that the error occurs, for example.

The only way for the custom error I created to display your message is by using:

catch(Exception $e){echo $e->getMessage()}

this way is displayed the custom error message I created, but only it, does not show other details of the error as displayed in the video class.

Is this what’s happening normal? I find it strange because my code is exactly the same as the teacher’s and this difference is occurring in the display of errors.

  • Error and exceptions in php are different things, capture the more Enerica Exception is not very good usually.

  • Yes, it is normal. First, the fact of the need for instruction echo is that effectively the method getMessage returns the error message, does not actually print it. As for the other messages, it would be necessary to know what they are. I can’t remember if the settings error_reporting affects this type of output.

  • the way I used to get the message, giving echo in $e->getMessage() is correct? is it the best thing to do? I thought maybe because the video guy used an older version of PHP, and I used 7.0, these things have changed...

  • Yes, this $e->getMessage() will only appear on your screen if you echo it yourself. But in production environment instead of echo on the screen is more interesting to save in log, because this message to the average user does not help. I would also put a semicolon at the end of the instruction, after the second parenthesis of getMessage();

  • ah that good then, thanks for helping, and on the semicolon I put yes, but forgot to add here in the post

  • It is possible to assign more than one exception to a block try as well as custom codes and in the block(s)) catch check the code to find out if you should or should not provide some return.

Show 1 more comment
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.