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

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Karl Seguin (_at_)
Date: 10/25/04


Date: Mon, 25 Oct 2004 11:40:54 -0400

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" <jonmaz@surfeuNOSPAM.de> wrote in message
news:upb7DZquEHA.1288@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>
>
>
>


Relevant Pages

  • Re: Try-catch not working - why does this throw an Exception?
    ... This isn't a runtime error but an actual ... compilation error... ... > The following code throws a "CS0020: Division by constant zero Exception". ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: Problem overriding sys.excepthook
    ... > importing the traceback module at the beginning of the script, ... uncaught exception. ... is -- Python will compile the entire module before executing it. ... "import" statement (although AFTER compilation of the main module), ...
    (comp.lang.python)
  • Re: JScript exception...
    ... > Can somebody tell me why following script throws TypeError ... > exception in Internet explorer? ... A Runtime Error has occurred. ... > ReferenceError exception. ...
    (microsoft.public.scripting.jscript)
  • Re: SQL and VB.NET
    ... You also need an exception handler to ... LITTLERED;Integrated security = SSPI;initial catalog = stktrn" ... there is SQL connection created along with SQL ... > compile and run it I get this runtime error: ...
    (microsoft.public.vb.database)
  • Re: Linux 64 bit arch: running compiled magicsquare example on similar architecture
    ... architectures or a problem with the 7.6 version of the MCR. ... seemed to help resolve the floating point exception on the ... I will try the latest 2007b release compilation and porting ... development machine (a 64-b Linux) the magicsquare example. ...
    (comp.soft-sys.matlab)