eldan88 Posted September 27, 2013 Share Posted September 27, 2013 Hey. When reading and practicing exception handling, it seems like if/else and exception handling preform very similar tasks. I was just wondering on when it would be practical to use exception handling rather than if else statement? Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/282477-exception-handling-or-if-else/ Share on other sites More sharing options...
Psycho Posted September 27, 2013 Share Posted September 27, 2013 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). Quote Link to comment https://forums.phpfreaks.com/topic/282477-exception-handling-or-if-else/#findComment-1451470 Share on other sites More sharing options...
eldan88 Posted September 30, 2013 Author Share Posted September 30, 2013 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? Quote Link to comment https://forums.phpfreaks.com/topic/282477-exception-handling-or-if-else/#findComment-1451970 Share on other sites More sharing options...
Psycho Posted October 1, 2013 Share Posted October 1, 2013 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. Quote Link to comment https://forums.phpfreaks.com/topic/282477-exception-handling-or-if-else/#findComment-1451974 Share on other sites More sharing options...
eldan88 Posted October 1, 2013 Author Share Posted October 1, 2013 Okay I got it. Where I can read about building my own error reporting framework. Are there any specific PHP functions for handling DB errors? Quote Link to comment https://forums.phpfreaks.com/topic/282477-exception-handling-or-if-else/#findComment-1452047 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.