|
-
Jan 25th, 2010, 11:51 AM
#1
Thread Starter
Addicted Member
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
-
Jan 25th, 2010, 09:48 PM
#2
Banned
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
-
Jan 25th, 2010, 10:36 PM
#3
Re: Exception error messages
Code:
Catch parameter As SqlException
You would just call this:
To get an easy to understand message.
-
Feb 9th, 2010, 12:17 AM
#4
Thread Starter
Addicted Member
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
-
Feb 9th, 2010, 12:24 AM
#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:
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).
-
Feb 9th, 2010, 12:46 AM
#6
Thread Starter
Addicted Member
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
-
Feb 9th, 2010, 12:57 AM
#7
Re: Exception error messages
No, ex.Message is just the error message. ex.ToString is the entire thing. message, stacktrace, inner exception.
-
Feb 9th, 2010, 01:01 AM
#8
Thread Starter
Addicted Member
Re: Exception error messages
 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
====================
ほんとにどもありがとう!
Rie Ishida
-
Feb 9th, 2010, 01:05 AM
#9
Re: Exception error messages
 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.
 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.
-
Feb 9th, 2010, 01:08 AM
#10
Re: Exception error messages
 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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|