Try...Catch Ex As Exception

Use this board to post your code snippets - tips and tricks
2 posts Page 1 of 1
Contributors
NoWayIn
VIP - Donator
VIP - Donator
Posts: 444
Joined: Sat Nov 21, 2009 11:16 pm

Try...Catch Ex As Exception
NoWayIn
Hi all !
This little snip will help you with unwanted errors, and it also give you the ability of what you want to do when an error hit.

So the little code is
Code: Select all
Try
'your code here
Catch Ex As Exception
MsgBox(ex.Message)
End Try
okay so let me explain a bit.

The Try is like saying, Try to run this code.
The Catch Ex As Exception - If there's no errors then VB will skip this, but if there's any error it will go here
and below Catch Ex As Exception you have MsgBox(ex.Message)
So if you have an error when the code try to execute, it will go to Msgbox(ex.Message)
Basically Ex.Message will tell you what your Error is

End Try will stop what you're trying to run

EDIT: Oh I also forgot something, Msgbox(ex.Message) will show you a Msgbox with the error, if you want to close your program after that error then just add
Code: Select all
End
right after Msgbox(ex.Message)
Last edited by NoWayIn on Sun Apr 11, 2010 6:24 am, edited 1 time in total.
User avatar
hungryhounduk
VIP - Site Partner
VIP - Site Partner
Posts: 2870
Joined: Mon Jul 27, 2009 11:58 am

good post NoWayIn :)
Image
2 posts Page 1 of 1
Return to “Quick Snips”