PHP try...catch..finally...

本文详细解读了PHP中try-catch-finally语句的三个代码示例,重点讲解了如何捕获和处理异常,以及finally块的执行特性。通过实例,读者将理解如何在程序中确保关键操作无论是否发生异常都能执行。
摘要由CSDN通过智能技术生成

代码示例一:

// example1

<?php
try{
    throw new Exception("Trigger exception first time!");

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

}finally{
    echo "Hello the world!\n";
}

输出:

/mnt/hgfs/SlgCardsServer/example.php:6:
string(29) "Trigger exception first time!"
Hello the world!

代码示例二:

// example2

<?php
try{
    throw new Exception("Trigger exception second time!");

}catch (Exception $e){
    var_dump($e->getMessage());
    return;

}finally{
    echo "Hello the world,again!\n";
}

输出:

/mnt/hgfs/SlgCardsServer/example.php:7:
string(30) "Trigger exception second time!"
Hello the world,again!

代码示例三

// example3

try{
    throw new Exception("Trigger exception second time!");

}catch (Exception $e){
    var_dump($e->getMessage());
    exit(0);

}finally{
    echo "Hello the world,again!\n";
}

输出:

/mnt/hgfs/SlgCardsServer/example.php:6:
string(30) "Trigger exception second time!"
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值