Try, Catch - Where Am I Going Wrong?

R

RobP

Hi,

I thought I knew how error handling with Try, Catch worked and have
used it before but now I'm stuck on it, and even the most basic thing
isn't working how I expected it too which makes me question if I
understand it at all. I just did this little example:

------------------------------------------------------------
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://
www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@ Page Language="VB" %>

<script runat="server">

sub page_init(Src as Object, E as EventArgs)
Try
strTest = "hello"
Catch
response.write("strTest is an undeclared variable")
End Try

end sub

</script>

<html dir="ltr" xmlns="http://www.w3.org/1999/xhtml">
<body>
<form id="form1" runat="server">
</form>
</body>

------------------------------------------------------------

When I compile that, I expect to get a blank page saying "strTest is
an undeclared variable" as the code under Catch tells it to, but
instead I get a page compilation error - BC30451: Name 'strTest' is
not declared. Can anyone see why my error trapping doesn't work?

Thanks in advance for any ideas

Rob
 
F

Family Tree Mike

RobP said:
Hi,

I thought I knew how error handling with Try, Catch worked and have
used it before but now I'm stuck on it, and even the most basic thing
isn't working how I expected it too which makes me question if I
understand it at all. I just did this little example:

------------------------------------------------------------
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://
www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@ Page Language="VB" %>

<script runat="server">

sub page_init(Src as Object, E as EventArgs)
Try
strTest = "hello"
Catch
response.write("strTest is an undeclared variable")
End Try

end sub

</script>

<html dir="ltr" xmlns="http://www.w3.org/1999/xhtml">
<body>
<form id="form1" runat="server">
</form>
</body>

------------------------------------------------------------

When I compile that, I expect to get a blank page saying "strTest is
an undeclared variable" as the code under Catch tells it to, but
instead I get a page compilation error - BC30451: Name 'strTest' is
not declared. Can anyone see why my error trapping doesn't work?

Thanks in advance for any ideas

Rob

Catches are for runtime errors, not compiler errors.
 
R

RobP

Catches are for runtime errors, not compiler errors.

Thanks for that. I guess my example wasn't very useful (although I
didn't know Catches weren't for compiler errors, cheers!) and may have
been a red herring. My actual problem that prompted my confusion was I
was trying to bind some XML to a control and wanted to catch the error
if the results weren't in the expected format. I just worked out my
problem is as ASP.net version issue, as the original code I have
worked fine compiling under ASP.net 1.1 - only 2.* I was seeing the
error, so I have a clearer idea of what I need to look at.

Thanks for the reply.
 
C

Cor Ligthert[MVP]

Rob,

Catches are for runtime errors, not programmers errors.

For this is the IsNothing

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top