你为什么要用静态的构造函数?构造函数是在类实例话的时候自动调用的!而写静态的函数,是为了不用实例化就可以调用该函数,既然不用实例化,就不调用构造函数了!至于异常的出来,当然在用try{}catch(...)在catch块中处理!

解决方案 »

  1.   

    如果是c/s的话比较麻烦
    必须在每个使用它的地方来试图捕获异常
    所以可以考虑
    把可能导致异常的行为单独出来成为一个类或者是其他的什么如果是b/s可以调用统一的异常错误处理
      

  2.   

    using System;
    第一种:
    namespace cccccc
    {
    class Class1
    {
    public class a
    {
    public static string ExceptionStr;
    static a()
    {
    try
    {
    throw new NullReferenceException("构造函数异常");
    }
    catch(NullReferenceException e)
    {
    ExceptionStr = e.toString();
    }
    }
    }
    static void Main(string[] args)
    {
    a x = new a();
    System.Console.WriteLine(a.ExceptionStr);
    System.Console.ReadLine();
    }
    }
    }
    这样可以获得异常的详细内容第二种:
    using System;namespace cccccc
    {
    class Class1
    {
    public class a
    {
    static a()
    {
    throw new NullReferenceException("构造函数异常");
    }
    }
    static void Main(string[] args)
    {
    try
    {
    a x = new a();
    }
    catch(Exception e)
    {
    System.Console.WriteLine(e.Message);
    }
    System.Console.ReadLine();

    }
    }
    }
    这样的话可以捕获异常,但是得到的值和抛出的值就不是一样了,因为这儿的Exception e 不等同与NullReferenceException,而在这儿又不可能用NullReferenceException(如果用这个自定义异常会出现运行时错误)总之这个问题不大好解决,上面的两种都不是很让人满意,不知道有没那位高人知道,顺便给楼上几位解释一下静态构造函数:静态构造函数自动被调用,不能被显式调用。虽然提供了许多约束条件,但是静态构造函数执行的确切时间和顺序是不确定的:
    一个类的静态构造函数在这个类的任何实例被创建前执行。
    一个类的静态构造函数在类的任何静态成员被引用前执行。
    一个类的静态构造函数在它的所有派生类的静态构造函数执行之后执行。
    一个类的静态构造函数从不会被执行一次以上。
      

  3.   

    谢谢13880079673(CMonkey)
    按你的第二种捕获方式的确不能得到正确的Exception,而且在调试的时候会报一个异常。
    看来只有在构造函数内就异常处理了。
      

  4.   

    作一个For循环,取内部的InnerException呢?我觉得这样应该可行
      

  5.   

    唔蝈蝈跑来讲了一大堆也没说究竟怎么弄..
    我把能想到的办法都试遍了,没有一个可行的,郁闷楼主碰到什么问题了非得这么设计?
    大部分情况下是不需要使用类型构造函数的,我这么认为类型在什么时候初始化,实在难以确定
    就算在程序的入口点这么写:
    try
    {
     MyClass mc = new MyClass();
    }
    catch...异常能捕获吗?还是不行,使用反射也不行,使用AppDomain的CreateInstance也不行
    Jeffer Richter的《.NET框架程序设计》也没说仔细,就说了蝈蝈总结的那些这种异常根本不能靠try-catch代码块捕获
    我觉得这个简直是无法完成的任务等待高人指点
      

  6.   

    public class Foo {
      static Bar bar ;  static {
        try {
           bar  = new Bar() ;
        } 
        catch ( Exception e ) {
          e.printStackTrace() ;
        }
      }
    }class Bar {
      public Bar ( ) throws Exception {
      }

      

  7.   

    Summary
    Represents the error that occurs when an exception is thrown inside the static constructor of a type.Description
    When a static constructor fails to initialize a type, a TypeInitializationException instance is created and passed a reference to the exception thrown by the static constructor. The System.TypeInitializationException.InnerException property stores the exception that was thrown by the static constructor.Example
    The following example demonstrates an error that causes a TypeInitializationException exception.using System;
    class TypeInit {
        // Static constructor
       static TypeInit () {
              // Throw an application-defined exception.
            throw new ApplicationException("Error in Class TypeInit");
        }
        public TypeInit() {}
    }
    class TestTypeInit {
        static public void Main() {
            try {
                TypeInit t = new TypeInit ();
            }
            catch (TypeInitializationException e) {
                Console.WriteLine("Error: {0}",e.InnerException);
            }
        }
    }