tags:

views:

44

answers:

2

Hello,

Can anybody tell me how are exceptions handled in PHP? I have this code

try{
   $this->b->methodthatraisesexception();
}
catch(Exception $ex){
  echo "Hi Hi hi, you tried to deleted a non existing file";
}

When i try to run this code instead of seeing the echo message, i see some kind of weird PHP error occured message. Btw, the above method is contained in A's class which handles exceptions. It is calling to b's method which generates error but doesn't have try catch. I see the below given error. Instead of my own echo statement. Please let me know if anything is unclear.

EDIT

A PHP Error was encountered

Severity: Warning

Message: unlink(C:/Program Files/wamp/www/\College\uploads\4c4b29de80e39.jpg) [function.unlink]: No such file or directory

Filename: models/settings.php

Line Number: 31

Thanks in advance :)

A: 
i see some kind of weird PHP error occured

And what might that error be?

Zane Edward Dockery
Please read my edited post. How do i prevent this error and show my own echo message?
Ankit Rathod
What I do for my file handling classes is temporary install my own errorhandler with set_error_handler that handles the E_WARNING. In the error handler function I first restore the original error handler and then throw the error string as an exception. Either that or you can use the silence operator (@) and check for the return code (but I am principally against this operator)
Blizz
+1  A: 

unlink() doesn't throw exceptions.

A E_WARNING level error will be generated on failure.

Returns TRUE on success or FALSE on failure.

http://php.net/manual/en/function.unlink.php

As far as throwing exceptions, you just throw 'em. You don't declare that the class throws them.

http://us3.php.net/manual/en/language.exceptions.php

Update:

Basically, do this where unlink() is called:

if(unlink('somefile'))
{
    // success condition
}
else
{
    // failure condition
}
George Marian