PHP Exceptions

What is a PHP Exceptions case?

An error or strange thing that a PHP script does is described by an object called an exception.

Many PHP functions and classes throw exceptions.

Functions and classes made by the user can also throw exceptions.

Using exceptions is a good way to stop a function when it finds data it can\’t use.

Throwing an Exception

With the throw statement, an exception can be thrown from a user-defined function or method. When an exception is thrown, the code that comes after it won\’t run.

If you don\’t catch an exception, a fatal error with the message \”Uncaught Exception\” will happen.

Let\’s see what happens if we throw an exception and don\’t catch it:

Example

<!DOCTYPE html>
<html>
<body>

<?php
function divide($num, $divisor) {
if($divisor == 0) {
throw new Exception(\”Divide by zero\”);
}
return $num / $divisor;
}

echo divide(15, 0);
?>

</body>
</html>

Output

Fatal error: Uncaught Exception: Division by zero in C:\\coderazaa\\test.php:4
Stack trace: #0 C:\\coderazaa\\test.php(9):
divide(5, 0) #1 {main} thrown in C:\\coderazaa\\test.php on line 4

The Try-and-Catch Statement

We can avoid the error in the previous example by using the try…catch statement to catch errors and keep going with the process.

Syntax

try {

code that can throw exceptions

}

catch(Exception $var)

{

code that is run when an exceptions is found.

}

The catch block shows what kind of exception needs to be caught and what variable can be used to get to the exception. In the example above, the name of the variable is $e and the type of the error is Exception.

 

Example

<!DOCTYPE html>
<html>
<body>

<?php
function divide($num, $divisor) {
if($divisor == 0) {
throw new Exception(\”divide by zero\”);
}
return $num / $divisor;
}

try {
echo divide(15, 0);
} catch(Exception $var) {
echo \”Unable to divide.\”;
}
?>

</body>
</html>

Output

Unable to divide.

The try, catch, and finally Statement

Exceptions can be caught with the try…catch…finally statement. No matter if an exception was caught or not, the code in the finally block will always run. If finally is there, you don\’t have to use the catch block.

Syntax

try {
code that can throw exceptions
} catch(Exception $var) {
code that is run when an error is found
} finally {
code that is always executed regardless of whether or not an exception was caught
}

 

Example

<!DOCTYPE html>
<html>
<body>

<?php
function divide($num, $divisor) {
if($divisor == 0) {
throw new Exception(\”Divide by zero\”);
}
return $num / $divisor;
}

try {
echo divide(15, 0);
} catch(Exception $var) {
echo \”Unable to divide. \”;
} finally {
echo \”Process complete.\”;
}
?>

</body>
</html>

Output

 Unable to divide. Process complete.

Object of Exception

The Exception Object has details about the error or strange thing that happened when the function was run.
Syntax

new Exception (message, code, previous)

 

Parameter Values

message

Optional. A string describing why the exception was thrown

code

Optional. An integer that can be used used to easily distinguish this exception from others of the same type

previous

Not required. If this exception was thrown in a catch block of another exception, it is recommended to pass that exception into this parameter

Methods

When catching an exception, the following table shows some of the methods that can be used to get information about the exception:

getMessage()

Returns a string describing why the exception was thrown

getPrevious()

If this exception was caused by another one, this method gives back the first one. If not, then it returns null

getCode()

gives back the error code.

getFile()

gives back the full path to the file where the error happened.

getLine()

gives you the line number of the code that caused the error.

Related Article
50+ PHP Interview Questions and Answers 2023

1. Differentiate between static and dynamic websites. Static Website The content cannot be modified after the script is executed The Read more

All We Need to Know About PHP Ecommerce Development

  Many e-commerce sites let you search for products, show them off, and sell them online. The flood of money Read more

PHP Custom Web Development: How It Can Be Used, What Its Pros and Cons Are,

PHP is a scripting language that runs on the server. It uses server resources to process outputs. It is a Read more

PHP Tutorial

PHP Tutorial – Complete Guide for Beginners to Advanced Welcome to the most comprehensive PHP tutorial available online at PHPOnline.in Read more

Introduction of PHP

Introduction to PHP – Learn PHP from Scratch with Practical Examples Welcome to your complete beginner's guide to PHP. Whether Read more

Syntax Overview of PHP

Syntax Overview of PHP (2025 Edition) Welcome to phponline.in, your one-stop platform for mastering PHP. This comprehensive, SEO-rich tutorial on Read more

Environment Setup in PHP

Setting Up PHP Environment (Beginner’s Guide) If you’re planning to learn PHP or start developing websites using PHP, the first Read more

Variable Types in PHP

PHP Variable Types: Complete Beginner's Guide to PHP Data Types Welcome to phponline.in, your trusted source for beginner-to-advanced level PHP Read more

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments