exception handling

hi friends
am new to c# and am developing a small app.
my question is how can i create global exception handler which displays/handles  error messages whenever there is one.

what am i trying at the moment is am using try..catch whereever i think the could b an error .for example

conn.ConnectionString = @"Application name=myapp.net;data source=myserver;database=mydb;user id=" + cUid+";pwd="+cPwd;

try

{

conn.Open(); // opening a sql server connection and there could b  errors    like       wrong pwd or user id

}

catch (Exception ex)

{

MessageBox.Show("Error message is " + ex.Message +

"\nError Source is " + ex.Source);

return;

}

but i want to write a global (something like on error go to ....) handler which fires whenever there is any error instead of writing try..catch..for each line that may create an error .
any ideas much appreciated




Answer this question

exception handling

  • GaryHend

    Hi,

    It is a good practice to have Exception Handling in each method where you expect that it is possible for some exception to happen. It is also possible to define a global exception handler at the global level for a Windows Form application.

    NOTE: The global exception handler needs to be used more like a backup in addition to the regular Exception Handling at the method level.

    For v1.0/v1.1 of Framework see the following link:
    http://pluralsight.com/blogs/craig/archive/2004/06/13/1455.aspx

    Look at the last post on the Thread below if you are using VS2005
    http://forums.microsoft.com/msdn/ShowPost.aspx PostID=53533

    Regards,
    Vikram

  • _anna

    ok. actually am going thru different articles from MS and other sites and examples i found ,for exception handling , are same as my 1st post. so i started out like that.
    looks like i need to read more on this.

    you seem to have good understanding on these and if you know any good links on exception handling that is much aprreciated Christian :)

  • _x3m

     cgraus wrote:

     Just catching the base Exception class means that you will catch exceptions that you really want thrown, to highlight problems in your code.


    I think i get it now Christian.an example on this would clear any doubts for me.
    if we take same sample code in my 1st post ,as you know,when creating a connection there can following possible errors.
    1. invalid login
    2.wrong password.

    in this case i'd displays error to users saying invalid user id or password instead throwing exception.
    how would you write exception handling for the abv scenario instead of what i was doing abv
    Thanks



  • Tony Morgan

    1.  Don't catch 'exception', catch the type of exception you reasonably expect to see happen in a piece of code, which you have a valid way of recovering from.  Showing a message box and returning doesn't put your data in a state that is usable, and is not really viable.  This sort of code should sit at the top level, as you've asked, and is really only useful if you want to write unhandled exceptions to a log file, for example.

    2.  Your entry point is a function with the signature static void main.  Wrap the code in here in a try catch, and you'll get the behaviour you want.  The most useful thing to log here is a stack trace as well as the exception type, so you know where it happened.



  • LtCondor

    thanks for post christian.
    actually i lost you with ur point 1.since am very new to c# did not get ur point.
    can i ask you to give me some examples please please .
    BTW my sample code was just an example. i want to write exception handler (probably a class or a program) that does various things with different errors nos.
    Thanks.

    otherwise you can point me to some site with some sample code for exception handling.
    Thanks again.

  • adavis2

    Thanks vikram.
    you always come back with some good examples and links.
    I appreciate ur help on this

  • Tim5827

    OK, basically, you should catch things like FileNotFoundException, rather than Exception.  That is, a try/catch should anticipate something that may go wrong, and which you have a way of dealing with.  Just catching the base Exception class means that you will catch exceptions that you really want thrown, to highlight problems in your code.

    You can't write a class or program to handle exceptions, because your exceptions need to be caught within the program/class they are thrown.  You may want to write a class for logging exceptions to disc, and call it from a try/catch handler in the static void main method, though.



  • nedde

    I'm sorry, I don't know of any links.  The only book I remember covering it in any detail is the Richter book on .NET in general.


  • Brad Alexander

    1. invalid login
    2.wrong password.



    These are not exceptions, your general code should check for and handle these.  Exception throwing is expensive, you shouldn't replace application logic with exceptions.



  • exception handling