473,320 Members | 1,951 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

try catch blocks

43
why do we use try catch blocks??
and what is the statement we write in catch's purpose?
Nov 28 '07 #1
5 1996
lotus18
866 512MB
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
115 100+
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
maloov
43
ok i actually need it in Visual Basic.Net (VB)..
Nov 29 '07 #4
Mohan Krishna
115 100+
ok i actually need it in Visual Basic.Net (VB)..
What more do you need? Are you looking for tutorials?
Nov 29 '07 #5
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

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

Similar topics

6
by: Erik Cruz | last post by:
Hi. I have read several articles recommending avoid to raise exceptions when possible, since exceptions are expensive to the system. Removing code from Try... Catch blocks can help performance?...
4
by: Chris Martin | last post by:
Below are three try-catch blocks. My question is, how can all three work, and how come there is no memory leak (or is there)? As I understand it, an exception object is being created and "thrown"...
4
by: Big D | last post by:
Hi all, I'm trying to understand the best way to use a try catch with a SQL transaction. I have a number of sql statements that need to be run, such as: 'open sqlConnection, get command...
8
by: Z D | last post by:
Hi, I was wondering what's the point of "finally" is in a try..catch..finally block? Isn't it the same to put the code that would be in the "finally" section right after the try/catch block?...
11
by: Pohihihi | last post by:
I was wondering what is the ill effect of using try catch in the code, both nested and simple big one. e.g. try { \\ whole app code goes here } catch (Exception ee) {}
5
by: PasalicZaharije | last post by:
Hallo, few days ago I see ctor like this: Ctor() try : v1(0) { // some code } catch(...) { // some code }
32
by: cj | last post by:
Another wish of mine. I wish there was a way in the Try Catch structure to say if there wasn't an error to do something. Like an else statement. Try Catch Else Finally. Also because I...
7
by: dick | last post by:
in the "try{throw}catch" structure, how the C++ code return the "type" thrown by a function?
5
by: not_a_commie | last post by:
Using a synchronized Queue, I did some testing on catching the "queue was empty on dequeue" exception vs. doing my own lock, checking for empty, and then dequeuing. The try/catch method, though...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.