I have written a BAT file to run my program ten times. But at some point, the program will crash. I hope I can detect this condition then close the program. The c# process of Responding that failed to know this problem.
Asked
Active
Viewed 618 times
0
-
possible duplicate of "What is exception handling and how does it affect me?" – CodeCaster Apr 15 '15 at 10:15
-
Wouldn't it be better to find out why it's not working and fix that instead? – DavidG Apr 15 '15 at 10:18
-
DavidG is right. However it looks to me that you want to suppress the Windows Error Reporting dialogs so that they don't block the batch file from progessing. As such, this is a duplicate. http://stackoverflow.com/questions/12102982/disabling-windows-error-reportingappcrash-dialog-programmatically – spender Apr 15 '15 at 10:18
-
More likely you are controlling an exception your shouldn't. – Tony Hopkinson Apr 15 '15 at 10:19
-
Did you write the app that is being launched? If so, the real solution is to trap errors and not allow them to take down the app. `try/catch` is your friend here. – spender Apr 15 '15 at 10:22
2 Answers
2
See How do I specify the exit code of a console application in .NET?, MSDN: Main() Return Values (C# Programming Guide) and .NET Global exception handler in console application.
The dialog is shown because you don't catch an exception. You need to combine everything shown there, so:
class Program {
static void Main(string[] args) {
AppDomain.CurrentDomain.UnhandledException += UnhandledExceptionTrapper;
// Your code here
throw new Exception("Kaboom");
}
static void UnhandledExceptionTrapper(object sender, UnhandledExceptionEventArgs e) {
Console.WriteLine(e.ExceptionObject.ToString());
Environment.Exit(1);
}
}
And:
@echo off
TestExceptionApp
@if "%ERRORLEVEL%" == "0" goto good
:fail
echo Execution Failed
echo return value = %ERRORLEVEL%
goto end
:good
echo Execution succeeded
echo Return value = %ERRORLEVEL%
goto end
:end

Community
- 1
- 1

CodeCaster
- 147,647
- 23
- 218
- 272
-
Does the `UnhandledException` handler actually prevent the exception from bubbling out and taking down the app? If not, OP is in the same position as before with WER dialogs blocking the batch file. – spender Apr 15 '15 at 10:24
-
1
-
As long as the application doesn't leak exceptions (which an `UnhandledException` handler prevents), you won't see a WER popup _for your code_. If OP's code starts another executable that causes the popup, well ... then they should mention that in the OP. – CodeCaster Apr 15 '15 at 10:29
-
1I'm not sure it does prevent exceptions from killing the app... it's just a last point of notification. The docs say "It allows the application to log information about the exception before the system default handler reports the exception to the user and terminates the application". The reason your code will work is that you're getting an `Environment.Exit` in before this happens. – spender Apr 15 '15 at 10:31
-
1If the other executable causes another exception, then it's called Inception. :) – jmc Apr 15 '15 at 10:33
0
You'll have to write a separate program that creates a new process that catches the error. When the catch is made you can send notification to yourself in the catch statement before the error handling notification.
It will likely need to run as a separate program called from the local user's system.

Frank
- 47
- 10
-
-
The OP will be back with more questions? IDK. I believe the OP's question is not really one but two questions. Hard to tell what they're really looking for. The original question, to detect, and then the second question at the bottom, to close. I think maybe the question should be re written and then asked. – Frank Apr 15 '15 at 10:24
-
@DavidG I reworded my comment. In a separate process a new program should be called. Hopefully one without errors :-D – Frank Apr 15 '15 at 10:28