2013-06-21 39 views
1

我已经使用SmartIrc4Net在C#中编写了一个IRC bot,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); 
    } 
} 
+3

问题。鉴于你试图捕捉*任何*异常,是什么让你认为这是一个安全的假设?当意外事件发生时,尽可能早地让程序崩溃几乎总是比较好 - 而不是可能将自己的状态变为无法识别的形式。捕获*具体*例外,你有一个实际的*策略*处理,并让所有其他人终止你的程序。 –

回答

2

将程序包装在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); 
     } 
    } 
} 
+0

并摆脱'Console.ReadLine()' – Jamiec

+0

是的,编辑。 –

+0

这是有效的,但它似乎让机器人再次加入了频道,这并不是我想要的。 – Michael

0

异常可以导致应用程序 接近,但它可以保持运行应用程序,而不必 代码发生任何“按任意键继续”的消息出现。

嗯......是的,你可以用这种方式编写你的应用程序,但我几乎可以保证它不是你想象的简单方法。当抛出异常时,出现问题。你不会通过耸耸肩膀继续进行神奇的修复,因为这可能导致更多的事情出错。

想象一下你有代码打开一个文件,然后对该文件的内容进行一些操作,然后向用户显示一些结果。如果文件不存在,则会抛出异常。如果你只是赶上例外,什么也不做,然后继续进行“用文件内容做些事情”的代码...恭喜,现在你有更多的例外处理,因为没有文件的内容 。你再次耸耸肩,继续“显示结果”代码...并祝贺,但更多的例外,因为没有结果!

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

0

用这样的态度是,你*假设*它是安全的为您的程序继续运行,你应该试试这个

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; 
     } 
    } 
} 
相关问题