tags:

views:

3244

answers:

4

Hey everyone, Im quite new to programming so please be nice :)

I am currently experimenting with try-catch statements, I read the documentation on the php.net website and didn't find it all that helpful. I understand what they do, but from reading it, i would not be able to implement one into my own code. I need a real example to help me understand.

How could 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 Didnt 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 :)

Thanks alot!

+4  A: 

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.

Tom Haigh
Thanks for your reply and answer! I understand this wasn't a great example. When would it be suitable to use an Exception? thanks!
Ben McRae
I don't thin there are good or bad examples. Try/Catch statements are useful, when they are useful.The only thing you should matter, is php will throw E_ERROR or E_WARNING when a function is misused.
Boris Guéry
+2  A: 

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.

Kazar
+2  A: 

try-catch statements are used to handle exceptions. I don't believe that the funtion 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.

Sean
His example is correct but I think the point was that he's trying to learn about exceptions rather than trying to fix/improve the sample code.
Steve Claridge
Thanks Everyone for your quick responses! This seems to not be the best example to learn from. When would be a good/right time to use an Exception then?
Ben McRae
A: 

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.

Al McNicoll