37. 错误与异常处理(1)

时间:2022-08-26 08:14:53

1. die() 语句
37. 错误与异常处理(1)

die("信息"); // 可以输出信息再退出

file_exists("new1.txt") or die("文件不存在") ;

2.自定义创建错误函数
37. 错误与异常处理(1)
37. 错误与异常处理(1)
37. 错误与异常处理(1)

// 自定义的错误(处理器)处理方法
function my_error($errno,$errmes)
{

echo "<font size='5' color='red'>$errno</font><br/>";
echo "错误信息是: $errmes" . "<br/>";
}

// 这里我们还需要改写 set_error_handler()函数
set_error_handler("my_error",E_WARNING);

$fp = fopen('aa.txt','r');
echo 'Ok';

37. 错误与异常处理(1)
37. 错误与异常处理(1)
37. 错误与异常处理(1)

错误触发器:
37. 错误与异常处理(1)
37. 错误与异常处理(1)
37. 错误与异常处理(1)

$age = 700;
if($age>120)
{
trigger_error("输入年龄过大");
exit();
}

37. 错误与异常处理(1)
37. 错误与异常处理(1)

自定义:
37. 错误与异常处理(1)
37. 错误与异常处理(1)

//自定义错误函数
function my_error3($errno,$errmes)
{

echo "错误号是:" . $errno;
}

//指定E_user_WARNING 错误级别的函数
set_error_handler("my_error3",E_USER_WARNING);

$age = 700;
if($age>120)
{
// 调用触发器,同时指定错误级别。
trigger_error("输入年龄过大123",E_USER_WARNING);
exit();
}
echo 'Ok';

37. 错误与异常处理(1)

//自定义错误函数


function my_error4($errno,$errmes)
{

echo "出大事了:" . $errno. "<BR/>";
exit();
}

//指定E_user_WARNING 错误级别的函数
set_error_handler("my_error4",E_USER_ERROR);

$age = 700;
if($age>120)
{
// 调用触发器,同时指定错误级别。
trigger_error("输入年龄过大123",E_USER_ERROR);
//exit();
}
echo 'Ok';

37. 错误与异常处理(1)


3.错误日志
37. 错误与异常处理(1)

function my_error4($errno,$errmes)
{

$err_info = "出大事了:" . $errno. "<BR/>";
echo $err_info;
//保存错误信息
error_log($err_info.'\r\n',3,"myerror.txt");
}

//指定E_user_WARNING 错误级别的函数
set_error_handler("my_error4",E_USER_ERROR);

$age = 700;
if($age>120)
{
// 调用触发器,同时指定错误级别。
trigger_error("输入年龄过大123",E_USER_ERROR);
//exit();
}
加上时间:
echo $err_info;
//保存错误信息
date_default_timezone_get("Asia/Chongqing");
error_log("时间是" . date("Y-m-d G-i-s") . $err_info . "\r\n" ,3,"myerror.txt");

4. 异常处理
37. 错误与异常处理(1)
37. 错误与异常处理(1)
37. 错误与异常处理(1)
37. 错误与异常处理(1)
37. 错误与异常处理(1)

37. 错误与异常处理(1)
37. 错误与异常处理(1)
37. 错误与异常处理(1)

//我们使用异常机制
try{
addUser('sss');
updateUser('xxx');
}
//catch 捕获Exception 是异常类(php定义好的一个类)
catch(Exception $e){
echo "失败信息 : " . $e->getMessage();
}

function addUser($username)
{

if($username == 'wjh'){
//添加ok
}else{
//添加error
throw new Exception("添加失败");
}
}

function updateUser($username)
{

if($username == 'xiaoming'){
//添加ok
}else{
throw new Exception('更新失败');
}
}

37. 错误与异常处理(1)
37. 错误与异常处理(1)

function a1($val)
{

if($val>100){
throw new Exception('val>100');
}
}

function a2($val)
{

if($val == 'hello'){
throw new Exception('不要输入hello');
}
}

try{
a2('hello');
}catch(Exception $e){
throw $e; //抛给系统
}

37. 错误与异常处理(1)
自定义异常处理:
37. 错误与异常处理(1)
37. 错误与异常处理(1)

<?php
class customException extends Exception
{

public function errorMessage()
{

//error message
$errorMsg = 'Error on line '.$this->getLine().' in '.$this->getFile()
.': <b>'.$this->getMessage().'</b> is not a valid E-Mail address';
return $errorMsg;
}
}

$email = "someone@example...com";

try
{
//check if
if(filter_var($email, FILTER_VALIDATE_EMAIL) === FALSE)
{
//throw exception if email is not valid
throw new customException($email);
}
}

catch (customException $e)
{
//display custom message
echo $e->errorMessage();
}
?>

37. 错误与异常处理(1)

37. 错误与异常处理(1)

<?php
class customException extends Exception
{

public function errorMessage()
{

//error message
$errorMsg = 'Error on line '.$this->getLine().' in '.$this->getFile()
.': <b>'.$this->getMessage().'</b> is not a valid E-Mail address';
return $errorMsg;
}
}

$email = "someone@example.com";

try
{
//check if
if(filter_var($email, FILTER_VALIDATE_EMAIL) === FALSE)
{
//throw exception if email is not valid
throw new customException($email);
}
//check for "example" in mail address
if(strpos($email, "example") !== FALSE)
{
throw new Exception("$email is an example e-mail");
}
}

catch (customException $e)
{
echo $e->errorMessage();
}

catch(Exception $e)
{
echo $e->getMessage();
}
?>

37. 错误与异常处理(1)


37. 错误与异常处理(1)

//定义一个*异常处理器
function my_exception($e)
{

echo '我是定义异常处理器' . $e->getMessage();
}

//修改默认的*异常处理器
set_exception_handler('my_exception');

function a1($val)
{

if($val>100){
throw new Exception('val>100');
}
}

function a2($val)
{

if($val == 'hello'){
throw new Exception('不要输入hello');
}
}

try{
a2('hello');
}catch(Exception $e){
throw $e; //抛给系统
}

37. 错误与异常处理(1)
37. 错误与异常处理(1)

自定义异常类:
37. 错误与异常处理(1)
37. 错误与异常处理(1)

//定义了一个异常
class MyException1 extends Exception
{


}

class MyException2 extends Exception
{


}

function A()
{

throw new MyException1('a');
}

function B()
{

throw new MyException2('b');
}

function C()
{

try{
A();
B();
}catch(MyException $e1){
$e1->getMessage();
}catch(MyException2 $e2){
$e2->getMessage();
}
}
try{
//$i = 8/0;

$fp = fopen('aa.txt','r'); //php需要抛出才可以捕获异常,这个太老,没有抛,所以捕获不到异常
}catch(Exception $e){
echo 'ok';
echo $e->getMessage();
}

37. 错误与异常处理(1)


37. 错误与异常处理(1)

<?php
class customException extends Exception
{

public function errorMessage()
{

//error message
$errorMsg = $this->getMessage().' is not a valid E-Mail address.';
return $errorMsg;
}
}

$email = "someone@example.com";

try
{
try
{
//check for "example" in mail address
if(strpos($email, "example") !== FALSE)
{
//throw exception if email is not valid
throw new Exception($email);
}
}
catch(Exception $e)
{
//re-throw exception
throw new customException($email);
}
}

catch (customException $e)
{
//display custom message
echo $e->errorMessage();
}

37. 错误与异常处理(1)


37. 错误与异常处理(1)