Jump to content

Exception Help


BK201

Recommended Posts

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();
}
}

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.