r/visualbasic • u/Faqueuetu • Mar 14 '16
Add Try/Catch en masse to program with no error traps?
So full disclosure, I am not a programmer but thanks to Google and persistence I have actually put together a functioning program to import CSV's to an Access Database (It's NOT pretty under the hood) that I am happy with.
The problem I have is that this program is fairly large at close to 2000 lines and I didn't even understand error trapping when I started.
My question is, is there a way to add a Try/Catch to every subroutine at once, or do I just need to bite the bullet and get to dropping them in manually? Sorry if this is a silly question.
Thanks for taking the time to read my question.
3
u/tweq Mar 14 '16
You should only catch exceptions in places when and where you can actually do something sensible with them. Adding a try/catch to every method and just suppressing all exceptions is terrible practice, and will only cause your program to enter unpredictable states that might actually cause more errors down the road.
1
u/Faqueuetu Mar 14 '16
Yea, I had a little time this morning to read up on error traps and I fully intend on going back after I show the program tomorrow to do the error traps right, or as close as I can get to right at least.
I am just going to demo it tomorrow and make note of any changes/additions I am just looking for a solution to stop the complete crash.
2
Mar 14 '16
If you post your code I bet you'd receive a lot of tips on optimization that would help with stability and with future projects.
4
u/AberrantCheese Mar 14 '16
You'll have to add them in manually. As a stop gap, you can also put in a 'global' try/catch by way of My.Application.UnhandledException Event. Basically, it's all-encompassing error trap. Note that it isn't a good idea to soley depend on this for all your error handling needs but it'll get you started until you can go back and add your try/catches.