Quit the program when an error is detected in C #?

With Python, I usually check the return value. And, if there is an error, I use sys.exit()along with the error message.

What is the equivalent action in C #?

  • Q1: How to print error message for stream stderr?
  • Q2: How to call the system.exit () function in C #?
  • Q3: How do C # programmers handle errors? Finding and eliminating exceptions? Or just get the return value and exit ()?
+3
source share
2 answers

Q1: In C #, you need to use System.Console.xxx to access streams for input, output and error: System.Console.Error is a standard error that you can write.

http://msdn.microsoft.com/en-us/library/system.console.aspx

Q2: exit with:

System.Environment.Exit( exitCode );

http://msdn.microsoft.com/en-us/library/system.environment.exit.aspx

Q3: , # () ( , Exception), .

, main() try... catch:

class App {
    public static void Main(String[] args)
    {
        try {
            <your code here>
        } catch(Exception exc) {
            <exception handling here>
        }
        finally {
            <clean up, when needed, here>
        }
    }
}

, .

+5

, ..NET , .

+3

Source: https://habr.com/ru/post/1791549/


All Articles