By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,988 Members | 1,476 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,988 IT Pros & Developers. It's quick & easy.

try catch blocks

P: 43
why do we use try catch blocks??
and what is the statement we write in catch's purpose?
Nov 28 '07 #1
Share this Question
Share on Google+
5 Replies


lotus18
100+
P: 866
why do we use try catch blocks??
and what is the statement we write in catch's purpose?
Try and Catch I think like in any other high programming languages like java, they are used to execute statement if it is true/correct else it will catch error.

Rey Sean
Nov 28 '07 #2

Mohan Krishna
100+
P: 115
why do we use try catch blocks??
and what is the statement we write in catch's purpose?
Hi Maloov !
In which Language do you need?
OK ! In OOPs, learn exception handling concept.
Try ... for the code to be tested
Catch ... if the test produces any error

ALL THE BEST !
Nov 28 '07 #3

P: 43
ok i actually need it in Visual Basic.Net (VB)..
Nov 29 '07 #4

Mohan Krishna
100+
P: 115
ok i actually need it in Visual Basic.Net (VB)..
What more do you need? Are you looking for tutorials?
Nov 29 '07 #5

P: 88
why do we use try catch blocks??
and what is the statement we write in catch's purpose?
as the others have stated, you use try...catch to catch errors.
Expand|Select|Wrap|Line Numbers
  1. Try
  2.    ' any code that might generate an error here, for example connecting to a database
  3. Catch ex As Exception
  4.    ' react to the error here. in this example, I want to throw the error
  5.    Throw ex
  6. End Try
if you use the above code, it differs little from just running the code without try...catch. but if you change the code in the catch statement you can handle the error much more smoothly.
in an asp.net web application i wrote some time ago, i created a public class called ErrorHandling, in which there was a subroutine that emailed the error message to the administrator of the website. I won't show you the whole class, but everywhere i used code that could potentially cause an error, i put it within a try...catch block as follows:
Expand|Select|Wrap|Line Numbers
  1. Try
  2.    ' any code that might generate an error here, for example connecting to a database
  3. Catch ex As Exception
  4.    ' send an email to the administrator of the site. 
  5.    ErrorHandling.SendError(ex.Message)
  6. End Try
the call to ErrorHandling.SendError() is a custom call to a class that I created myself - it won't work for you unless you create a class also.

another thing that has not been mentioned in this thread is the third, optional, part of the try...catch statement: finally. it is really simple to understand - it just means that the code in that block will be run regardless of whether an error occurs. an example might show what i mean:
Expand|Select|Wrap|Line Numbers
  1. Try
  2.    ' any code that might generate an error here, for example connecting to a database
  3. Catch ex As Exception
  4.    ' send an email to the administrator of the site. 
  5.    ErrorHandling.SendError(ex.Message)
  6. Finally
  7.    MsgBox("This message box will be shown no matter what")
  8. End Try
i hope this gives you a picture of how try...catch can be used to handle errors smoothly improve the user experience of an application.
Nov 29 '07 #6

Post your reply

Sign in to post your reply or Sign up for a free account.