问题描述:

I understand what try-catch statements do, but from reading the documentation on php.net, I would not be able to implement one into my own code. I need a real example to help me understand.

How can I turn this example into a try catch statement, if the upload was not successful?

$move = move_uploaded_file($_FILES['file']['tmp_name'], $_SERVER['DOCUMENT_ROOT']."/uploads/".$_FILES['file']['name']);

if (!$move) {

die ('File didn't upload');

} else {

//opens the uploaded file for extraction

echo "Upload Complete!";

}

This may not be a good example to work with, but any help would be appreciated.

网友答案:

You could do it like this.

try {
    //throw exception if can't move the file
    if (!move_uploaded_file( ... )) {
        throw new Exception('Could not move file');
    }

    //do some more things with the file which may also throw an exception
    //...

    //ok if got here
    echo "Upload Complete!";
} catch (Exception $e) {
    die ('File did not upload: ' . $e->getMessage());
}

It is a bit pointless for the above example, but you should get the idea. Note that you can throw the exception(s) from anywhere (e.g. within a function/method that you call from withing the try{}) and they will propagate upwards.

网友答案:

Well, if you want to use exceptions, you could do something like:

function handleUpload() {


    $move = move_uploaded_file($_FILES['file']['tmp_name'], $_SERVER['DOCUMENT_ROOT']."/uploads/".$_FILES['file']['name']);

    if (!$move) {
       throw new Exception('File Didnt Upload');
    }

}

try {
   handleUpload();
   echo "File Uploaded Successfully";
} catch(Exception $ex) {
   die($ex->getMessage);
}

I know this may seem like bloat - but you can call the method from anywhere in the call stack, and catch the exception at any point.

网友答案:

try-catch statements are used to handle exceptions. I don't believe that the function move_uploaded_files can throw and exception, as such I think that you code is written is correct. After the call, you look at the return code. If it's false, you end processing, otherwise you are report success.

网友答案:

According to a similar post in PHPbug, only OO code (Object-Oriented code) throws exceptions. That would mean that functions such as move_uploaded_file won't throw their own exceptions, but some other code will.

网友答案:

I found this helpful.

A good example of the application of exception handling and try catch stuff is here.

网友答案:

You have to take a look on [Exception Best Practices in PHP 5.3][1]

Exception handling in PHP is not a new feature by any stretch. In the following link, you’ll see two new features in PHP 5.3 based around exceptions. The first is nested exceptions and the second is a new set of exception types offered by the SPL extension (which is now a core extension of the PHP runtime). Both of these new features have found their way into the book of best best practices and deserve to be examined in detail.

http://ralphschindler.com/2010/09/15/exception-best-practices-in-php-5-3

相关阅读:
Top