Handling exception in type constructor

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Martijn

    #1

    Handling exception in type constructor

    Hi,

    When I run the undermentioned code, I get the
    message: 'The type initializer
    for "ConsoleApplica tion1.Class2" threw an exception.'.
    Why not 'Attempted to divide by zero.'? I want to handle
    the exception in the Main method. Thanks in advance.

    using System;

    namespace ConsoleApplicat ion1
    {
    public class Class1
    {
    [STAThread]
    static void Main(string[] args)
    {
    try
    {
    Class2 c = new Class2();
    }
    catch(Exception ex)
    {
    Console.WriteLi ne
    (ex.Message);
    Console.Read();
    }
    }
    }

    public class Class2
    {
    static Class2()
    {
    int a = 2, b = 0;
    int c = a / b; //Error.
    }

    public Class2()
    {
    //
    }
    }
    }
Working...