Solved

Appropriate use of exceptions

Posted on 2007-12-01
4
246 Views
Last Modified: 2013-12-13
I can best present this question with code...

Thanks
//This is how you'd (presumably) normally work with exceptions...
 
//within the class
public function query($query) {
 if (!$query = @mysql_query($query,$this->conn)) {
  throw new Exception('Query Error');
 }
 return $query;
}
 
//code
$db = new classname();
try {
 $db->query('the_query'); 
}
catch (Exception $e) {
 die($e->getMessage());
}
 
#########################################
 
/* but I'm thinking as an exception thrown will always result in the
 termination of the script here, and I'd otherwise have to include
 the try-catch block every time I want to run a query; would it not 
make more sense to just build it into the class? i.e. */
 
#########################################
 
//within the class
public function query($query) {
 try {
  if (!$query = @mysql_query($query,$this->conn)) {
   throw new Exception('Query Error');
  }
 } catch (Exception $e) {
  die($e->getMessage());
 }
 $this->query_count++;
 return $query;
}
 
//code
$db = new classname();
$db->query('the_query');
 
 
/* is there any reason why this would be considered bad 
practice, or a bad idea? */

Open in new window

0
Comment
Question by:calcanus
4 Comments
 
LVL 36

Accepted Solution

by:
Zyloch earned 125 total points
ID: 20388704
If you are just going to kill the script, then you would do it within the function itself, but most of the time, you wouldn't want to be that extreme. You would want some way to notify the calling function that something went wrong. In this case, you would throw your exception. Perhaps, you would log the error to a log file before that inside your function.

Yes, it litters your code with many try-catch clauses. However, since you can pass any message string in an exception, you can use a central catch clause. Or if the function that is catching the exception is part of another function, you can translate multiple exception types into a single exception that is more general (perhaps logging along the way).

That said, you may be able to use set_exception_handler() to handle truly redundant cases.
0
 
LVL 19

Assisted Solution

by:SteveH_UK
SteveH_UK earned 125 total points
ID: 20389160
In this particular case, you might also consider supporting both options by an optional parameter.  That way your code is reusable, and supports informing the caller of an "exceptional" circumstance, but also allows you to choose a default behaviour of exiting your script.
0
 
LVL 16

Expert Comment

by:CWS (haripriya)
ID: 21188508
No comment has been added to this question in more than 21 days, so it is now classified as abandoned.

I will leave the following recommendation for this question in the Cleanup topic area:
   Split: steelseth12 {http:#20388704} & SteveH_UK {http:#20389160}

Any objections should be posted here in the next 4 days. After that time, the question will be closed.

cyberwebservice
Experts Exchange Cleanup Volunteer
0
 
LVL 1

Expert Comment

by:Computer101
ID: 21216914
Forced accept.

Computer101
EE Admin
0

Featured Post

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How would I do a website like this? 5 59
php string detection problem 7 34
Attach to file (img) a unique id 8 27
how to use Initialization Vector for openssl_encrypt() 5 28
Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
This article discusses how to create an extensible mechanism for linked drop downs.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question