473,326 Members | 2,655 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

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

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>

Nov 18 '05 #1
7 1914
Jon Maz <jo****@surfeuNOSPAM.de> 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 - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 18 '05 #2
Aha... Then the fact that inline .aspx pages only compile when they're first
run makes no difference?

J
Nov 18 '05 #3
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" <jo****@surfeuNOSPAM.de> wrote in message
news:up**************@TK2MSFTNGP11.phx.gbl...
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>

Nov 18 '05 #4
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:OH**************@TK2MSFTNGP15.phx.gbl...
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" <jo****@surfeuNOSPAM.de> wrote in message
news:up**************@TK2MSFTNGP11.phx.gbl...
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>


Nov 18 '05 #5
Jon Maz <jo****@surfeuNOSPAM.de> 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 - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 18 '05 #6
Thanks for the help!

JON
Nov 18 '05 #7
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
Nov 18 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

39
by: Erlend Fuglum | last post by:
Hi everyone, I'm having some trouble sorting lists. I suspect this might have something to do with locale settings and/or character encoding/unicode. Consider the following example, text...
12
by: Brian Kelley | last post by:
def res(): try: a = 1 return finally: print "do I get here?" res() outputs "do I get here?"
1
by: Askari | last post by:
Hi, In my program (too long for cut-paste) : http://www.cvm.qc.ca/9974331/Temp/crypte.py When I run and click on the button "Crypter!", a thread is run for the method "def crypte():" (line...
9
by: David Stockwell | last post by:
In referring to my copy of the python bible, it tells me I can't use all three items 'try' except and finally. I can use the t/f or t/e combinations though What combination can i use if i want...
26
by: djw | last post by:
Hi, Folks- I have a question regarding the "proper" use of try: finally:... Consider some code like this: d = Device.open() try: d.someMethodThatCanRaiseError(...) if SomeCondition: raise...
40
by: Steve Juranich | last post by:
I know that this topic has the potential for blowing up in my face, but I can't help asking. I've been using Python since 1.5.1, so I'm not what you'd call a "n00b". I dutifully evangelize on the...
4
by: wk6pack | last post by:
Hi, I was wondering why when I declare the dim variable outside the try statement, I could use the .dispose() function but when I declare it inside the try statement, I get Name 'varname' is not...
3
by: Sori Schwimmer | last post by:
Hi, I think that would be useful to have an improved version of the "try" statement, as follows: try(retrys=0,timeout=0): # things to try except: # what to do if failed
20
by: John Salerno | last post by:
I'm starting out with this: try: if int(text) 0: return True else: self.error_message() return False except ValueError: self.error_message()
5
by: Ed Jensen | last post by:
I'm using: Python 2.3.2 (#1, Oct 17 2003, 19:06:15) on sunos5 And I'm trying to execute: #! /usr/bin/env python try:
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.