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
Re: Exception error messages
Code:
Catch parameter As SqlException
You would just call this:
To get an easy to understand message.
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?
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:
Try
'Stuff here trips an error
Catch ex As Exception
Messagebox.show(ex.ToString)
Finally
'Do ending stuff here
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).
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?
Re: Exception error messages
No, ex.Message is just the error message. ex.ToString is the entire thing. message, stacktrace, inner exception.
Re: Exception error messages
Quote:
Originally Posted by
formlesstree4
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
Re: Exception error messages
Quote:
Originally Posted by
riechan
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
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.
Re: Exception error messages
Quote:
Originally Posted by
jmcilhinney
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.