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