public class StringNotFound : Exception
    {
        public string str { get; set; }        public StringNotFound()
        {
        }        public StringNotFound(string name)
        {
            str = name;
        }        public StringNotFound(string name, Exception innerException)
            : base("String Not Found", innerException)
        {
            str = name;
        }        public StringNotFound(string name, string message, Exception innerException)
            : base(message, innerException)
        {
            str = name;
        }
    }
A,B 2个工程在一个solution下,A是类库工程,我在上面定义一个异常类,
步骤:
1.在A中会有代码直接抛出异常: throw StringNotFound(“myName”);
2.B引入类库A工程,然后调用步骤1里的代码。我在B中写个try-cathch语句来捕获异常,try{}
catch(Exception e)
{
    Console.WriteLine(e.InnerException.Message);
}我想让在B中捕获的异常信息更具体一些,比如是输出时 ”myName srting Not Found“,请问怎么实现?

解决方案 »

  1.   

    catch(Exception e)
    {
        Console.WriteLine(e.tostring());
    }
      

  2.   

    catch(ArgumentNullException e)
    {
        Console.WriteLine(e.Message);
    }
      

  3.   

    上面的错了!catch(ArgumentNullException e)
    {
      Console.WriteLine(e.tostring());
    }
      

  4.   

    忘了说了,现在异常只能显示e.InnerException.Message,就是说它能显示是哪个方法抛出的异常,
    但是我想让他显示我自己定义的异常类中的"myName srting Not Found",还有一点是有多个异常类,难道我每个都catch一遍?请问怎么做?
      

  5.   


    public class StringNotFound : Exception
    {
      
    public StringNotFound()
    {
    } public StringNotFound(string name)
    : base(name + " String Not Found")
    {
    }
      

  6.   

    现在的难点是通过反射掉的异常,它现在抛异常只能用catch(Exception e)才能捕获到,像用我自己写的异常类根本捕获不到!catch(StringNotFound e)