Jump to content

Exception Handling or If-else?


eldan88

Recommended Posts

Your query doesn't make sense to me since "Exceptions" and the use of if/else conditions are not mutually exclusive. You would normally use a control such as if/else to make a determination on whether to throw an exception or not anyway.

 

Having said that, what I *think* you meant to ask is when does it make sense to throw an exception as opposed to writing functionality to handle the error in the code, e.g. echoing the error. That is a good question and it does not always have an easy answer.

 

I guess I would state that Exceptions should primarily be used for unanticipated situations that an error would prevent the code from completing and providing a complete output. Versus a situation that could be expected to happen. For example, let's say you have code to calculate the the percentage of a salesperson's sales are made from "widgets" in a given time period. That would be calculated as (widget sales / total sales). What if the salesperson doesn't have any sales during the given time period? That would lead to a division by zero error. But, that is a perfectly valid scenario and needs to be handled in the code to provide an acceptable output rather than throwing a system error

 

if($totalSales==0) {
    $percentage = 'N/A';
} else {
    $percentage = $widgetSales / $totalSales;
}

 

Now, let's take that one step further. Let's say you have a function to determine a salesperson's sales for a given period so you can perform the above calculation. If that function fails because of bad input data, DB error, etc. then you simply can't provide a response to fulfill the request for the percentage of widget sales. This would be a situation where it could be appropriate to throw an error. But, that doesn't mean you can't also have code to provide a good user response. That's because you can suppress errors to the user. That allows you to provide the user with friendly messaging in a production environment while allowing you to debug errors in a test environment.

 

So, let's say you have a loop to iterate over five salespeople to provide their sales data. In that loop you call a method to get the salesperson's data. If that method fails then you can't generate the output for that sales person. That might be a good place to implement an exception. So, the method would throw the exception (e.g. "Unable to get sales data for $salesPerson") and the method would return false to the calling code. That code would see the false result and provide something such as "Sales data unavailable at this time." for that particular sales person. The user would only see that and not the exception (assuming error reporting was at the appropriate level).

Link to comment
Share on other sites

Wow. That was a well written explanation. So an example for the "Unable to get sales data for $salesPerson") How would I be able to send an email to my self, at when catching the results? Will I use the error_get_last() function and have it emailed to me?

Link to comment
Share on other sites

Wow. That was a well written explanation. So an example for the "Unable to get sales data for $salesPerson") How would I be able to send an email to my self, at when catching the results? Will I use the error_get_last() function and have it emailed to me?

 

Well, just be sure that you want to receive such an email. At a minimum you would want to log them so the data is in one place instead of tracking down emails. As for using error_get_last(), well that only tells you about PHP errors. A function to get a salesperson's data could just as easily malfunction in the DB call. Of course, you could check for that and then throw a PHP error with the details of the DB error. Then you could get the data using error_get_last(). At this point, we're down to personal preference on how to build a good error reporting framework. You could just as easily build your own function to call when any type of error occurs, pass it the file name, line number, actual error message, relevant variable values, etc. etc.

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.