Results 1 to 10 of 10

Thread: Exception error messages

  1. #1

    Thread Starter
    Addicted Member riechan's Avatar
    Join Date
    Feb 2008
    Location
    Japan
    Posts
    254

    Exception error messages

    Can anyone give me some easy to understand exception-handled error messages, like this:

    Code:
    ...
    Catch parameter As SqlException
                MsgBox("Unable to save record due to invalid parameters.", MsgBoxStyle.Information, "Unable To Save Record")
                dbConn.Close()
    ...
    for the following events:

    - During database connection error
    - Error occurred uring saving, deleting
    - Unable to load data from database

    And any else you guys can come up. Kindly include what kind of exception that would be as well. Thanks!
    ====================
    ほんとにどもありがとう!

    Rie Ishida

  2. #2
    Banned
    Join Date
    Aug 2009
    Posts
    33

    Re: Exception error messages

    Example:

    sub mySub
    dim sqlCn as new SqlConnection(connString)
    try
    sqlcn.open
    'here do some thing
    catch ex
    msgbox ex.message
    finally
    sqlCn.close
    end try
    end sub

    ________________
    Sabrina Gage
    Last edited by si_the_geek; May 19th, 2010 at 05:55 PM. Reason: removed advertising link

  3. #3

    Re: Exception error messages

    Code:
    Catch parameter As SqlException
    You would just call this:
    Code:
    parameter.Message
    To get an easy to understand message.

  4. #4

    Thread Starter
    Addicted Member riechan's Avatar
    Join Date
    Feb 2008
    Location
    Japan
    Posts
    254

    Re: Exception error messages

    What kinds of exceptions does this line encompass/include?

    Code:
    Catch ex as Exception
    ...
    And what does the Finally block do?
    ====================
    ほんとにどもありがとう!

    Rie Ishida

  5. #5

    Re: Exception error messages

    Catch ex As Exception allows the programmer to 'catch' errors in their code. So lets say...you do this:
    vb.net Code:
    1. Try
    2.    'Stuff here trips an error
    3.    Catch ex As Exception
    4.         Messagebox.show(ex.ToString)
    5.    Finally
    6.         'Do ending stuff here
    7. End Try
    You can display the error message, which gives you a message and a stack trace as well as Inner Exceptions. These are crucial to debugging your code.

    Finally allows you to do any ending code that your sub might need to do. Generally used for disposing of objects (I think...don't quote me on that).

  6. #6

    Thread Starter
    Addicted Member riechan's Avatar
    Join Date
    Feb 2008
    Location
    Japan
    Posts
    254

    Re: Exception error messages

    Okay, am not quoting you on it.

    So, you can place stuff like closing the database connection in the Finally clause, just like in the post above. What if I use this:

    Code:
    Try
    ...
    Catch ex as exception
    msgbox(ex.message)
    Finally
    ...
    End Try
    Is ex.ToString the same as ex.message?
    ====================
    ほんとにどもありがとう!

    Rie Ishida

  7. #7

    Re: Exception error messages

    No, ex.Message is just the error message. ex.ToString is the entire thing. message, stacktrace, inner exception.

  8. #8

    Thread Starter
    Addicted Member riechan's Avatar
    Join Date
    Feb 2008
    Location
    Japan
    Posts
    254

    Re: Exception error messages

    Quote Originally Posted by formlesstree4 View Post
    No, ex.Message is just the error message. ex.ToString is the entire thing. message, stacktrace, inner exception.
    I guess you can use the ex.ToString for debugging. I'm thinking of using the ex.Message for displaying error messages that are a little bit more understandable to common users.

    Would you mind checking this for me, and tell me if there's anything wrong in it? Do I need to check if the dbconn is still open in the Finally clause, or is it okay if I just close it right away?

    Code:
                Try
                ...
                Catch parameter As SqlException
                MsgBox("An unexpected error occurred during saving. Please try again.", MsgBoxStyle.Information, "Error Occurred During Save")
            Catch invalidparameter As InvalidOperationException
                MsgBox(invalidparameter.Message, MsgBoxStyle.Exclamation, "Invalid Operation Error Occurred")
            Catch ex As Exception
                MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Runtime Error")
            Finally
                If dbConn.State = ConnectionState.Open Then
                    dbConn.Close()
                End If
            End Try
    ====================
    ほんとにどもありがとう!

    Rie Ishida

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Exception error messages

    Quote Originally Posted by riechan View Post
    So, you can place stuff like closing the database connection in the Finally clause, just like in the post above.
    You put code in the finally block that you need to execute whether an exception is thrown or not. The difference between using the Finally block and putting the code after the End Try is that the Finally block is executed even if there's a Return statement in the Try or Catch block.
    Quote Originally Posted by riechan View Post
    Is ex.ToString the same as ex.message?
    You don't need to ask us things that you can simply test for yourself. It would have taken about 30 seconds to write some code and run it and see what each produced. If your attitude is "look first, ask questions later" then you'll learn far quicker. If your first thought is to get someone else to do it for you then you're not going to learn as fast.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  10. #10

    Re: Exception error messages

    Quote Originally Posted by jmcilhinney View Post
    You put code in the finally block that you need to execute whether an exception is thrown or not. The difference between using the Finally block and putting the code after the End Try is that the Finally block is executed even if there's a Return statement in the Try or Catch block.
    Quite useful to know, thanks for clearing that up.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width