search
HomeBackend DevelopmentPHP Tutorialphp错误处理初步学习(转)

php异常处理初步学习(转)
/**
* PHP异常处理
*
* PHP 5 添加了类似于其它语言的异常处理模块。在 PHP 代码中所产生的异常可被 throw
* 语句抛出并被 catch 语句捕获。需要进行异常处理的代码都必须放入 try 代码块内,以
* 便捕获可能存在的异常。每一个 try 至少要有一个与之对应的 catch。使用多个 catch
* 可以捕获不同的类所产生的异常。当 try 代码块不再抛出异常或者找不到 catch 能匹配
* 所抛出的异常时,PHP 代码就会在跳转到最后一个 catch 的后面继续执行。当然,PHP
* 允许在 catch 代码块内再次抛出(throw)异常。
* 当一个异常被抛出时,其后(译者注:指抛出异常时所在的代码块)的代码将不会继续
* 执行,而 PHP 就会尝试查找第一个能与之匹配的 catch。如果一个异常没有被捕获,而
* 且又没用使用 set_exception_handler() 作相应的处理的话,那么 PHP 将会产生一
* 个严重的错误,并且输出 Uncaught Exception ... (未捕获异常)的提示信息。
*/
?>

/**
* Exception.php
*
*PHP5内置的异常类的属性与方法
* 以下这段代码只为说明内置异常处理类的结构,它并不是一段有实际意义的可用代码。
*/

class Exception{
protected $message = 'Unknown exception'; // 异常信息
protected $code = 0; // 用户自定义异常代码
protected $file; // 发生异常的文件名
protected $line; // 发生异常的代码行号

function __construct($message = null, $code = 0);
final function getMessage(); // 返回异常信息
final function getCode(); // 返回异常代码(代号)
final function getFile(); // 返回发生异常的文件名
final function getLine(); // 返回发生异常的代码行号
final function getTrace(); // backtrace() 数组
final function getTraceAsString(); // 已格成化成字符串的 getTrace() 信息

//可重载的方法
function __toString(); // 可输出的字符串
}
?>

/**
* syntax .php
*/

//语法结构以及分析

//PHP有两种抛出异常的格式,如下

//【1】try...catch...
try {
//实行可能有异常的操作,比如数据库错作,文件错作
}catch (Exception $e){
//打印错误信息  写日志 或则再处理
}

//【2】throw
$message='我必须被运行在try{}块中,出现异常的话我($message)将被返回(传递)给catch()里的异常对象的实例比如上面的$e';
$code=123; //错误代码号,可在catch块中用$e->getCode();返回我的值 123,这样我就可以自定义错误代码号

throw new Exception($message,$code);

//学JAVA的注意,PHP异常处理没有throws
?>
/**
* Example.php
*/
//两个实例掌握PHP异常处理


//例【1】用 try...catch
/* PDO连接mysql数据库,如果没看过PDO,先看下PDO的构造函数,要不跳过例1看例2 */
$dsn = 'mysql:host=localhost;dbname=testdb';
$user = 'dbuser';
$password = 'dbpass';
try {
$dbh = new PDO($dsn, $user, $password); //创建数据库连接对象容易出现异常
echo '如果上面出现异常就不能显示我了';
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->__toString();
}
?>

//例[2] try..cathc 和 throw一起用
try {
$error = '我抛出异常信息,并且跳出try块';
if(is_dir('./tests')){
echo 'do sth.';
}else{
throw new Exception($error,12345);
}
echo '上面有异常的话就轮不到我了!~
',"\n";
} catch (Exception $e) {
echo '捕获异常: ', $e->getMessage(),$e->getCode(), "\n
"; //显示$error和123456
}
echo '继续执行';
?>

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Dependency Injection in PHP: Avoiding Common PitfallsDependency Injection in PHP: Avoiding Common PitfallsMay 16, 2025 am 12:17 AM

DependencyInjection(DI)inPHPenhancescodeflexibilityandtestabilitybydecouplingdependencycreationfromusage.ToimplementDIeffectively:1)UseDIcontainersjudiciouslytoavoidover-engineering.2)Avoidconstructoroverloadbylimitingdependenciestothreeorfour.3)Adhe

How to Speed Up Your PHP Website: Performance TuningHow to Speed Up Your PHP Website: Performance TuningMay 16, 2025 am 12:12 AM

ToimproveyourPHPwebsite'sperformance,usethesestrategies:1)ImplementopcodecachingwithOPcachetospeedupscriptinterpretation.2)Optimizedatabasequeriesbyselectingonlynecessaryfields.3)UsecachingsystemslikeRedisorMemcachedtoreducedatabaseload.4)Applyasynch

Sending Mass Emails with PHP: Is it Possible?Sending Mass Emails with PHP: Is it Possible?May 16, 2025 am 12:10 AM

Yes,itispossibletosendmassemailswithPHP.1)UselibrarieslikePHPMailerorSwiftMailerforefficientemailsending.2)Implementdelaysbetweenemailstoavoidspamflags.3)Personalizeemailsusingdynamiccontenttoimproveengagement.4)UsequeuesystemslikeRabbitMQorRedisforb

What is the purpose of Dependency Injection in PHP?What is the purpose of Dependency Injection in PHP?May 16, 2025 am 12:10 AM

DependencyInjection(DI)inPHPisadesignpatternthatachievesInversionofControl(IoC)byallowingdependenciestobeinjectedintoclasses,enhancingmodularity,testability,andflexibility.DIdecouplesclassesfromspecificimplementations,makingcodemoremanageableandadapt

How to send an email using PHP?How to send an email using PHP?May 16, 2025 am 12:03 AM

The best ways to send emails using PHP include: 1. Use PHP's mail() function to basic sending; 2. Use PHPMailer library to send more complex HTML mail; 3. Use transactional mail services such as SendGrid to improve reliability and analysis capabilities. With these methods, you can ensure that emails not only reach the inbox, but also attract recipients.

How to calculate the total number of elements in a PHP multidimensional array?How to calculate the total number of elements in a PHP multidimensional array?May 15, 2025 pm 09:00 PM

Calculating the total number of elements in a PHP multidimensional array can be done using recursive or iterative methods. 1. The recursive method counts by traversing the array and recursively processing nested arrays. 2. The iterative method uses the stack to simulate recursion to avoid depth problems. 3. The array_walk_recursive function can also be implemented, but it requires manual counting.

What are the characteristics of do-while loops in PHP?What are the characteristics of do-while loops in PHP?May 15, 2025 pm 08:57 PM

In PHP, the characteristic of a do-while loop is to ensure that the loop body is executed at least once, and then decide whether to continue the loop based on the conditions. 1) It executes the loop body before conditional checking, suitable for scenarios where operations need to be performed at least once, such as user input verification and menu systems. 2) However, the syntax of the do-while loop can cause confusion among newbies and may add unnecessary performance overhead.

How to hash strings in PHP?How to hash strings in PHP?May 15, 2025 pm 08:54 PM

Efficient hashing strings in PHP can use the following methods: 1. Use the md5 function for fast hashing, but is not suitable for password storage. 2. Use the sha256 function to improve security. 3. Use the password_hash function to process passwords to provide the highest security and convenience.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!