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

michaelmugo
michaelmugo
3,971 Points

Is $e an object created from the Exception class?

If it is, how come it is not declared as:

$e = new Exception();

1 Answer

Michaelmugo,

Here's PHP's manual on exceptions which explains things a little more thoroughly. The $e instance is created when you reach the catch block:

You are actually instantiating the Exception class when you throw the exception:

if($a != $b) {
   throw new Exception("Variables are not equal");
}
catch(Exception $e){
   $error = $e->getMessage();
}

Once you get to the catch block you are creating $e and identifying it as class type Exception. You can then use $e like you would any other way you would use classes. Also, you can also have multiple catch blocks per situation, create your own exception class, and use the keyword 'finally' when dealing with exceptions,. I would click the link and read the documentation for more information. Hope this helps.

Cheers!