---
title: "Exception Handling in PHP"  
description: "Exceptions are important to control over error handling Exception handlings are used to change the execution of a code if a specified error occurs.Her"  
author: "Royce Roy"  
published: 2016-06-10  
updated: 2018-03-15  
canonical: https://www.mindstick.com/blog/11151/exception-handling-in-php  
category: "php"  
tags: ["exception handling", "php"]  
reading_time: 2 minutes  

---

# Exception Handling in PHP

[Exceptions](https://www.mindstick.com/interview/22871/define-predifined-generic-exceptions) are important to control over **[error handling](https://www.mindstick.com/blog/11149/error-handling-in-php).** Exception handlings are used to change the [execution](https://www.mindstick.com/blog/178/synchronous-and-asynchronous-command-execution-in-c-sharp-dot-net) of a code if a specified error occurs.

**Here are some keywords related to [exception handling](https://www.mindstick.com/articles/12240/introduction-of-exception-handling):**

**·** **Try:** A [function](https://www.mindstick.com/articles/13001/multi-statement-table-valued-user-defined-function-in-sql-server) which is using an exception must be in a ‘try’ block. If the exception is triggered, an exception is thrown, otherwise if exception does not trigged, the code will continue its execution.

**·** **Throw:** This is how you trigger an exception. Each “throw” must have at least one catch.

**·** **Catch-** A [catch block](https://www.mindstick.com/forum/159348/how-does-an-exception-find-the-correct-catch-block) retrieves an exception and creates an object containing the information about exception.

```
<?php
// function is created with an exceptionfunction check($num) {  if($num>2) {    throw new Exception("Value must be 2 or below");  }  return true;}
// exception is triggered  in a "try" blocktry {  check (3);  //If the exception is thrown, this text will not be shown  echo ' the number is 2 or below';}
//catch exception
catch(Exception $e) {  echo 'Message: ' .$e->getMessage();}
?>
```

**Output:**

Message: Value must be 1 or below

Example explained

· The check() function is created. It checks whether a number is greater than 2.If it is, than an exception is thrown.

· The check() function is called in a “try” block.

· The exception within check() function is thrown.

· After exception is thrown the catch block retrieves the exception and creates an object ($e) containing the information about exception.

· The [error message](https://www.mindstick.com/forum/23174/error-message-the-page-you-are-requesting-cannot-be-served-because-of-the-extension-configuration) is displayed by calling $e->getMessage() from the exception object.

##### Creating a custom Exception class in PHP

You can create your own [custom exception](https://www.mindstick.com/forum/159237/how-to-create-a-custom-exception-in-java-and-when-would-to-use-it) handler. To create a custom exception handler you must create a special class function. This function is called when an [exception occurs](https://www.mindstick.com/forum/159341/how-to-use-try-and-catch-in-java-for-exception-handling-and-what-happens-when-an-exception-occurs) in PHP. The class must be an [extension](https://www.mindstick.com/articles/22/extension-method) of the exception class. Use following function to set a user-defined exception handler function:

```
string set_exception_handler ( callback $exception_handler )
```

When uncaught exception occurs than exception_handler (the name of the function) is called.

```
<?php
   function exception_handler($error) {      echo "An uncaught exception: " , $error->getMessage(), "\n";   }  set_exception_handler('exception_handler');   throw new Exception('Uncaught Exception');   echo “This is not Executed\n";
?>
```

\

---

Original Source: https://www.mindstick.com/blog/11151/exception-handling-in-php

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
