简体   繁体   English

PHP异常尝试以程序方式捕获

[英]php exception try catch in procedural way

I want to know if it's possible to use try catch in procedural way to find exceptions and do some thing when I catch them like this: make sure if picture is uploaded then try to resize the picture if the resize function had a problem like (memory_limit) remove the picture. 我想知道是否有可能以程序方式使用try catch来查找异常并在像这样捕获它们时做一些事情:确保图片是否已上传,如果resize函数遇到类似(memory_limit的问题,请尝试调整图片的大小)删除图片。

 if(move-uploaded-file($_FILES['name']['tmp_name'], "folder/image.png"){
    try{
       resizeimage("folder/image.png", "folder/image.png");
    }catch(Exception $e){
       unlink("folder/image.png");
    }
 }

If that is the code you want to use, the Exception object in the catch will become available if the try{} errors and you will have access to it's methods. 如果这是您要使用的代码,则在try {}错误的情况下,捕获中的Exception对象将变为可用,并且您可以访问它的方法。 http://php.net/manual/en/language.exceptions.php . http://php.net/manual/zh/language.exceptions.php The way you handle the error with the Exception methods is the only part of the code that will be object oriented. 使用Exception方法处理错误的方式是代码中唯一面向对象的部分。 Everything inside the catch can be as procedural as you like. 渔获物内的所有物品都可以根据您的喜好进行操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM