BK201 Posted June 2, 2012 Share Posted June 2, 2012 OK I really can't seem to wrap my head around exceptions / why to use them over just normal error handling. So if someone could please explain to me how to properly use them I would be ecstatic. I've been trying to write an exception for a method that executes a query. I'm not really concerned with how to just fix this but to wrap my head around how they work, I would greatly appreciate an example with some explanation. I know if (mysqlerror()) is retarded but it was my last desperate attempt of many before seeking help lol. public function executeQuery() { mysql_query($this->_query); try { if (mysql_error()) { throw new Exception("There was a problem executing creation query"); } } catch (Exception $e) { echo $e->getMessage(); } } Quote Link to comment Share on other sites More sharing options...
silkfire Posted June 2, 2012 Share Posted June 2, 2012 I know PDO has Exception handling that you can catch. Is a way of gracefully taking care of errors, before spitting them out at the user. Not sure if mysql_ driver has Exception handling. Quote Link to comment Share on other sites More sharing options...
cpd Posted June 2, 2012 Share Posted June 2, 2012 Exceptions are extremely handy when you want to handle errors but continue executing your code. They can be extremely useful between different objects where one object can catch the error thrown by another and handle it appropriately. This allows continued execution of the code rather then a straight up error message from PHP displaying on the page. For example, you may have an FileObject which can read and write to files. The FileObject is instantiated and a method open($file) is called. This method checks to see if the files exists before trying to open it and if $file does not exist, it throws an exception which can be caught and handled appropriately. You could pass the thrown error to a MessageHandlingObject which in turn prints any errors that have occurred during execution. When you should and shouldn't use exceptions is somewhat subjective. Its down to the programmer however, you shouldn't be throwing exceptions if you can't then trace them back to where they originated as this can then cause problems for developers who follow in your footsteps. Exceptions are brilliant if you want to continue executing your code even if errors occur, this is why the phrase "try" is used as you try to do something and if it doesn't work, handle it appropriately and carry on. In your situation I would probably be doing something like. class MySQLAdaptor { public function query($sql){ $res = mysql_query($sql); if(!$res){ throw new Exception(mysql_error); } } } try { $sql = new MySQLAdaptor; $sql->query("SQL CODE"); } catch(Exception $e){ var_dump($e); } Although, many people would argue an error when executing a query is fatal and could cause harsh problems with your entire application therefore, exceptions shouldn't be used. I believe its largely down to what query your executing. If its not a vital query you could throw the exception and handle it, if its very important you could just let the code error. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.