Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
What is a PHP exception?
3.
Keywords in PHP exceptions handling
4.
Advantages of PHP exception handling
5.
What happens if a thrown PHP exception is not handled well?
6.
The syntax for the try-catch statement 
7.
What is a PHP Exception Object?
8.
What are some of the methods used in PHP exceptions?
9.
FAQs
10.
Key takeaway
Last Updated: Mar 27, 2024

PHP exceptions

Author Ranjul Arumadi
2 upvotes

Introduction

Errors are always something that a programmer dreads. Sometimes these errors may be hard to find as illogical conditions may cause them. For us to deal with these errors easily, exception handling is used. PHP exceptions are certain objects that can describe the errors in a PHP program. They contain descriptions of these errors, making it easy to deal with them. This blog talks about various concepts related to PHP exceptions.

What is a PHP exception?

PHP exceptions are objects thrown by PHP functions and PHP classes when an error or a strange behavior occurs in a program. These objects describe the errors that have occurred. These objects can also be thrown in as a user-defined function and class. The PHP exception provides a better way to deal with errors and data not authorized to be accessed. 

Keywords in PHP exceptions handling

The exception handling in PHP is similar to other programming languages like Java.

Some of the keywords used in handling PHP exceptions are

1. try:

Try statement represents a piece of code that can create an error. That is, the code in the try statement can generate an exception.

 

2. catch:

This statement is present after the try statement. When an exception gets created inside the try block, then the code immediately shifts to the catch statement as it is the catch statement that contains the information on how to deal with an exception.

 

3. throw:

This statement is used to throw an exception. The throw statement allows a user-defined function or method to throw an exception. If a block does not handle certain exceptions, it gets thrown.

 

4. finally:

Finally is a statement that is present after the catch block. It gets executed whether or not an exception has occurred. It does the cleanup activity in the PHP code.

Advantages of PHP exception handling

Apart from making it easier to deal with errors, exception handling in PHP performs other functions. Let us see about those functions.

  • It helps to separate the error handling code from the normal code. A separate code section deals with exceptions created when exception handling is used. 
  • In the absence of exception handling, each program section will have to contain code that deals with errors and exceptions. 
  • When exception handling is done, the control is transferred to the error correction code whenever an exception gets created. 
  • If an exception is not created, the error correction part is not accessed. PHP exception handling also performs the grouping of errors based on the type and objects thrown.

What happens if a thrown PHP exception is not handled well?

When an exception is thrown, the code searches for the catch statement that can handle the exception. If the exception does not get caught, an error will be issued with an "Uncaught Exception” message. The uncaught exception error message pops up if no set_exception_handler() is defined.

The syntax for the try-catch statement 

The try...catch statement is used to catch exceptions and continue the process. Let us see its syntax.

try {
 // Code that contains an exceptions
} catch(Exception $e) {
 // Code that can deal with and rectify a thrown exception.
}
You can also try this code with Online PHP Compiler
Run Code

 

Example code:

try {
   print "Inside try block";
   throw new Exception();
} catch (Exception $e) {
   print "Inside catch statement";
} finally {
   print "Always executed even when no exception is thrown";
}
You can also try this code with Online PHP Compiler
Run Code

 

Output:

Inside try block
Inside catch statement
Always executed even when no exception is thrown

 

In the above example, the created error is stored in the variable $e and is thrown. The catch block gets executed only when an exception has occurred. The try and finally block is always executed. The sequence of execution is. First, the try block is executed. If no exception gets thrown, the catch block gets skipped, and the finally block is executed. If an exception has occurred, the catch block is executed, followed by the finally block.

What is a PHP Exception Object?

An exception object is an object that gets created when exceptions are created, and it contains details on the error that has occurred in the program.

 

Syntax:

new Exception(message, code, previous)
You can also try this code with Online PHP Compiler
Run Code

 

Explanation of the parameters:

Parameters

Description

message The 'message' parameter is optional, and it contains a string that contains the description of the exception in holds.
code The 'code' parameter is optional, and it contains an integer that can differentiate one exception from the other.
previous The 'previous' parameter is optional. If an exception gets thrown in the catch block of another exception, the 'previous' parameter asks to pass that exception to it.

What are some of the methods used in PHP exceptions?

Some of the methods used in PHP exceptions are:

Methods

Description

getMessage() This method returns a string that describes the reason for throwing an exception.
getPrevious() If another exception triggers an exception, this method returns the previous exception. In the absence of such a condition, it returns null.
getCode() This method returns the code of the exception.
getFile() This method returns the file path in which an exception is thrown.
getLine() This method returns the line number of the line of the code that generated an exception condition.

FAQs

  1. What is the use of the getTrace() method?
    The getTrace() method returns a stack trace in array format. It will contain information regarding all the functions running at a given moment.
     
  2. What is the use of the getTraceAsString() method?
    The getTraceAsString() method is similar to the getTrace() method. The difference is that the return type is a string instead of an array.

Key takeaway

PHP exceptions are certain objects that can describe the unnecessary errors in a PHP program. They contain descriptions of these errors, making it easy to deal with them. Using exceptions helps to separate the error handling code from the normal code. 

 

If you loved reading this article about PHP exceptions, check out Why Use PHP programming in 2021? Its Pros and Cons and 5 Best Free PHP Projects With Source Code To Work In 2021.

Live masterclass