C# 阻止控制台应用程序在捕获异常后关闭

C# 阻止控制台应用程序在捕获异常后关闭,c#,C#,我已经用C#编写了一个使用SmartIrc4Net的IRC bot,它的目的只是在识别命令时提供信息 我的问题是,代码中可能会发生异常,导致应用程序关闭,但是否可以保持应用程序运行,而不显示任何“按任意键继续”消息。理想情况下,这应该只记录异常并继续 我知道我可以首先管理异常,但基于每个命令验证所有输入将花费很长时间。或者可能还有其他我可能没有涵盖的例外情况 static void Main(string[] args) { IrcClient bot = new IrcClient()

我已经用C#编写了一个使用SmartIrc4Net的IRC bot,它的目的只是在识别命令时提供信息

我的问题是,代码中可能会发生异常,导致应用程序关闭,但是否可以保持应用程序运行,而不显示任何“按任意键继续”消息。理想情况下,这应该只记录异常并继续

我知道我可以首先管理异常,但基于每个命令验证所有输入将花费很长时间。或者可能还有其他我可能没有涵盖的例外情况

static void Main(string[] args)
{
    IrcClient bot = new IrcClient();

    // attach events

    try
    {
        // connect to server, login etc

        // here we tell the IRC API to go into a receive mode, all events
        // will be triggered by _this_ thread (main thread in this case)
        // Listen() blocks by default, you can also use ListenOnce() if you
        // need that does one IRC operation and then returns, so you need then
        // an own loop
        bot.Listen();

        // disconnect when Listen() returns our IRC session is over
        bot.Disconnect();
    }
    catch (ConnectionException e)
    {
        Console.WriteLine("Couldn't connect! Reason: " + e.Message);
        Console.ReadLine();
    }
    catch (Exception e)
    {
        Console.WriteLine(">> Error: " + e);
    }
}

将程序包装在
while(true)
块中

static void Main(string[] args)
{
    while(true){
        IrcClient bot = new IrcClient();

        // attach events
        try
        {
            // connect to server, login etc

            // here we tell the IRC API to go into a receive mode, all events
            // will be triggered by _this_ thread (main thread in this case)
            // Listen() blocks by default, you can also use ListenOnce() if you
            // need that does one IRC operation and then returns, so you need then
            // an own loop
            bot.Listen();

            // disconnect when Listen() returns our IRC session is over
            bot.Disconnect();
        }
        catch (ConnectionException e)
        {
            Console.WriteLine("Couldn't connect! Reason: " + e.Message);
        }
        catch (Exception e)
        {
            Console.WriteLine(">> Error: " + e);
        }
    }
}
代码中可能会发生异常,从而导致应用程序 关闭,但是否可以保持应用程序运行而不关闭 显示任何“按任意键继续”消息

嗯。。。是的,你可以这样写你的应用程序,但我可以保证这不是你认为的简单的解决方法。当抛出异常时,表示出现了问题。你不会神奇地耸耸肩,不顾一切地坚持下去,这样做可能会导致更多的事情出错

想象一下,您拥有打开一个文件的代码,然后对该文件的内容执行某些操作,然后向用户显示一些结果。如果文件不存在,将引发异常。如果您只是捕获异常,则不执行任何操作,然后继续执行“对文件内容执行某些操作”代码。。。祝贺您,现在您有更多的异常需要处理,因为文件中没有内容。你再次耸耸肩,继续执行“显示结果”代码。。。祝贺你,还有更多的例外,因为没有结果

没有懒惰的出路。捕获特定的异常,并适当地处理它们。是的,这需要更多的努力。是的,它需要更多的代码。是的,你将不得不考虑“适当处理”在每个个案中的含义。这就是编程。

你应该试试这个

static void Main(string[] args)
{
    bool shouldStop=false;
    while(!shouldStop){
        IrcClient bot = new IrcClient();
        shouldStop=true;

        // attach events
        try
        {
            // connect to server, login etc

            // here we tell the IRC API to go into a receive mode, all events
            // will be triggered by _this_ thread (main thread in this case)
            // Listen() blocks by default, you can also use ListenOnce() if you
            // need that does one IRC operation and then returns, so you need then
            // an own loop
            bot.Listen();

            // disconnect when Listen() returns our IRC session is over
            bot.Disconnect();
        }
        catch (ConnectionException e)
        {
            Console.WriteLine("Couldn't connect! Reason: " + e.Message);
            shouldStop=false;
        }
        catch (Exception e)
        {
            Console.WriteLine(">> Error: " + e);
            shouldStop=false;
        }
    }
}

摆脱控制台。ReadLine()这是可行的,但它似乎会让机器人再次加入频道,这是我并不真正想要的。@user969894-这就是为什么“捕获所有内容并继续”是错误的另一个原因-一些错误/异常可能会导致机器人不再在频道中。其他人可能会保持连接。这就是为什么您应该处理特定的异常,如果您有合理的机会了解程序之后处于何种状态。这种态度的问题在于您认为程序继续运行是安全的。考虑到您试图捕捉任何异常,是什么让您认为这是一个安全的假设?当发生意外情况时,让程序尽早崩溃几乎总是比将自己的状态破坏成无法识别的形式要好。捕获您有实际策略处理的特定异常,并让所有其他异常终止您的程序。