Analyze PHP5 exception handling with examples_PHP tutorial
<?php
/**
* ■Exception handling in PHP5
*
* PHP 5 adds an exception handling module similar to other languages. Exceptions generated in PHP code can be thrown
* statement throws and is caught by a catch statement. Code that requires exception handling must be placed within the try code block, ending with
* Catch possible exceptions. Every try must have at least one corresponding catch. Use multiple catches
* Can catch exceptions generated by different classes. When the try code block no longer throws an exception or cannot find a matching catch
* When an exception is thrown, the PHP code will continue executing after jumping to the last catch. Of course, PHP
* Allow exceptions to be thrown again within catch blocks.
* When an exception is thrown, the subsequent code will not continue
* is executed, and PHP will try to find the first matching catch. If an exception is not caught and
* If there is no need to use set_exception_handler() for corresponding processing, then PHP will generate a
* A serious error, and the prompt message Uncaught Exception... (uncaught exception) is output.
*/
?>
<?php
/**
* Exception.php
*
* ■㈡The properties and methods of the built-in exception class in PHP5
* The following code is only to illustrate the structure of the built-in exception handling class. It is not a usable code with practical significance.
*/
class Exception{
protected $message = 'Unknown exception'; // Exception message
protected $code = 0; // User-defined exception code
protected $file; // The file name where the exception occurred
protected $line; // Code line number where the exception occurred
function __construct($message = null, $code = 0);
final function getMessage(); // Return exception message
final function getCode(); // Return exception code (code name)
final function getFile(); // Return the file name where the exception occurred
final function getLine(); // Returns the code line number where the exception occurred
final function getTrace(); // backtrace() array
final function getTraceAsString(); // The getTrace() information that has been formatted into a string
Overloadable methods
function __toString(); // Outputable string
}
?>
<?php
/**
* syntax .php
*/
//■㈢Grammar structure and analysis
//PHP has two formats for throwing exceptions, as follows
//【1】try...catch...
try {
//Perform abnormal operations, such as database errors and file errors
}catch (Exception $e){
//Print error message
}
//【2】throw
$message='I must be run in the try{} block. If an exception occurs, I ($message) will be returned (passed) to the instance of the exception object in catch(), such as $e above';
$code=123; //Error code number, you can use $e->getCode(); in the catch block to return my value 123, so that I can customize the error code number
throw new Exception($message,$code);
//Note when learning JAVA, PHP exception handling does not have throws
?>
<?php
/**
* Example.php
*/
//■㈣Two examples to master PHP exception handling
//Example [1] use try...catch
/* PDO connects to the mysql database. If you have not seen PDO, first look at the constructor of PDO, or skip Example 1 and look at Example 2 */
$dsn = 'mysql:host=localhost;dbname=testdb';
$user = 'dbuser';
$password = 'dbpass';
try {
$dbh = new PDO($dsn, $user, $password); //Creating a database connection object is prone to exceptions
echo 'If there is an exception above, I will not be displayed';
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->__toString();
}
?>
<?php
//Example[2] try..cathc and throw are used together
try {
$error = 'I threw an exception message and jumped out of the try block';
if(is_dir('./tests')){
echo 'do sth.';
}else{
throw new Exception($error,12345);
}
echo 'If there is an exception above, it won't be my turn! ~<br />',"n";
} catch (Exception $e) {
echo 'Catch exception: ', $e->getMessage(),$e->getCode(), "n<br />"; //Display $error and 123456
}
echo 'Continue execution';
?>
<?php
//PHP's processing is much easier to learn than JAVA, because JAVA has too many exception classes, throws, etc.
?>

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

In this chapter, we are going to learn the following topics related to routing ?

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Validator can be created by adding the following two lines in the controller.
