June 2017
Intermediate to advanced
536 pages
9h 49m
English
By extending the built-in Exception class, PHP lets us throw any object as if it were an exception. Let's take a look at the following example:
<?php class UsernameException extends Exception {} class PasswordException extends Exception {} $username = 'john'; $password = ''; try { if (empty($username)) { throw new UsernameException(); } if (empty($password)) { throw new PasswordException(); } throw new Exception(); } catch (UsernameException $e) { echo 'Caught UsernameException.'; } catch (PasswordException $e) { echo 'Caught PasswordException.'; } catch (Exception $e) { echo 'Caught Exception.'; } finally { echo 'Finally.'; }
Here, we defined two custom exceptions, UsernameException and PasswordException ...
Read now
Unlock full access