1

SmartIrc4Net を使用して C# で IRC ボットを作成しました。ボットの目的は、コマンドが認識されたときに情報を提供することだけです。

私の問題は、アプリケーションを閉じる原因となるコードで例外が発生する可能性がありますが、アプリケーションを実行し続け、「続行するには何かキーを押してください」というメッセージが表示されないことです。これは理想的には、例外をログに記録して続行する必要があります。

そもそも例外を管理できることはわかっていますが、コマンドごとにすべての入力を検証するには時間がかかります。または、私がカバーしていない他の例外があるかもしれません.

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);
    }
}
4

3 に答える 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);
        }
    }
}
于 2013-06-21T09:58:33.027 に答える
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;
        }
    }
}
于 2013-06-21T11:28:55.503 に答える
0

アプリケーションを閉じる原因となるコードで例外が発生する可能性がありますが、アプリケーションを実行し続け、「続行するには何かキーを押してください」というメッセージが表示されないようにすることは可能ですか?

ええと... はい、そのようにアプリケーションを書くこともできますが、あなたが思っているような簡単な方法ではないことはほぼ保証できます。例外がスローされると、何か問題が発生しています。肩をすくめて続行することで魔法のように何かを修正することはできません。

ファイルを開き、そのファイルの内容に対して何かを実行し、結果をユーザーに表示するコードがあると想像してみてください。ファイルが存在しない場合は、例外がスローされます。例外をキャッチしただけの場合は、何もせずに「ファイルの内容で何かを行う」コードを続行します...おめでとう、ファイルの内容がないため、処理する例外がさらに増えました。もう一度肩をすくめて、「結果を表示する」コードを続けます...おめでとうございます。結果がないため、さらに多くの例外があります!

怠惰な方法はありません。特定の例外をキャッチし、適切に処理します。はい、これにはもっと手間がかかります。はい、より多くのコードが必要です。はい、個々のケースで「適切に処理する」とはどういう意味かを考える必要があります。それがプログラミングです。

于 2013-06-21T11:09:31.007 に答える