When to use Try...Catch

Posted on 2001-09-17
Last Modified: 2010-05-02
I guess this is more of a general programming question than a VB question, but maybe someone can give me an answer.

When is it appropriate to use Try...Catch statements?  Should I put them around every line of code in my application, or only use them in certain instances?  Do they effect performance?  Are there any general guidelines?

Question by:NJordan72
  • 3
  • 2
LVL 44

Accepted Solution

Arthur_Wood earned 15 total points
ID: 6488042
Try...Catch...Finally... is used for the purpose of "Exception" handling - which is the more general case of what is generally refered to as Error Handling.

Try...Catch... is generally used to "protect" blocks of code where you "expect" or "anticipate" that errors (exceptions to normal execution) might occur.  If an exception DIOES occur in the Try block, the code will immediately jump to the "Catch" block, where you would typically provide code to "handle" the exception.

The General mechanisms for Exception hadling, when to do it, and how to do it, is one of the major areas of learning hw to be an effective programmer.  THis topic IDS NOT one which can be easily explained in a restricted forum such as this.  Generally, to become adept at handling errors (exception) may in fact require several years of Programming experience.

Expert Comment

ID: 6488100
The Visual C++ area would be a better place to post this question.  Visual Basic does not support Try Catch Finally blocks, it only supports On Error Goto/Resume statements.
LVL 44

Expert Comment

ID: 6488105
I took the question as refering to VB.NET --which DOES suport Try..CAtch..Finally...
Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.


Expert Comment

ID: 6488154
True, I seldom think about .Net since it will be in beta for many months to come.

Author Comment

ID: 6488208
I am using .NET.. Thanks for the responses!
LVL 44

Expert Comment

ID: 6488230
I would suggest picking up a copy of

Professional VB.Net
WROX Press  ISBN 1-861004-97-4

It has a very good chapter on Exception Handling (Chapter 9)

Arthur Wood

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question