With Python, I normally check the return value. And, if there's an error, I use sys.exit()
together with error message.
What's equivalent action in C#?
The exit () function is used to break out of a loop. This function causes an immediate termination of the entire program done by the operation system. The general form of the exit() function is as follows − void exit (int code);
So the C family has three ways to end the program: exit(), return, and final closing brace.
exit is a jump statement in C/C++ language which takes an integer (zero or non zero) to represent different exit status. Exit Success: Exit Success is indicated by exit(0) statement which means successful termination of the program, i.e. program has been executed without any error or interrupt.
The function has been defined under the stdlib. h header file, which must be included while using the exit() function.
Q1: In C#, you have to use System.Console.xxx in order to access the streams for input, output, and error: System.Console.Error is the standard error you can write to.
http://msdn.microsoft.com/en-us/library/system.console.aspx
Q2: You exit with:
System.Environment.Exit( exitCode );
http://msdn.microsoft.com/en-us/library/system.environment.exit.aspx
Q3: Yes, C# programmers raise (throw) exceptions (objects of classes deriving from the Exception class), and catch them in upper-level callers.
If you want to catch errors in the entire program, you just encapsulate the entire main() procedure in a 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>
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With