Exceptions in PHP

JanTvrdik 1,766 views 20 slides Feb 12, 2013
Slide 1
Slide 1 of 20
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20

About This Presentation

Best practices for using exceptions in PHP.
How to design exception hierarchies.
Difference between runtime and usage (logic) error.


Slide Content

Jan Tvrdík

try {
...
if (!$ok) {
throw new FooException();
}

} catch (FooException $e) {
...
}

try {
...
if (!$ok) {
throw new FooException();
}

} catch (FooException $e) {
...
} catch (BarException $e) {
...
}

try {
...
if (!$ok) {
throw new FooException();
}

} catch (FooException $e) {
...
} finally {
...
}

try
catch finally
Was there
an exception thrown?
Is there a corresponding
catch block?
Yes
Yes
No
No

try {
$this->db->lock('users');
$this->db->query(...);
$this->db->unlock();

// intentionally catch all exceptions
} catch (Exception $e) {
$this->db->unlock();
throw $e;
}

try {
$this->db->lock('users');
$this->db->query(...);
$this->db->unlock();

// intentionally catch all exceptions
} catch (Exception $e) {
$this->db->unlock();
throw $e;
}

try {
$this->db->lock('users');
$this->db->query(...);

} finally {
$this->db->unlock();
}

try {
return 1;

} finally {
return 2;
}

constructor parameters
string $message = ""
int $code = 0
Exception $previous = NULL
methods
string getMessage()
Exception getPrevious()
mixed getCode()
string getFile()
int getLine()
array getTrace()

usage error
runtime error
System failure

can be avoided
should directly lead to a fix
message is more important than type
very important in libraries
should not be catched
examples:
wrong argument type / value
calling method in incorrect order
creating an instance of static class
calling not implemented method

can NOT be avoided
type is more important than message
should be catched
often related to thread safety
examples:
file / directory / database entry not found
duplicate entry in database

Exception
ErrorException

LogicException (for usage errors)
InvalidArgumentException
RuntimeException (for runtime errors)

Exception
LogicException
App\LogicException
App\InvalidArgumentException
App\NotImplementedException
App\StaticClassException
RuntimeException

Exception
LogicException

RuntimeException
App\RuntimeException
App\IOException
App\FileNotFoundException
App\DirectoryNotFoundException
App\EntryNotFoundException
App\DuplicateEntryException

throw only exceptions from your namespace
usage error
write nice ex. message for human developer
runtime error
use very specific type
(often requires creating a new one)
message is often useless
phpDoc
Always specify all runtime exceptions
@throw annotation is part of API

never display ex. message to the user
catch as specific types as possible
read and respect @throw annotations
use $previous when rethrowing ex.

Twitter + GitHub
@JanTvrdik

nette.merxes.cz