r/visualbasic • • 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.

5 Upvotes

7 comments sorted by

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.

1

u/Faqueuetu Mar 14 '16

Thank you for this tidbit. Basically I have to show it to my boss tomorrow and I'm still polishing the gui so time is limited. I can use the global and then after tomorrow go back and do it right.

Thanks again.

2

u/chrwei Mar 14 '16

the global event is good for putting more friendly messages on generic errors, and also for replacing the default messagebox with something nicer. for instance I made a custom message box that has "continue" instead of "OK" after repeated "how do i continue, do I press ok?" support calls, and also has a "copy to clipboard" button to make it easier for users to send me the message.

in the long run, error conditions do need to be handled specifically. such as on your import, some exceptions you'll want to add the record to a list to be manually fixed, but others you might want to just skip the record and move on.

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

u/[deleted] 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.