Home Backend Development PHP Tutorial php错误、异常处理机制(补充)_PHP

php错误、异常处理机制(补充)_PHP

Jun 01, 2016 pm 12:11 PM
Exception handling mistake

一、错误处理
异常处理: 意外,是在程序运行过程中发生的意料这外的事,使用异常改变脚本正常流程
PHP5中的一个新的重要特性
复制代码 代码如下:
if(){
}else{
}
try {
}catch(异常对象){
}

1. 如果try中代码没有问题,则将try中代码执行完后就到catch后执行
2. 如果try中代码有异常发生,则抛出一个异常对象(使用throw),抛出给了catch中的参数, 则在try中代码就不会再继续执行下去
直接跳转到catch中去执行, catch中执行完成, 再继续向下执行
注意: 提示发生了什么异常,这不是主要我们要做事,需要在catch中解决这个异常, 如果解决不了,则出去给用户
二、自己定义一个异常类
作用:就是写一个或多个方法解决当发生这个异常时的处理方式
1. 自己定义异常类,必须是Exception(内置类)的子类,
2. Exception类中的只有构造方法和toString()可以重写, 其它都final
三、处理多个异常
自己定义功能类时如果在方法中抛出异常
复制代码 代码如下:
class OpenFileException extends Exception {
function __construct($message = null, $code = 0){
parent::__construct($message, $code);
echo "wwwwwwwwwwwwwww
";
}
function open(){
touch("tmp.txt");
$file=fopen("tmp.txt", "r");
return $file;
}
}
class DemoException extends Exception {
function pro(){
echo "处理demo发生的异常
";
}
}
class TestException extends Exception {
function pro(){
echo "这里处理test发生的异常
";
}
}
class HelloException extends Exception {
}
class MyClass {
function openfile(){
$file=@fopen("tmp.txt", "r");
if(!$file)
throw new OpenFileException("文件打开失败");
}
function demo($num=0){
if($num==1)
throw new DemoException("演示出异常");
}
function test($num=0){
if($num==1)
throw new TestException("测试出错");
}
function fun($num=0){
if($num==1)
throw new HelloException("###########");
}
}
try{
echo "11111111111111
";
$my=new MyClass();
$my->openfile();
$my->demo(0);
$my->test(0);
$my->fun(1);
echo "22222222222222222
";
}catch(OpenFileException $e){ //$e =new Exception();
echo $e->getMessage()."
";
$file=$e->open();
}catch(DemoException $e){
echo $e->getMessage()."
";
$e->pro();
}catch(TestException $e){
echo $e->getMessage()."
";
$e->pro();
}catch(Exception $e){
echo $e->getMessage()."
";
}
var_dump($file);
echo "444444444444444444444
";
Statement of this Website
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

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Two Point Museum: All Exhibits And Where To Find Them
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Solution to Windows Update prompt Error 0x8024401c error Solution to Windows Update prompt Error 0x8024401c error Jun 08, 2024 pm 12:18 PM

Table of Contents Solution 1 Solution 21. Delete the temporary files of Windows update 2. Repair damaged system files 3. View and modify registry entries 4. Turn off the network card IPv6 5. Run the WindowsUpdateTroubleshooter tool to repair 6. Turn off the firewall and other related anti-virus software. 7. Close the WidowsUpdate service. Solution 3 Solution 4 "0x8024401c" error occurs during Windows update on Huawei computers Symptom Problem Cause Solution Still not solved? Recently, the web server needs to be updated due to system vulnerabilities. After logging in to the server, the update prompts error code 0x8024401c. Solution 1

C++ function exceptions and multithreading: error handling in concurrent environments C++ function exceptions and multithreading: error handling in concurrent environments May 04, 2024 pm 04:42 PM

Function exception handling in C++ is particularly important for multi-threaded environments to ensure thread safety and data integrity. The try-catch statement allows you to catch and handle specific types of exceptions when they occur to prevent program crashes or data corruption.

How does C++ exception handling support custom error handling routines? How does C++ exception handling support custom error handling routines? Jun 05, 2024 pm 12:13 PM

C++ exception handling allows the creation of custom error handling routines to handle runtime errors by throwing exceptions and catching them using try-catch blocks. 1. Create a custom exception class derived from the exception class and override the what() method; 2. Use the throw keyword to throw an exception; 3. Use the try-catch block to catch exceptions and specify the exception types that can be handled.

What is the relationship between recursive calls and exception handling in Java functions? What is the relationship between recursive calls and exception handling in Java functions? May 03, 2024 pm 06:12 PM

Exception handling in recursive calls: Limiting recursion depth: Preventing stack overflow. Use exception handling: Use try-catch statements to handle exceptions. Tail recursion optimization: avoid stack overflow.

Exception handling in C++ technology: How to handle exceptions correctly in a multi-threaded environment? Exception handling in C++ technology: How to handle exceptions correctly in a multi-threaded environment? May 09, 2024 pm 12:36 PM

In multithreaded C++, exception handling follows the following principles: timeliness, thread safety, and clarity. In practice, you can ensure thread safety of exception handling code by using mutex or atomic variables. Additionally, consider reentrancy, performance, and testing of your exception handling code to ensure it runs safely and efficiently in a multi-threaded environment.

How to handle exceptions in C++ Lambda expressions? How to handle exceptions in C++ Lambda expressions? Jun 03, 2024 pm 03:01 PM

Exception handling in C++ Lambda expressions does not have its own scope, and exceptions are not caught by default. To catch exceptions, you can use Lambda expression catching syntax, which allows a Lambda expression to capture a variable within its definition scope, allowing exception handling in a try-catch block.

PHP exception handling: understand system behavior through exception tracking PHP exception handling: understand system behavior through exception tracking Jun 05, 2024 pm 07:57 PM

PHP exception handling: Understanding system behavior through exception tracking Exceptions are the mechanism used by PHP to handle errors, and exceptions are handled by exception handlers. The exception class Exception represents general exceptions, while the Throwable class represents all exceptions. Use the throw keyword to throw exceptions and use try...catch statements to define exception handlers. In practical cases, exception handling is used to capture and handle DivisionByZeroError that may be thrown by the calculate() function to ensure that the application can fail gracefully when an error occurs.

How to handle cross-thread C++ exceptions? How to handle cross-thread C++ exceptions? Jun 06, 2024 am 10:44 AM

In multi-threaded C++, exception handling is implemented through the std::promise and std::future mechanisms: use the promise object to record the exception in the thread that throws the exception. Use a future object to check for exceptions in the thread that receives the exception. Practical cases show how to use promises and futures to catch and handle exceptions in different threads.

See all articles