none
Running a .net console app RRS feed

  • Question

  • How does a .net console app return an error code to operating system? I am running my app as a scheduled task. Shall I throw an exception to show my failure?
    Wednesday, October 3, 2012 5:28 PM

Answers

  • Hi,

    you can define the main method returning an int.

    public static int Main(string[] args) 
    {
        int errorNumber = 0;
        if(somecondition)
           errorNumber = 16328;
        return errorNumber;
    }

    Regards,

      Thorsten


    Wednesday, October 3, 2012 5:31 PM
  • You can use Environment.Exit() method as well:

    http://msdn.microsoft.com/en-us/library/system.environment.exit(v=vs.100).aspx

    Regards,

    Miguel.

    Wednesday, October 3, 2012 5:33 PM
  • In addition to what Thorsten said - when you return a value from Main(), typically, any non-zero value is considered an "error" case.  For details, see Main() Return Values:

    "In this example, a batch file is used to run a program and test the return value of the Main function. When a program is executed in Windows, any value returned from the Main function is stored in an environment variable called ERRORLEVEL. A batch file can determine the outcome of execution by inspecting the ERRORLEVEL variable. Traditionally, a return value of zero indicates successful execution. The following example is a simple program that returns zero from the Main function. The zero indicates that the program ran successfully."


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Wednesday, October 3, 2012 5:33 PM
    Moderator

All replies

  • Hi,

    you can define the main method returning an int.

    public static int Main(string[] args) 
    {
        int errorNumber = 0;
        if(somecondition)
           errorNumber = 16328;
        return errorNumber;
    }

    Regards,

      Thorsten


    Wednesday, October 3, 2012 5:31 PM
  • You can use Environment.Exit() method as well:

    http://msdn.microsoft.com/en-us/library/system.environment.exit(v=vs.100).aspx

    Regards,

    Miguel.

    Wednesday, October 3, 2012 5:33 PM
  • In addition to what Thorsten said - when you return a value from Main(), typically, any non-zero value is considered an "error" case.  For details, see Main() Return Values:

    "In this example, a batch file is used to run a program and test the return value of the Main function. When a program is executed in Windows, any value returned from the Main function is stored in an environment variable called ERRORLEVEL. A batch file can determine the outcome of execution by inspecting the ERRORLEVEL variable. Traditionally, a return value of zero indicates successful execution. The following example is a simple program that returns zero from the Main function. The zero indicates that the program ran successfully."


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Wednesday, October 3, 2012 5:33 PM
    Moderator