DEV Community

pO0q ๐Ÿฆ„
pO0q ๐Ÿฆ„

Posted on

PHP: why make your own exceptions?

Like many other programming languages, PHP allows throwing and catching exceptions.

If you surround your code with a try/catch block, you can handle exceptions very precisely in your app.

The how is quite straightforward, so do not hesitate to read the documentation to achieve it.

Here, I want to discuss the why and when you should use your own exceptions.

Why use exceptions anyway?

As a beginner, you might not see why exceptions can be useful, and even think that's not a great idea.

Why raise errors? Handling default values and behaviors should give a better experience to the end users when failures happen.

Pay attention to the words and their meanings. It's critical to understand the difference between failures and exceptions.

Exceptions are meant to handle unexpected situations and behaviors. You do want some failures to happen, so the idea is not to replace all error cases with an exception.

However, it's particularly useful when using third-party APIs and similar resources, as you don't control them.

If you maintain public APIs, raise exceptions early, especially when you receive wrong inputs. Don't let malformed payloads reach the heart of your app.

The built-in exception vs. your custom exception

In code, this would mean:

if (SOME_CONDITION) {
    throw new Exception('Error.');
}
Enter fullscreen mode Exit fullscreen mode

vs.:

class MyException extends Exception {}

if (SOME_CONDITION) {
    throw new MyException('My specific error happened.');
}
Enter fullscreen mode Exit fullscreen mode

The throw keyword is usually found within methods and functions (not like the above example) to force an error for a specific condition or when wrong inputs are sent to the app.

Then, developers can catch it with a try/catch block, just like with the basic built-in exception.

A good reason not to use the basic exception would be to provide a specific case other parts of the code can catch instead of the generic exception.

Catch them all?

You may catch more than one custom exceptions in PHP:

try {
   // success
} catch(MyException $e) {

} catch(MyOtherException $e) {

}
Enter fullscreen mode Exit fullscreen mode

However, don't mute them. For example, the following code would be considered as a bad practice:

  try {
        $PDO = new PDO( '...' );
  } catch (PDOException $e) {
        $this->log->error($e->getMessage());
  } 
Enter fullscreen mode Exit fullscreen mode

If your critical DB request fails, it's an emergency, and you want to know it, so let it cry.

Wrap up

Exceptions are great to handle processes and resources you don't control, like third-party vendors and APIs.

Making you own exceptions can be great for your maintenance and to keep your app consistent.

However, don't try to wrap all failures or catch bugs.

Top comments (1)

Collapse
 
sultanov profile image
Asrorbek Sultanov • Edited

What about this?

try {
// success
} catch(MyException|MyOtherException $e) {

}