简体   繁体   中英

Prevent console app from closing after catching exception

I've written an IRC bot in C# using SmartIrc4Net, the purpose of the bot is to just provide information when a command is recognised.

My problem is that, exceptions can happen in the code which causes the application to close but is it possible to keep the application running and not have any "press any key to continue" messages to appear. This should ideally just log the exception and continue on.

I know I could manage the exception in the first place but validating all the input on a per command basis will take a long time. Or there might even be other exceptions I might not have covered.

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

Wrap your program in a while(true) block.

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

exceptions can happen in the code which causes the application to close but is it possible to keep the application running and not have any "press any key to continue" messages to appear.

Well... yes, you could write your application that way, but I can pretty much guarantee it's not the easy way out you think it is. When an exception is thrown, something has gone wrong . You don't magically fix anything by shrugging your shoulders and carrying on regardless, all that's likely to result from that is that more things will go wrong.

Imagine for a moment you have code that opens a file, and then does something with the contents of that file, and then displays some results to the user. If the file doesn't exist, an exception will be thrown. If you just catch the exception, do nothing, and then carry on with the "do something with the contents of the file" code... congratulations, now you have more exceptions to deal with because there are no contents of the file . You shrug your shoulders again, carry on with the "display the results" code... and congratulations, yet more exceptions because there are no results!

There is no lazy way out. Catch specific exceptions, and handle them appropriately. Yes, this takes more effort. Yes, it takes more code. Yes, you are going to have to think about what "handle it appropriately" means in each individual case. That's programming.

you should try this

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM