I was looking at some other question and then I had this question:
- Who calls the Main() method?
-
If I want to exit/quit the application (when I get some exception in the Main() method itself), is it a good idea to use
return;statement from thecatchblock in the Main() method?- Please note that I am not starting any thread in the Main() method explicitly. When we start the application, are there any threads start automatically in the background?
Application.Exit()does not guarantee an application to quit- (EDITED point)
Environment.Exit()is another option.
Can use of return; statement to quit the application be a good idea? If no, what are those (subtle) things because of which it may not be good idea?
In comparison, which is the best approach to quit?
If your application is going to terminate due to an unexpected error, you may wish to use Environment.FailFast which will “crash” the application with a specified message that gets written to the event log and offers the user an opportunity to submit the crash data to Microsoft. As the developer, Microsoft can make the crash data available to you.
But if you just want to return an error condition to the calling process (such as in the case of a console application) you can modify your Main method signature to return an int and then return a non-zero value which by convention implies an error condition.