Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在c#自定义异常处理的情况下如何调用构造函数?_C#_Exception Handling - Fatal编程技术网

在c#自定义异常处理的情况下如何调用构造函数?

在c#自定义异常处理的情况下如何调用构造函数?,c#,exception-handling,C#,Exception Handling,这是我的节目 class Program { static void Main(string[] args) { throw new UserAlreadyLoggedInException("Hello"); } } public class UserAlreadyLoggedInException : Exception { public UserAlreadyLoggedInException(string message) : base(

这是我的节目

class Program
{
    static void Main(string[] args)
    {
        throw new UserAlreadyLoggedInException("Hello");
    }
}

public class UserAlreadyLoggedInException : Exception
{
    public UserAlreadyLoggedInException(string message) : base(message)
    {
        Console.WriteLine("Here");
    }    
}
现在,我知道基类构造函数在派生类构造函数之前运行。但是当我运行上面的代码时,结果是

     Here
     Unhandled Exception:Testing.UserAlreadyLoggedInException:Hello.

为什么在未处理之前打印“Here”…?

您必须先创建异常,然后才能抛出异常

  • 创建异常实例,该异常实例由新的UseralReadyLoggedException启动
  • UserAlreadyLoggedInException
    调用构造函数
  • 调用构造函数内部的
    控制台.WriteLine
  • 建造师完成
  • 抛出新创建的异常实例
  • 不会处理异常,因此应用程序错误处理程序会将错误写入控制台

  • 您首先必须创建异常,然后才能抛出异常

  • 创建异常实例,该异常实例由新的UseralReadyLoggedException启动
  • UserAlreadyLoggedInException
    调用构造函数
  • 调用构造函数内部的
    控制台.WriteLine
  • 建造师完成
  • 抛出新创建的异常实例
  • 不会处理异常,因此应用程序错误处理程序会将错误写入控制台

  • 异常在实例化和抛出后打印到控制台


    实例化打印“Here”,然后运行时捕获它并打印“Unhandled Exception:
    ToString()
    表示。

    在实例化并抛出异常后,将异常打印到控制台


    实例化打印“Here”,然后运行时捕获它并打印“Unhandled Exception:
    ToString()
    表示。

    如果您添加自己的try/catch,程序流将变得更加明显。请注意,Exception的构造函数不写任何内容,它只存储消息字符串以供以后使用

    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                throw new UserAlreadyLoggedInException("Hello");
            }
            catch (Exception e)
            {
                Console.WriteLine("My handled exception: {0}", e.Message);
            }
        }
    }
    
    public class UserAlreadyLoggedInException : Exception
    {
        public UserAlreadyLoggedInException(string message) : base(message)
        {
            Console.WriteLine("Here");
        }    
    }
    

    如果您添加自己的try/catch,程序流程将变得更加明显。请注意,Exception的构造函数不写任何内容,它只存储消息字符串以供以后使用

    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                throw new UserAlreadyLoggedInException("Hello");
            }
            catch (Exception e)
            {
                Console.WriteLine("My handled exception: {0}", e.Message);
            }
        }
    }
    
    public class UserAlreadyLoggedInException : Exception
    {
        public UserAlreadyLoggedInException(string message) : base(message)
        {
            Console.WriteLine("Here");
        }    
    }
    

    你为什么不试试这个:

    static class Program
    {
      static void Main()
      {
        throw new UserAlreadyLoggedInException("Hello");
      }
    }
    
    
    class LoginException : Exception
    {
      public LoginException(string message) : base(message)
      {
        Console.WriteLine("least derived class");
      }
    }
    
    class UserAlreadyLoggedInException : LoginException
    {
      public UserAlreadyLoggedInException(string message) : base(message)
      {
        Console.WriteLine("most derived class");
      }
    }
    
    您也可以尝试这样编写
    Main
    方法:

      static void Main()
      {
        var ualie = new UserAlreadyLoggedInException("Hello");
        Console.WriteLine("nothing bad has happened yet; nothing thrown yet");
        throw ualie;
      }
    
    因此,使用
    new
    关键字构造
    Exception
    实例不会“引发”或“抛出”异常。为此,您需要
    抛出
    throw
    语句首先计算
    throw
    关键字后面的表达式。该评估的结果将是对异常实例的引用。计算表达式后,
    抛出
    “抛出”表达式值引用的异常


    您的误解是,
    System.Exception
    的实例构造函数一运行,异常就会“爆炸”。事实并非如此。

    为什么不试试这个:

    static class Program
    {
      static void Main()
      {
        throw new UserAlreadyLoggedInException("Hello");
      }
    }
    
    
    class LoginException : Exception
    {
      public LoginException(string message) : base(message)
      {
        Console.WriteLine("least derived class");
      }
    }
    
    class UserAlreadyLoggedInException : LoginException
    {
      public UserAlreadyLoggedInException(string message) : base(message)
      {
        Console.WriteLine("most derived class");
      }
    }
    
    您也可以尝试这样编写
    Main
    方法:

      static void Main()
      {
        var ualie = new UserAlreadyLoggedInException("Hello");
        Console.WriteLine("nothing bad has happened yet; nothing thrown yet");
        throw ualie;
      }
    
    因此,使用
    new
    关键字构造
    Exception
    实例不会“引发”或“抛出”异常。为此,您需要
    抛出
    throw
    语句首先计算
    throw
    关键字后面的表达式。该评估的结果将是对异常实例的引用。计算表达式后,
    抛出
    “抛出”表达式值引用的异常



    您的误解是,
    System.Exception
    的实例构造函数一运行,异常就会“爆炸”。事实并非如此。

    @dividedbyzero:你还不明白什么?但在正常继承中,我认为基类构造函数在调用子构造函数之后立即运行,而不执行任何子构造函数代码。那么基础(消息)会怎么样呢?@dividedbyzero:这与继承无关。你能解释一下吗?@dividedbyzero:我想答案会。首先创建一个异常,并执行所有相关代码,然后抛出它。@dividedbyzero:您没有得到什么?但是在正常继承中,我认为基类构造函数在调用子构造函数之后立即运行,而不执行任何子构造函数代码。那么基础(消息)会怎么样呢?@dividedbyzero:这与继承无关。你能解释一下吗?@dividedbyzero:我想答案会。首先创建一个异常,并执行所有相关代码,然后抛出该异常。不是基本构造函数打印到控制台,而是显示抛出和未处理异常的环境。如果使用Visual Studio:通过在
    throw new userreadyloggedinexception(“Hello”)处设置断点进行调试并使用F11遍历它。这会编译吗
    UseralReadyLoggeDineException
    类具有
    ParentException
    的构造函数?@SriramSakthivel:否,已更正。我在此处未看到任何异常处理。您只需创建自己的异常类型,然后抛出它。正是按照这个顺序。所以
    “在这里”
    (创建了异常),然后“发生了未处理的异常…”(因为您没有处理它)。不清楚的是什么?不是基本构造函数打印到控制台,而是显示抛出和未处理异常的环境。如果您使用Visual Studio:通过在
    throw new userreadyloggedinexception(“Hello”)处设置断点进行调试并使用F11遍历它。这会编译吗
    UseralReadyLoggeDineException
    类具有
    ParentException
    的构造函数?@SriramSakthivel:否,已更正。我在此处未看到任何异常处理。您只需创建自己的异常类型,然后抛出它。正是按照这个顺序。所以
    “在这里”
    (创建了异常),然后“发生了未处理的异常…”(因为您没有处理它)。什么是不清楚的?