Why doesn't try..catch work on my web page?

R

Rob Nicholson

Why doesn't try..catch work on my asp.net page?

Try
Dim n As Integer = 10
n = n / 0
Catch ex As Exception
' ignore error
End Try

When I single step over the n=n/0 line, it goes straight to the standard
ASP.NET error page. Why isn't the TRY ignored?

Thanks, Rob.
 
R

Rob Nicholson

not of type Exception?

Sorry don't understand. Same code in a VB client program goes into the Catch
section.

Thanks, Rob.
 
R

Rob Nicholson

not of type Exception?

Ahh, yes I do :) I'm building a library function and I've created an
object/class in that called Exception. Changing it to Catch ex As
System.Exception worked.

Hmm, that's going to catch one out especially as VB creates the Catch ex as
Exception bit when you type Try<CR>. I think I'll rename my own exception
class.

Cheers, Rob.
 
K

Karl Seguin

Rob:
Funny, works for me..the exception is ignored and everything keeps
working....when I move the division outside the try/catch it craps out as
expected.

Perhaps some more context...or an actual full sample...for example, this
doesn't generate an error for me:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
</html>

<Script language="vb" runat="server">
Sub Page_Load
Try
Dim n As Integer = 10
n = n / 0
Catch ex As Exception
' ignore error
End Try
End Sub
</Script>

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/index.aspx - New and Improved (yes, the popup is
annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)
 
R

Rob Nicholson

Funny, works for me..the exception is ignored and everything keeps
working....when I move the division outside the try/catch it craps out as
expected.

Caught by my own cleverness or rather a bit of slack "wizard" functionality
by VB.NET

I'd created my own class called Exception so

Catch ex as Exception

Was actually:

Catch ex As Granite.Exception

Which is why it didn't work...

I actually think the VB.NET should generate a fully qualified line, i.e.

Catch ex As System.Exception

Cheers, Rob.
 
K

Kevin Spencer

All Exceptions are of type Exception, just as all Objects are of type
Object.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Neither a follower nor a lender be.
 
K

Kevin Spencer

I'm not sure what you mean by "it goes to the standard ASP.net error page,"
but I can tell you why no exception is being caught. None is being thrown.
In .Net, dividing any number by zero yields Infinity, and does not throw an
exception.

If your page is going to an error page, there could be another exceptin
happening in it somewhere.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Neither a follower nor a lender be.
 
R

Rob Nicholson

In .Net, dividing any number by zero yields Infinity, and does not throw
an exception.

Err, yes it does - it generates an arithmetic overflow.

Cheers, Rob.
 
R

Rob Nicholson

All Exceptions are of type Exception, just as all Objects are of type

Thats not quite true - all exceptions are of type System.Exception.
Technically an exception object is one which inherits the
System.ApplicationException base object. Which is what we're doing - we've
inherited the exception object as we wanted to store more information with
it. It works fine through the exception chain.

Cheers, Rob.
 
K

Kevin Spencer

Thanks Rob. Your post prompted me to do a little research, and it turns out
that I was partially right, and you were partially right. An attempt to
divide an integer or decimal by 0 yields a "DivideByZeroException." However,
dividing a double or a float by 0 yields Infinity, with no Exception.

However, in this case, my advice was wrong, as he was attempting to divide
an integer by 0.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Neither a follower nor a lender be.
 
K

Kevin Spencer

All Exceptions are of type Exception, just as all Objects are of type
Thats not quite true - all exceptions are of type System.Exception.

Picky. Do you always insist on the full NameSpace in order for a statement
to be correct? You should have quit when you were ahead! ;-)

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Neither a follower nor a lender be.
 
R

Rob Nicholson

Picky. Do you always insist on the full NameSpace in order for a statement
to be correct? You should have quit when you were ahead! ;-)

In this situation the answer is "Yes". The VB.NET wizard generates the
following code when you type Try<CR>:

Try
Catch ex As Exception
End Try

That is dangerous as you might not spot that what you really need is Catch
ex As System.Exception if you *happen* to have created your own class called
Exception. Which will take precedence. The net result is that the Catch
doesn't fire and your code crashes.

Rob.
 
M

Mark Rae

Ahh, yes I do :) I'm building a library function and I've created an
object/class in that called Exception. Changing it to Catch ex As
System.Exception worked.

Hmm, that's going to catch one out especially as VB creates the Catch ex
as Exception bit when you type Try<CR>. I think I'll rename my own
exception class.

Generally, that's why you'll typically see classes with names starting with
"C" e.g. CException, CRegistry, CDataAccess etc...
 
K

Kevin Spencer

In this situation the answer is "Yes".

Sloppy logic, Rob. What on earth made you think that I was somehow referring
to a class that I had created and named "Exception?" What I said was "All
Exceptions inherit Exception." From the context, it was clear that I was
referring to the ONLY "Exception" that all Exceptions inherit from. In fact,
even the original poster mentioned that his class inherits System.Exception.
It is not possible to create any type of Exception class that does NOT
inherit System.Exception, which is the base class for ALL Exceptions.

The VB.NET wizard generates the
following code when you type Try<CR>:

Try
Catch ex As Exception
End Try

That is dangerous as you might not spot that what you really need is Catch
ex As System.Exception if you *happen* to have created your own class
called Exception. Which will take precedence.

Well, darn. I suppose I should switch to VB.Net and let Wizards and
Designers type all my code for me. I apologize for my ignorance. I always
write my own try/catch blocks in C#.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Neither a follower nor a lender be.
 
R

Rob Nicholson

Generally, that's why you'll typically see classes with names starting
with "C" e.g. CException, CRegistry, CDataAccess etc...

Ahh, yes - that's a point of great discussion at the moment in the team. In
our last large VB6 project, all the classes were prefixed by C but that
mainly because of earlier projects in C++.

Trends change :) I thought the C prefix was old fashioned now?

Rob.
 
R

Rob Nicholson

Sloppy logic, Rob. What on earth made you think that I was somehow
referring to a class that I had created and named "Exception?" What I said
was "All Exceptions inherit Exception." From the context, it was clear
that I was

Sloppy quoting Kevin - if you are going to quote yourself, please do it
accurately. Changing your quote from "All Exceptions are of type Exception"
(incorrect) to "All Exceptions inherit Exception" (maybe correct) doesn't
help your argument.

Colliding function names have been problem with programming languages for
many years. Namespaces do resolve them but you can only be 100% guaranteed
that you uniquely qualify something if you do you the full name.

My original post was asking "Why is the exception being captured" and the
answer is because the Catch statement wasn't fully qualified and therefore
didn't execute as expected.

Rob.
 
R

Rob Nicholson

I'm with Kevin on this one...specifying System.Exception because maybe you
created your own Exception class is overkill....

And why do you think namespaces were invented?

Rob.
 
R

Rob Nicholson

Besides, how much code do you write which swollows exceptions?

Well one would hope that if you write with code that communications with
*anything* outside of memory based variables, that you write code that traps
and therefore swallows exceptions.

Rob.
 

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

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top