Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Wednesday, April 30, 2014 6:05 AM
Hi,
i am writing a console timer job to send automated email. every has being successfully.. but for any code, there might be some exception being surface due to unforeseen circumstances. therefore i would like to check if there is a way to stop the console from closing when there is a exception?
All replies (8)
Wednesday, April 30, 2014 9:15 AM ✅Answered
Add a while loop.
static void Main(string[] args){ while(true) { try { // put all code in here ... try { File.Open("Not An Existing File", FileMode.Open); // test an exception } catch (Exception e) { Console.WriteLine(e.Message); } } finally { Console.Write("Press Enter to close window ..."); Console.Read(); } }}
jdweng
Wednesday, April 30, 2014 6:46 AM
I think that the only way you can achieve it is to put some try-catch blocks around your code, each one give back the precise info about the raised exception.
Than in main() you put a nother try-catch block to intercept the top-level raised exception and, i.e. write in the console output.
Note that with this solution the console won't close, but won't work anymore since a restart.
Davide 'n3wt0n' Benvegnù
If you found my answer helpful, please vote it.
If you think that my answer resolve your issue, plase "Mark it as answer".
Wednesday, April 30, 2014 6:48 AM
You can structure your console main method like this
static void Main(string[] args)
{
DoSomeWork();
}
static void DoSomeWork()
{
try {
// specify all the main method code here and call it from main
}
catch {
//log exceptions somewhere
Console.WriteLine("Some error occured, do you want to try again? Enter 'Y' or 'N' to exit");
if (((ConsoleKeyInfo)Console.ReadKey()).Key == ConsoleKey.Y)
{
DoSomeWork();
}
}
}
Hope this helps!
Ovais Mehboob Ahmed Khan http://ovaismehboob.wordpress.com
Wednesday, April 30, 2014 8:10 AM
i would like to check if there is a way to stop the console from closing when there is a exception?
This should work:
static void Main(string[] args)
{
try
{
// put all code in here ...
try
{
File.Open("Not An Existing File", FileMode.Open); // test an exception
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
finally
{
Console.Write("Press Enter to close window ...");
Console.Read();
}
}
- Wayne
Wednesday, April 30, 2014 8:41 AM
hi.. thanks for all the respond... sorry that i forget to mention that the console application is suppose to be fully automated once deploy.. is there anywat for it to loop back to preform its normal task?
Wednesday, April 30, 2014 9:18 AM
You can use this kind of approach:
Davide 'n3wt0n' Benvegnù
If you found my answer helpful, please vote it.
If you think that my answer resolve your issue, plase "Mark it as answer".
Wednesday, April 30, 2014 10:13 AM
Yes. If you wrap the inside part of the loop with the try and catch, the loop will not stop.
Noam B.
Do not Forget to Vote as Answer/Helpful, please. It encourages us to help you...
Wednesday, April 30, 2014 1:45 PM
i forget to mention that the console application is suppose to be fully automated once deploy.. is there anywat for it to loop back to preform its normal task?
Do you really think it's wise and prudent to keep the application running indefinitely
after an unknown or unhandled exception has been thrown?
Your original issue was how to keep the console window from closing after an exception.
Doesn't that imply that the application is terminating? If it isn't, won't the console
window remain open anyway?
- Wayne