php 錯(cuò)誤處理類
Jun 13, 2016 pm 12:15 PM
php 異常處理類
PHP具有很多異常處理類,其中Exception是所有異常處理的基類。
Exception具有幾個(gè)基本屬性與方法,其中包括了:
message 異常消息內(nèi)容
code 異常代碼
file 拋出異常的文件名
line 拋出異常在該文件的行數(shù)
其中常用的方法有:
getTrace 獲取異常追蹤信息
getTraceAsString 獲取異常追蹤信息的字符串
getMessage 獲取出錯(cuò)信息
如果必要的話,可以通過(guò)繼承Exception類來(lái)建立自定義的異常處理類。
//自定義的異常類,繼承了PHP的異常基類Exceptionclass MyException extends Exception { function getInfo() { return '自定義錯(cuò)誤信息'; }}try { //使用異常的函數(shù)應(yīng)該位于 "try" 代碼塊內(nèi)。如果沒有觸發(fā)異常,則代碼將照常繼續(xù)執(zhí)行。但是如果異常被觸發(fā),會(huì)拋出一個(gè)異常。 throw new MyException('error');//這里規(guī)定如何觸發(fā)異常。注意:每一個(gè) "throw" 必須對(duì)應(yīng)至少一個(gè) "catch",當(dāng)然可以對(duì)應(yīng)多個(gè)"catch"} catch(Exception $e) {//"catch" 代碼塊會(huì)捕獲異常,并創(chuàng)建一個(gè)包含異常信息的對(duì)象 echo $e->getInfo();//獲取自定義的異常信息 echo $e->getMessage();//獲取繼承自基類的getMessage信息}

Hot AI Tools

Undress AI Tool
Undress images for free

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

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

Clothoff.io
AI clothes remover

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

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 Java, when multiple threads operate a collection object at the same time, a ConcurrentModificationException exception may occur. This exception usually occurs when traversing the collection when modifying or deleting elements. This will cause the state of the collection to be inconsistent, thus throwing abnormal. This article will delve into the causes and solutions to this exception. 1. Causes of Exception Normally, ConcurrentModificationException exception

In PHP development, you may encounter errors such as "PHPFatalerror:Uncaughtexception'PDOException'". This is an exception caused by an error when PHP operates the database. If this error is not handled in time, it will cause program interruption or unexpected errors. So how to solve this problem? Here are some common solutions. 1. Check the database parameters. First, we need to check the parameters passed when connecting to the database.

How to use try and catch in C requires specific code examples. In C language, there is no built-in try and catch mechanism for exception handling. However, the functionality of try and catch can be simulated by using the setjmp and longjmp functions. Below I will introduce in detail how to use these two functions for exception handling and give corresponding code examples. First, we need to understand the principles of the setjmp and longjmp functions. When the setjmp function is called, the current program will be saved.

How to deal with UnsupportedEncodingException in Java? In Java programming, you may encounter UnsupportedEncodingException. This exception is usually caused by incorrect encoding conversion or an unsupported encoding. In this article, we will introduce the causes of UnsupportedEncodingException exception and how to deal with it. What is UnsupportedE

In Java development, we often use arrays to store a series of data because of the convenience and performance advantages of arrays. However, in the process of using arrays, some exceptions will occur, and one of the common exceptions is ArrayStoreException. This exception is thrown when we store incompatible data types in the array. This article will introduce what an ArrayStoreException is, why it occurs, and how to solve it. 1. Arr

What are the common causes of ConcurrentModificationException in Java? When traversing a collection using an iterator in the Java collection framework, a ConcurrentModificationException exception is sometimes thrown, which is one of the common Java exceptions. So, what is the reason for this exception? First, we need to understand that the iterators provided by the Java collection framework are stateful. That is, when traversing

The Exception class and the Error class are both subclasses of the java.lang.Throwable class. We can handle runtime exceptions, but not errors. Exceptions are objects that represent logical errors that occur at runtime, causing the JVM to enter an "ambiguous" state. Objects automatically created by the JVM to represent these runtime errors are called exceptions. Error is a subclass of the Throwable class that indicates serious problems that reasonable applications should not try to catch. Most of these errors are anomalies. If an exception occurs, we can use try and catch blocks to handle it. If an error occurs that we cannot handle, the program will terminate. There are two types of exceptions, one is CheckedExce

In Java programming, array is an important data structure. Arrays can store multiple values ??in a single variable, and more importantly each value can be accessed using an index. But while working with arrays, some exceptions may occur, one of them is ArrayStoreException. This article will discuss common causes of ArrayStoreException exceptions. 1. Type mismatch The element type must be specified when the array is created. When we try to store incompatible data types into an array, it throws
