2013-06-21 36 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; 
     } 
    } 
} 
相關問題