I am using asp.net and I am always struggling for a neat way to handle errors and if there are any, to pass the errormessage to the user. For example, I have an User class,
Either by wrapping in a using statement or having a Try/Finally to ensure the db connection is disposed, as that particular type of exception could be trouble for you.
This free book by Karl Seguin has a chapter on Exception Handling which should make things quite clear for you.
Bubble up the exception to the UI where it can be handled according to your style there. For unhandled exceptions, as mentioned below, ELMAH is very useful.
I think you missed the right way to do exception handling. Storing the text that is language/locale dependent in your database is not really a good idea. The error might also not be meaningful in the context in which it it executed, the caller of the method knows what is intended but the code that retrieves a value from a database does not know about the "higher goals" of your code! The description of the database error might also not interest users at all, for them the system just cannot get the list of users, that's all. For developers it is important to know what exactly got wrong. Putting this visible to users might also show them data you do not want them to see like table names, passwords, database user names, depending on what the exception contains (which you cannot control)
So now how to handle it:
onClick_Event
In your code (Database for e.g.) follow this principle:
This way you would always have an object in a consistent state and clean code.
It all depends on the type of issue you have with the query execution. Those issues could be anything from no records found to a connection failing. If the problem is something you can test for, which the absence of records would be, you can test for that and present your user with a message stating that no records were found. Even if a connection fails, you should be able to test for that and tell your user.
For issues you cannot test for, depending on the type of application you are writing, you may want to let the application fail and use some framework like ELMAH log and report the error.
try these links which come from my other post on this topic for more information
Code Analysis Team Blog
Martin Fowler - Fail Fast
MSDN on Exception Handling
Checked vs Unchecked Exceptions
Also, this excellent article was posted recently
If you don't want to use the usual try/catch and display error divs in the catch then there is a Page.Error event which you could use to catch handle errors with the context of your page. Within that you would call Server.GetLastError() to get the last exception thrown and then inform the user something went wrong by directing them to an error page, or by showing/hiding error elements on your page.