📜  PHP的异常与错误

📅  最后修改于: 2022-05-13 01:54:11.059000             🧑  作者: Mango

PHP的异常与错误

错误:错误是程序本身无法处理的意外结果。这可以通过手动使用代码中的问题来解决。错误可能是程序本身无法处理的无限循环,因此您必须手动修复该问题。有一个简单的程序来处理错误,即使用die()函数。
句法:

die("message")

程序:


输出:

Sorry Error!! file does not exists

异常:异常也是程序的意外结果,但程序本身可以通过抛出另一个异常来处理异常。异常应该只与错误条件一起使用,其中错误是不可删除的。有一种简单的方法可以通过使用trycatch方法来克服异常。

句法:



try {

}
catch {

}

程序:

getmessage();
}
   
?>

输出:

Error
Why should not be equal to 0

差异总结:

ERROREXCEPTION
Error are the procedural approach.Exceptions are an object-oriented approach to programming.
The default error handling in PHP is very simple. An error message with filename, line number and a message describing the error is sent to the browser.Exceptions are used to change the normal flow of a script if a specified error occurs.
This can be done using PHP die() Function.Basic Exception Handling using throw new Exception() in case of advance Exception handling youn have to use try and catch method.
Errors are mostly caused by the environment in which program is running.Program itself is responsible for causing exceptions.

注意: PHP默认是一种异常轻量级语言,但在使用面向对象的代码时,您可以将错误更改为异常。