• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
The Electric Toolbox Blog

The Electric Toolbox Blog

Linux, Apache, Nginx, MySQL, Javascript and PHP articles

  • Applications
  • FCKEditor
  • Apache
  • Windows
  • Contact Us
Home / Catch mutiple exception types with PHP

Catch mutiple exception types with PHP

PHP’s try..catch can be used to catch multiple exception types. If the try block could cause one of several different exceptions they can each be handled separately with their own catch section.

Example exceptions

Here’s some example exceptions that have been defined for the purposes of this example:

class FooException extends Exception {
    public function __construct($message = null, $code = 0) {
        // do something
    }
}

class BarException extends Exception {
    public function __construct($message = null, $code = 0) {
        // do something
    }
}

class BazException extends Exception {
    public function __construct($message = null, $code = 0) {
        // do something
    }
}

Handling multiple exceptions

It’s very simple – there can be a catch block for each exception type that can be thrown:

try {
    // some code that might trigger a Foo/Bar/Baz/Exception
}
catch(FooException $e) {
    // we caught a foo exception
}
catch(BarException $e) {
    // we caught a bar exception
}
catch(BazException $e) {
    // we caught a baz exception
}
catch(Exception $e) {
    // we caught a normal exception
    // or an exception that wasn't handled by any of the above
}

If an exception is thrown that is not handled by any of the other catch statements it will be handled by the catch(Exception $e) block. It does not necessarily have to be the last one.

Exception Series

This is the six post in a weekly series of seven about PHP exceptions. Read the previous post "Extend PHP’s exception object", the last post in the series "Exiting from within a PHP exception" and use the links below to subscribe to my RSS feed, by email, or follow me on Twitter or Facebook to keep up to date with my daily postings.

Check Out These Related posts:

  1. PHP Exceptions – available information
  2. Catch uncaught exceptions with PHP
  3. Replace error reporting with exception handlers with PHP
  4. Extend PHP’s exception object

Filed Under: PHP

Primary Sidebar

Categories

  • Apache
  • Applications
  • Article
  • Case Studies
  • Email Servers
  • FCKEditor
  • HTML And CSS
  • Javascript
  • Linux/Unix/BSD
  • Microsoft SQL Server
  • Miscellaneous Postings
  • MySql
  • Networking
  • Nginx Web Server
  • Offsite Articles
  • OSX
  • PHP
  • Quick Tips
  • RFC – Request for Comments
  • SilverStripe
  • VMWare
  • VPN
  • Windows
  • WordPress

Recent Posts

  • Vim Show Line Numbers
  • Add User To Group Linux
  • Chmod 777 Tutorial
  • How to Copy Directory Linux
  • Linux create user

Copyright © 2021. ElectricToolBox. All Rights Reserved.

  • Contact Us
  • Copyright Info
  • Privacy Policy
  • Sitemap