Wednesday 15 September 2010

c# - Prevent console app from closing after catching exception -


I have written an IRC bot in C # through SmartIrc4Net, the purpose of the bot is to provide information only when there is a command is recognized.

My problem is that, there can be an exception in the code, due to which the application stops, but it is possible to run and to continue the message "any key to continue Do not press ". It should ideally log on to the exception only and continue.

I know that I can manage the exception in the first place, but it will take a lot of time to validate all the inputs on the per-command basis. Or even other exceptions can not be covered with whom I can not cover.

  Fixed zero main (string [] args) {IRCClient bot = new IRCLANT (); // Attach events {// Join the server, login etc. // Here we tell the IRC API to go to the received mode, all events // Listen to this thread by _the_ th_dres (main thread in this case) // () Block by default, you can also use the Sunnoon () if you need an IRC operation and then return, then you need it // a self loop bot. Liston (); // Disconnect when listen () is on the return bot of our IRC session. Disconnect (); } Catch (Connection E) {console. WriteLine ("Could not connect! Reason:" + e.Message); Console.ReadLine (); } Hold (Exception e) {Console.WriteLine (">>> Error:" + E); }}    

Wrap your program in a (while) Block. Fixed Zero Main (string [] args) {while (true) {IRCClient bot = new IRCLANT (); // Attach events {// Join the server, login etc. // Here we tell the IRC API to go to the received mode, all events // Listen to this thread by _the_ th_dres (main thread in this case) // () Block by default, you can also use the Sunnoon () if you need an IRC operation and then return, then you need it // a self loop bot. Liston (); // Disconnect when listen () is on the return bot of our IRC session. Disconnect (); } Catch (Connection E) {console. WriteLine ("Could not connect! Reason:" + e.Message); } Hold (Exception e) {Console.WriteLine (">>> Error:" + E); }}}

No comments:

Post a Comment