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

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

P: n/a
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.
Nov 19 '05 #1
Share this Question
Share on Google+
21 Replies


P: n/a
not of type Exception?

--
Curt Christianson
Site & Scripts: http://www.Darkfalz.com
Blog: http://blog.Darkfalz.com
"Rob Nicholson" <ro***********@nospam-unforgettable.com> wrote in message
news:em**************@TK2MSFTNGP09.phx.gbl...
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.

Nov 19 '05 #2

P: n/a
> not of type Exception?

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

Thanks, Rob.
Nov 19 '05 #3

P: n/a
> 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.
Nov 19 '05 #4

P: n/a
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!)
"Rob Nicholson" <ro***********@nospam-unforgettable.com> wrote in message
news:em**************@TK2MSFTNGP09.phx.gbl...
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.

Nov 19 '05 #5

P: n/a
> 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.
Nov 19 '05 #6

P: n/a
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.

"Curt_C [MVP]" <software_AT_darkfalz.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
not of type Exception?

--
Curt Christianson
Site & Scripts: http://www.Darkfalz.com
Blog: http://blog.Darkfalz.com
"Rob Nicholson" <ro***********@nospam-unforgettable.com> wrote in message
news:em**************@TK2MSFTNGP09.phx.gbl...
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.


Nov 19 '05 #7

P: n/a
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.

"Rob Nicholson" <ro***********@nospam-unforgettable.com> wrote in message
news:em**************@TK2MSFTNGP09.phx.gbl...
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.

Nov 19 '05 #8

P: n/a
> 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.
Nov 19 '05 #9

P: n/a
> 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.
Nov 19 '05 #10

P: n/a
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.

"Rob Nicholson" <ro***********@nospam-unforgettable.com> wrote in message
news:uo**************@TK2MSFTNGP09.phx.gbl...
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.

Nov 19 '05 #11

P: n/a
>> 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.

"Rob Nicholson" <ro***********@nospam-unforgettable.com> wrote in message
news:uI**************@TK2MSFTNGP09.phx.gbl...
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.

Nov 19 '05 #12

P: n/a
> 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.
Nov 19 '05 #13

P: n/a
"Rob Nicholson" <ro***********@nospam-unforgettable.com> wrote in message
news:Ob**************@TK2MSFTNGP10.phx.gbl...
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.


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

P: n/a
> 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.

"Rob Nicholson" <ro***********@nospam-unforgettable.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
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.

Nov 19 '05 #15

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

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

Karl
--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is
annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)
"Kevin Spencer" <ke***@DIESPAMMERSDIEtakempis.com> wrote in message
news:OI****************@TK2MSFTNGP10.phx.gbl...
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.

"Rob Nicholson" <ro***********@nospam-unforgettable.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl... 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.


Nov 19 '05 #16

P: n/a
> 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.
Nov 19 '05 #17

P: n/a
> 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.
Nov 19 '05 #18

P: n/a
> 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.
Nov 19 '05 #19

P: n/a
> 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.
Nov 19 '05 #20

P: n/a
> 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.


Sorry Rob. The 2 phrases are equivalent. And there's no maybe about it. All
Exceptions DO inherit Exception.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
What You Seek Is What You Get.

"Rob Nicholson" <ro***********@nospam-unforgettable.com> wrote in message
news:ec**************@TK2MSFTNGP09.phx.gbl...
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.

Nov 19 '05 #21

P: n/a
>> I'm with Kevin on this one...specifying System.Exception because maybe
you
created your own Exception class is overkill....
Whoa, hey, I didn't say that. I said that I didn't have to mention the
namespace IN MY MESSAGE, as it was implied. For example, people often refer
in their messages to the "Page" class, rather than "System.Web.UI.Page."

In code, it is often necessary to use full or partial namespaces when you
have assemblies containing classes or namespaces having the same name in the
same project. Rob is correct here.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
What You Seek Is What You Get.

"Rob Nicholson" <ro***********@nospam-unforgettable.com> wrote in message
news:O7**************@TK2MSFTNGP10.phx.gbl... 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.

Nov 19 '05 #22

This discussion thread is closed

Replies have been disabled for this discussion.