0

我想从 main 返回一个默认的 int 值。

考虑以下:

using System;
class Class1
{
    static int Main(string[] args)
    {
        int intReturnCode = 1;
        int intRandNumber;

        Random myRandom = new Random();
        intRandNumber = myRandom.Next(0,2);
        if(intRandNumber ==1)
        {
            throw new Exception("ErrorError");      
        }
        return intReturnCode;
    }
}

遇到异常时,我无法设置返回码。

是否可以在 main 中有默认返回码?

澄清:我有一个抛出未处理异常的程序。我在 try catch 中有应用程序,但是一些错误(可能内存不足、stackoverflow 等)仍在冒泡并导致我的应用程序在生产中失败。

为了解决这个问题,我添加了代码来捕获未处理的异常。

这被添加到主要:

AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(OnUnhandledException);

现在,当发生未处理的异常时,我有了这个方法。

public static void OnUnhandledException(object sender, UnhandledExceptionEventArgs e)   
{ 
    //here's how you get the exception  

    Exception exception = (Exception)e.ExceptionObject;                 

    //bail out in a tidy way and perform your logging
}

问题是我不再在 Main 中,我想以非零退出代码退出。

4

4 回答 4

5

未处理的异常是实现定义的行为。任何事情都有可能发生;CLR 可以决定设置它认为合适的进程的返回代码,它可以启动一个调试器,它可以做任何它想做的事情。您不能依赖任何包含未处理异常的程序的任何行为。

如果您想要具有可预测的行为,例如确定进程结束时的返回码是什么,那么总共必须有零个未处理的异常。

如果您有一个第三方组件抛出未处理的内存异常,那么您最好的选择是:修复该组件中的错误。如果你不能这样做,那么将组件隔离到它自己的进程或它自己的 appdomain中。

于 2013-04-24T16:24:35.647 回答
4

问题实际上是为什么您要抛出异常main而不是提供指示错误的返回代码?而不是你在做什么,我的代码如下所示:

static int Main(string[] args)
{
    int intRandNumber;

    try
    {
        Random myRandom = new Random();
        intRandNumber = myRandom.Next(0,2);
        if(intRandNumber ==1)
        {
            Console.WriteLine("Got invalid random number!");
            return 0;
        }
    }
    catch (Exception exp)
    {
        Console.WriteLine("Strange ... an error occurred! " + exp.ToString());
        return -1;
    }

    return 1;
}

根据经验,您永远不应该抛出异常来控制程序流。如果可以的话,处理像oops 这样的情况,我得到了错误的数字而不会引发异常。

于 2013-04-24T15:51:24.440 回答
3

在主线程中抛出异常会结束执行而没有到达return: 那是你得到“控制台应用程序已停止工作,你想调试吗?”的时候。来自操作系统的对话框。在这些Main条件下不能返回任何东西,因为没有任何东西可以返回。

如果您想在遇到异常时返回某些内容,请像这样编写程序代码:

// Put your implementation into a private method
private static int DoWork(string[] args) {
    ... // Do actual work, and throw exceptions if you need to
    return intReturnCode;
}
// The Main method calls the implementation, and returns the value
// returned from the implementation if everything goes well.
// If an exception is thrown, however, Main could return another value
// of its choice.
public static int Main(string[] args) {
    try {
        return DoWork(args);
    } catch {
        return myDefaultReturnCode;
    }
}
于 2013-04-24T15:52:12.343 回答
0

捕获您的异常并在 finally 块中设置 return 语句

using System;
class Class1
{
    static int Main(string[] args)
    {
        try
        {
            int intReturnCode = 1;
            int intRandNumber;

            Random myRandom = new Random();
            intRandNumber = myRandom.Next(0,2);
            if(intRandNumber ==1)
            {
                throw new Exception("ErrorError");      
            }
        }
        catch(Exception e)
        {
          // ...
        }
        finally
        {
            return intReturnCode;
        }
    }
}
于 2013-04-24T15:52:58.197 回答