Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Try-catch not working - why does this throw an Exception?

Reply
Thread Tools

Try-catch not working - why does this throw an Exception?

 
 
Jon Maz
Guest
Posts: n/a
 
      10-25-2004
Hi All,

The following code throws a "CS0020: Division by constant zero Exception".
I would have expected to see a nice, friendly "caught an exception" written
to the screen.

Can anyone explain?

TIA,

JON


<%@ Page Language="C#" %>

<script runat="server">
public void Page_Load(Object sender, EventArgs e)
{
try
{
int blob = 1/0;
}
catch(Exception ex)
{
Response.Write("caught an exception");
}
}
</script>



 
Reply With Quote
 
 
 
 
Jon Maz
Guest
Posts: n/a
 
      10-25-2004
Aha... Then the fact that inline .aspx pages only compile when they're first
run makes no difference?

J


 
Reply With Quote
 
 
 
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      10-25-2004
Jon Maz <(E-Mail Removed)> wrote:
> The following code throws a "CS0020: Division by constant zero Exception".
> I would have expected to see a nice, friendly "caught an exception" written
> to the screen.
>
> Can anyone explain?


Sure - it's an error at compilation time, not at runtime. The catch
block only "counts" at runtime.

--
Jon Skeet - <(E-Mail Removed)>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
Karl Seguin
Guest
Posts: n/a
 
      10-25-2004
This isn't a runtime error (which try/catch work on) but an actual
compilation error...

If you change your code to:
int test = 0;
try {
int blob = 1/test;
} catch {
Response.Write("caught an exception");
}

you'll see the exception caught.

In other words, the Page_Load isn't actually getting executed, simply
compiled...and the compiler is thankfully letting you know there'll always
be an error with your code....always better to have compiler-time errors
than runtime exceptions.

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/


"Jon Maz" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hi All,
>
> The following code throws a "CS0020: Division by constant zero Exception".
> I would have expected to see a nice, friendly "caught an exception"

written
> to the screen.
>
> Can anyone explain?
>
> TIA,
>
> JON
>
>
> <%@ Page Language="C#" %>
>
> <script runat="server">
> public void Page_Load(Object sender, EventArgs e)
> {
> try
> {
> int blob = 1/0;
> }
> catch(Exception ex)
> {
> Response.Write("caught an exception");
> }
> }
> </script>
>
>
>



 
Reply With Quote
 
Karl Seguin
Guest
Posts: n/a
 
      10-25-2004
wow, I was late to the party on this one...

karl

--
MY ASP.Net tutorials
http://www.openmymind.net/


"Karl Seguin" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net>
wrote in message news:(E-Mail Removed)...
> This isn't a runtime error (which try/catch work on) but an actual
> compilation error...
>
> If you change your code to:
> int test = 0;
> try {
> int blob = 1/test;
> } catch {
> Response.Write("caught an exception");
> }
>
> you'll see the exception caught.
>
> In other words, the Page_Load isn't actually getting executed, simply
> compiled...and the compiler is thankfully letting you know there'll always
> be an error with your code....always better to have compiler-time errors
> than runtime exceptions.
>
> Karl
>
> --
> MY ASP.Net tutorials
> http://www.openmymind.net/
>
>
> "Jon Maz" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > Hi All,
> >
> > The following code throws a "CS0020: Division by constant zero

Exception".
> > I would have expected to see a nice, friendly "caught an exception"

> written
> > to the screen.
> >
> > Can anyone explain?
> >
> > TIA,
> >
> > JON
> >
> >
> > <%@ Page Language="C#" %>
> >
> > <script runat="server">
> > public void Page_Load(Object sender, EventArgs e)
> > {
> > try
> > {
> > int blob = 1/0;
> > }
> > catch(Exception ex)
> > {
> > Response.Write("caught an exception");
> > }
> > }
> > </script>
> >
> >
> >

>
>



 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      10-25-2004
Jon Maz <(E-Mail Removed)> wrote:
> Aha... Then the fact that inline .aspx pages only compile when they're first
> run makes no difference?


Makes no difference in what way? It certainly doesn't make a difference
to when errors occur. It's not your code that's throwing an exception -
it's the compiler, basically.

--
Jon Skeet - <(E-Mail Removed)>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
Jon Maz
Guest
Posts: n/a
 
      10-25-2004
Thanks for the help!

JON


 
Reply With Quote
 
Jon Maz
Guest
Posts: n/a
 
      10-25-2004
Yeah, Karl, you're getting a bit slow in your old age...



Actually your code snippet helped - I am bathed in the light of
understanding...

Thanks!

JON


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Why does std::stack::pop() not throw an exception if the stack is empty? Debajit Adhikary C++ 36 02-10-2011 08:54 PM
To throw or to throw not? Emanuele D'Arrigo Python 6 11-15-2008 04:12 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
JNI's throw new does not throw an exception yarona@m-sys.com Java 15 09-08-2005 08:36 AM
Throw Exception Vs Throw New Exception Kerri ASP .Net 2 10-27-2003 02:13 PM



Advertisments