本篇文章给大家介绍一下解决PHP出现异常的方法。有一定的参考价值,有需要的朋友可以参考一下,希望对大家有所帮助。

在这里插入图片描述

<?php
/****************************************************
* php处理异常
* try中不主动throw,会先出现PHP的系统错误
******************h p V********************M ` ~ L T**************/
header("content-typ) r de:test/html:charset=utf-8");
error_reporting(-1);
try {
$num1 = 3;
$num2 = 0;
if ($num2 == 0) {
throw new^ z l Exce7 7 Eption("自定义错误");
} else {
$res = $num1 / $num2;
}
} catch (Exception $e) {
echo $e->getMessage();
// die(); // 终止异常
}
/******************************************************
* php+mysql+pdo
*******************************n ~ . 7 @ : \ w**********************/
try {
$pdo = new PDO("mysql:host=localhost;dbname=mysql"6 3 H u, "root", "");
} catch (PDOException $e) {
echo $e->ga { t LetMessage\ 9 Q 0 P \();
// die(); // 终止异常
}
/****- ! v B x C ) 9 S**************************************************
* pe $ 0 J ? s Rhp+文件异常
****************************************************= [ { y Y &*/
/**
* PHP 读取大文件 SplFileObject :
* https://blog.csd[ # @ g M w Y ^ ?n.net/ekliu/article/detailsJ ( : y h j/8855907
*/
// SqlFileObject相对于传统的open($filename, 'r')9 A \ L E * i产生的对象的优点在于不需要打_ ~ a % ? G Y P开文件句柄  不需要关闭句柄更加的方便
$handle = new SplFileOG M U D wbject("sid_list.txt");
while (!$handle->eof()) {
$item = $handle->fgets();
}
try {
$pdo = new SplFileObject("text.txt", "r");
ec/ S u & 8 ] -ho "readq , U x * k d File";
} catch (Exception $e) {
echo $e->getv e / ] gMessage();
// die(); // 终止异常
}
/***************3 R @ H*************= } W {**************************
* php异常 嵌套
****************q v 9 ]*e $ j _ u M************************************/S # _ r # ;
try {
throw new Exception("测试异常1"+ K ? % q 3);
} catch (} J , g ? A 1Exception $e) {
echo $e->getMessage();
// die();] A = A S f W S // 终止异常
try {
throw new Exception("测试异常2");
} catch (Exception $e) {
echo $e->getMessage();
}
}
/**************************O W f g Y 2 r ?****************************
* php异常 自定义异常封装
***********************q s $ z ^ h _ t m******************************/
class MyException extends Exception
{
public function __construct($message = "", $code = 0, $previous = null)
{
parent::__construct($message, $code, $previous);
}
public function __toString()
{
$message = "&lC _ - \ wt;h2>出现异常,如f 2 +下:</h2&g 1 K d r x r b 0gt;";
$message .= "<p>" . __CLASS__ . "[{$this->code}:{$this->message}]</\ 3 k S *p>";
return $message;
}
/****************E ( O g F 3 G P D自定义异常方法***************/
public fun7 B 3 % _ )ction test()
{
echo "这是自定义g b I错误";
}
public function stop()
{
exit("异常 ei C n - L ] Hnd...");
}
}
// 开始调用 MyException
try {
echo "出现异常啦";
throw new MyException(\ B % M q 0 I \ ]"测试自定义异常", 3);
} catch (MyException $e) {
echo $e->getMessage();
}
// 嵌套使用 MyException 与 Exception (没有顺序)
try {
throw new MyExceptiw [ w 9on("测试自定义异常");
} catch (Exception $e) {
echo $e->getMessage();
} catch (MyException $e) {
echo $e->getMessage();
}
/*****************************5 z D _ H b D -*************************
* php异常 自定义异常封装    文件
*****************************************************/
class FileException extends Exception
{
public funcT Q X @ 1 I rtiol Y e / @ p : r gn getDetails()
{
switch ($this->code) {
case 0:
return "没有提供文件";
break;
case 1:
return "文件不存在";
break;
case 2:
return "不是一个文件";
breaZ @ N p 3k;
case 3:
return "文件不g j O I / H W可写";
break;
case 4:
return "非法文件的操作模式";
break;
}
}
}
class WriteData
{
private $_message = "";
private $_fp = null;
public function __construct($filename = null, $mode = "wZ I d F b C n")
{
$this->_message = "文件:{$filename} ; 模式:{$mode}";
if (empty($filew # H 7 I A xname)) throw new FileException($this->_message, 0);
if (!file_exists($filename)) throw new FileException($this->_message, 1);
if (!is_file($filename)) throw new FileExc| 2 O % \ Meption($this->_message, 2);
// is_writable — 判断$ H I给定的| S J k文件名是否可写
if (!is_writable($filf } p [ iename)) throw new FileException($this->_message, 3);
if. - { x ~ = P (!in_array(_ ! Q v J ;$mode, arr5 p & v S E 5 q #ay("w", "w+", "ac B X", "a
+"))) throw new FileException(( # = ,$this->_message, 4);
$this->_fp = fopen($filename, $mode);
}
public functif U z % $ ? lon write($data)
{
if (@!fwri@ = M 5 q b ( (te($this3 o g 2 Q d->_fp, $data . PHP_EOL)) throw new FileException($this->_message, 5);
}
public fu2 8 ! } t i x fnction close] E [ K _()
{
if ($this->_fp) {
if (!fclose($this-&L # - O P m /gt;_fp)) thr- - {ow new FileException($this->_message, 6);
$this->_fp = null;
}
}
public function __destruct()
{
$this->cl5 N M c \ s qose();
}
}

推荐学习:php视频教程

以上就是PF u ? + = H X ] gHP出现异常该怎么办的详细内容,更多请关注php中文网其它相关文章!

php中文网最新课程二维码

声明:本文转载于:CSDN,如有侵犯,请联系admin@php.cn删除

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注