tags:

views:

85

answers:

4

Hi

I know it is not good to just put a try catch with Exception on it since that could lead to burying problems and other things.

I however still don't know if I should do it in my case.

My site is heavy ajax so I send json responses back. So right now I have identified some possible exceptions that could be raised like a null reference, sql db and etc.

So in the catch statement of these ones I would have a nice message to the user something like

a database error has occurred your stuff has not been saved

However I am thinking what happens if their is some other exception that I don't see right now. If that would happen I think the form would just hang and the user would not know what is going on.

So would it be better in this case to

  • catch exceptions that I identified -such as sql, outof range
    • log with elmah
    • show nice customized msg for each exception
  • catch Exceptions after
    • log with elmah
    • show some generic msg
    • come back and add that exception list.

So

catch(IndexOutOfRangeException ex)
{
    // log here
    // customized msg
}
catch(Exception ex)
{
    // log here
    // generic msg
    // come back and add another exception later on what actually failed.
}

instead of

catch(IndexOutOfRangeException ex)
{
    // log here
    // customized msg
}
+2  A: 

In low-level code it's usually bad form to catch exceptions there. In theory you want them to be thrown, so you know when there's a problem. It's usually best to catch ones you know that will occur, and deal with them. Then catch the unhandled ones at a much higher level.

Chad Moran
Well I am not using any low-level code. Plus I am using I log all my caught exceptions. So I am not sure for me it is hard call to say no I won't log it since potentially my users could be sitting there wondering what is happening.
chobo2
When I saw low-level I mean method-level, implementation code as you've shown.
Chad Moran
A: 

Hiding exceptions could not be a good idea because it would hide an error of the code, and would not allow you to understand what is wrong. Not all the errors should be shown to the users who use that knowledge to attach your site.

Rather than showing an error to the user, I would log it in a database, or in a file; you can show to the user an error message, but the error message could a code that only you know what it means. It's what is done from a cache server, that returns to the user a error code called guru meditation.

My answer is then: catch all the exceptions, even the ones you didn't think could be raised. This will allow you to control the error message users would see.

kiamlaluno
Well thats my intention. I not going to give them some stack track. I am also not hiding an the error message per say since I log in ELMAH(a error logging tool) so I would know about it. It's just I am thinking if I don't catch all exceptions and I forget one the user might left in the dark. I will still know what happened cuz of ELMAH but the user wont. So thats why I am thinking catch all exceptions as last resort display my own msg to user. Once I see it look into what happened and maybe add another catch.
chobo2
What I mean is that is fine to show an error message to users, but the error should not give any hints on the software you are using, to avoid such details can be used to attach your site. In some places the code should catch all the left exceptions, to allow you to control the message users would see. Users that are logging in are not interested to know that the query "SELECT * FROM drupal.users WHERE name = 'xyz'" is failed; they just need to know there is an error on the server side, and they need to try again later.
kiamlaluno
Well my messages are like "Failed to save your information due to database error" or "Failed to save your information due to an unkwon error".
chobo2
A: 

Catch the exceptions that you know you can deal with or do something with and take appropriate action to correct the problem.

If the exception is unexpected, log as much information as possible and notify the user.

Generally speaking, users don't care if it was a database error or a file write permission error or whatever. All they need to be told is that something went wrong.

I guess my point is that you should only catch specific exceptions if you plan to do something specific that relate to those exceptions, otherwise a general catch of the Exception class to deal with the unknown should be all you need.

Sir Psycho
A: 

You can catch an Exception, log it, and then re-throw it if you want a different part of the program to handle it. Or you can just let it bubble up and log it and handle it at a higher level of the stack if you wish.

Joe Philllips