Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP Object-Oriented PHP Basics (Retired) Inheritance, Interfaces, and Exceptions Managing Exceptions

JAKZ AIZZAT
JAKZ AIZZAT
7,813 Points

Managing Exception

Can anyone explain to what's this exception ? Seriously I dont understand about this. I dont know how to use this code?What's function of doing this and How to use it. I have done read exception in php.net but I'm still dont understand about it.

1 Answer

Andrew McCormick
Andrew McCormick
17,730 Points

Jakz, Basically if an error is thrown you want to handle it gracefully. PHP has created ways to somewhat handle the errors. Rather than putting your code into an infinite loop or having your machine catch on fire, it politely displays stuff like the error number, a snippet of where the error is, and some other useful info to debug the error. So basically if you have someone using your website and something doesn't work like you intended when you wrote the code you don't want the user to see a bunch of gobbly glop that exposes your code as well as file names for you php files. Not only would it be bad for your customer that paid you money to write the application, it could be a security vulnerability. So what you are doing by managing exceptions is creating custom ways to handle those errors. Maybe if an error is thrown you want your application to skip over the next block of code because you know it won't work. Maybe you want to display a friendly message and apologize for the error. Whatever you want, you can do. You first try your code and then catch any exceptions that might be thrown. This lets your application flow nice and smoothly with no angry users or customers and with no calls to fire department because of burning computers.

does that help a little?

w3schools Exception handling. (typically more down to earth than php.net explanations)

how to handle errors in OOP

Hi Andrew,

You have "w3c" in your anchor text. The w3c isn't affiliated with w3schools.

Andrew McCormick
Andrew McCormick
17,730 Points

ha. I really need to read over stuff after I type it. thanks.

JAKZ AIZZAT
JAKZ AIZZAT
7,813 Points

So,we need to use this for backup if any error occur?

Can you give me simple code about exception so I can try to understand how its work.