# ASP Try...Catch Question

**URL:** <https://boards.straightdope.com/t/asp-try-catch-question/337745>\
**Category:** Factual Questions\
**Created:** [December 30, 2005, 5:18pm UTC](https://boards.straightdope.com/t/asp-try-catch-question/337745 "2005-12-30T17:18:01Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![ishamael69](https://avatars.discourse-cdn.com/v4/letter/i/b9e5f3/32.png) [@ishamael69](https://boards.straightdope.com/u/ishamael69)\
**Post date:** [December 30, 2005, 5:18pm UTC](https://boards.straightdope.com/t/asp-try-catch-question/337745/1 "2005-12-30T17:18:01Z")

</div>

I’ve got a do loop

example:  
Do While X \< 20  
…A lot of code  
Loop

Now, I need to catch an error if X cannot be converted to a numeric value. However, I do not want to catch anything else in the Do loop (which may include the same class of error). Is there a way to accomplish this?

---

<div class="post-metadata">

**Author:** ![Athena](https://avatars.discourse-cdn.com/v4/letter/a/35a633/32.png) [@Athena](https://boards.straightdope.com/u/Athena)\
**Post date:** [December 30, 2005, 5:43pm UTC](https://boards.straightdope.com/t/asp-try-catch-question/337745/2 "2005-12-30T17:43:13Z")

</div>

What kind of data type is X? A variant or an object or something?

An easy way to do something like this is to include a try/catch before the loop. Something like:

try  
{  
}

---

<div class="post-metadata">

**Author:** ![Athena](https://avatars.discourse-cdn.com/v4/letter/a/35a633/32.png) [@Athena](https://boards.straightdope.com/u/Athena)\
**Post date:** [December 30, 2005, 5:44pm UTC](https://boards.straightdope.com/t/asp-try-catch-question/337745/3 "2005-12-30T17:44:17Z")

</div>

What kind of data type is X? A variant or an object or something?

An easy way to do something like this is to include a try/catch before the loop. Something like:

try  
{  
int y = x; //this will error out if x can’t be converted

}  
catch (Exception exp)  
{  
throw (new MyCustomException());

}

---

<div class="post-metadata">

**Author:** ![Athena](https://avatars.discourse-cdn.com/v4/letter/a/35a633/32.png) [@Athena](https://boards.straightdope.com/u/Athena)\
**Post date:** [December 30, 2005, 5:48pm UTC](https://boards.straightdope.com/t/asp-try-catch-question/337745/4 "2005-12-30T17:48:20Z")

</div>

Mods, mind removing my fat finger half reply up above? Thankee!

---

<div class="post-metadata">

**Author:** ![ishamael69](https://avatars.discourse-cdn.com/v4/letter/i/b9e5f3/32.png) [@ishamael69](https://boards.straightdope.com/u/ishamael69)\
**Post date:** [December 30, 2005, 11:05pm UTC](https://boards.straightdope.com/t/asp-try-catch-question/337745/5 "2005-12-30T23:05:31Z")

</div>

X is from a COM object (actually it is the value of one cell of a spreadsheet), and it may or may not be valid each time I go through the loop.

So, I guess I could do:

try  
{  
…int y = x; //this will error out if x can’t be converted  
}  
catch (Exception exp)  
{  
…throw (new MyCustomException());  
}

while (y \<20)  
{  
…//Do what I need to do here  
…  
…  
…try  
…{  
…int y = newx; //this will error out if x can’t be converted  
…}  
…catch (Exception exp)  
…{  
…throw (new MyCustomException());  
…}  
}

(sorry if my C# skills are lacking.

---

<div class="post-metadata">

**Author:** ![Imasquare](https://avatars.discourse-cdn.com/v4/letter/i/5f9b8f/32.png) [@Imasquare](https://boards.straightdope.com/u/Imasquare)\
**Post date:** [December 30, 2005, 11:10pm UTC](https://boards.straightdope.com/t/asp-try-catch-question/337745/6 "2005-12-30T23:10:58Z")

</div>

> [@ishamael69](#):
>
> Is there a way to accomplish this?

Are you using [ASP.NET](http://ASP.NET) or classic ASP? If [ASP.NET](http://ASP.NET) then try Athena’s solution, but for the old-fashioned style of ASP this might work (can’t guarantee cause I’ve only got [ASP.NET](http://ASP.NET) now):

Do While X \< 20  
…If Not IsNumeric(X) Then  
… ’ Handle the Error  
…Exit Do  
…Else  
…A lot of code  
…End If  
Loop

---

<div class="post-metadata">

**Author:** ![Armilla](https://avatars.discourse-cdn.com/v4/letter/a/e19b73/32.png) [@Armilla](https://boards.straightdope.com/u/Armilla)\
**Post date:** [December 31, 2005, 2:28am UTC](https://boards.straightdope.com/t/asp-try-catch-question/337745/7 "2005-12-31T02:28:08Z")

</div>

By the look of your example code you’re using [VB.Net](http://VB.Net). In this case it’d be best to test the variable X before using it in the loop. VB has the IsNumeric function, but this may not be specific enough for you since it will return true if the variable can be converted into an integer, double, single etc.

The most precise way of dealing with it would be to declare another variable and do a type-specific conversion inside a catch:

```auto

Dim iX as Integer
Try
    iX = Integer.Parse(X)
Catch e As Exception
    ' Do whatever you need to here when it won't convert
End Try

```

This example is if you expect X to be an Integer, but it will be similar for other numeric types.

If you’re using the 2.0 framework then there is a final CanParse (or similar, my memory fails me) method on the value class that will tell you whether the input is in the correct format.

---

<div class="post-metadata">

**Author:** ![ishamael69](https://avatars.discourse-cdn.com/v4/letter/i/b9e5f3/32.png) [@ishamael69](https://boards.straightdope.com/u/ishamael69)\
**Post date:** [December 31, 2005, 4:19pm UTC](https://boards.straightdope.com/t/asp-try-catch-question/337745/8 "2005-12-31T16:19:56Z")

</div>

Yes, I am using [ASP.NET](http://ASP.NET) w/ [VB.NET](http://VB.NET) 1.1.

The problem with just doing the try catch block before the loop is that the value changes each time. What I’m going to try now:

If Double.TryParse(X) Then  
…Do While X \< 20  
…do something

…Do While Not Double.TryParse(X)  
…change the value of X  
…Loop  
…Loop  
End If
