Windows Form Main Entry Point Question

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello Every one
I am a begginner in C# programming,recentry i have met one question,How do I try and catch Exception in the Main Method.It seems that dot net do not wish to try and catch in the main method,but in each class method. Does anyone have meet this question before
Thanks in advance!
 
Hi,

You encapsulate the Main method to be able to end the program
"gracefully". Any exceptions that happen inside the program will
eventually end up in Main (unless they are handled). If there is no catch
block in Main to handle the exception either you get an "Unhandled
exception occured in ...".
If you catch it, you can display the error messages in a friendlier way,
tell the user to contact some e-mail address, or even add an error
reporting procedure.

You do not want to try catch everywhere in your code as that will slow
down your program. Only add it when there is a chance that something can
go wrong, to let your program deal with it and continue as "normal".

Happy coding!
Morten Wennevik [C# MVP]
 
Hello,

If I got your question right...

In Windows Forms apps, it is more advisable to install the global exception
handler as the handler for the Application.ThreadException event, as well as
for the AppDomain.UnhandledException event.

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://www.x-unity.net/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

lzh_gladiator said:
Hello Every one:
I am a begginner in C# programming,recentry i have met one
question,How do I try and catch Exception in the Main Method.It seems that
dot net do not wish to try and catch in the main method,but in each class
method. Does anyone have meet this question before?
 
Hello Mr Dmitriy Lapshin
Thanks for your answers! You shed a light on my way,but i want to know more about global exception handler , can you tell me how do i install global exception handler
Thanks in advanc

lzh_gladiato
 
Hello
Thanks for your answers! You shed a light on my way,but i want to know more about global exception handler , can you tell me how to implement a global exception handler in C
,related resource will more helpful!
sincerely your
Thanks in advanc
 
Please refer to the

"Application.ThreadException event"
"AppDomain.UnhandledException event"

topics in the online or offline MSDN library.

These topics should contain the examples.

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://www.x-unity.net/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

"Implement global Exception handler in c#"
Hello:
Thanks for your answers! You shed a light on my way,but i want to
know more about global exception handler , can you tell me how to implement
a global exception handler in C#
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top