-
Notifications
You must be signed in to change notification settings - Fork 257
Ability to catch AppDomain.UnhandledException in a not-hosted script (cscs) ? #257
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
I think it is possible. |
Unfortunately var fields = AppDomain.CurrentDomain
.GetType()
.GetFields(BindingFlags.NonPublic |
BindingFlags.Static |
BindingFlags.Instance |
BindingFlags.FlattenHierarchy |
BindingFlags.IgnoreCase); Thus despite all my effort, I do not see how this enhancement can be implemented. :( |
Unfortunately, it looks like you are right. I haven't checked this before, but seems there's no way to get into the UnhandledException delegates in a domain except += and -=. But, if you still have a bit of spare time, I probably have an idea for a simple workaround for this limitation. It's not very elegant, but it works. dbg.cs public class dbg
{
public static UnhandledExceptionEventHandler UnhandledException = null; // <-- instead CurrentDomain for scripts
...
} Utils.cs internal static string DbgInjectionCodeInterface = @"// Auto-generated file
...
partial class dbg
{
public static UnhandledExceptionEventHandler UnhandledException = null; // <-- expose to script
...
}"; csscript.cs try {
... executor.Execute ...
catch (Exception e)
{
dbg.UnhandledException?.Invoke(null, new UnhandledExceptionEventArgs(e, true)); // <-- callback to script if exists
if (!CSSUtils.IsRuntimeErrorReportingSuppressed)
print("Error: Specified file could not be executed." + NewLine);
throw;
} The sample script I posted at the beginning only needs a slight modification and works as expected. #if CS_SCRIPT
dbg.UnhandledException = new UnhandledExceptionEventHandler(ExceptionEventHandler);
#else
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(ExceptionEventHandler);
#endif I'm not sure putting this delegate in the dbg class is a good idea, but it was on hand. Besides, exception handling seems logically related to debugging, so maybe this is the right place. If this would be acceptable, then in my opinion it may solve the problem of the script ability to catching its own unhandled exceptions. |
Yeah, I thought about this too. I mean using a global delegate. But your use of partial using static dbg;
. . .
static void Main()
{
UnhandledException += (sender, args) =>
{
Console.WriteLine("UnhandledException intercepted");
Console.WriteLine("Message: "+((Exception)args.ExceptionObject).Message);
Environment.Exit(0);
};
. . . And you can even use your conditional compilation: #if CS_SCRIPT
using static dbg;
#else
using static AppDomain.CurrentDomain;
#endif So... I am not giving up just yet :) |
- Updated `-speed` and `-code` with the complete support `-ng:*` switches - Added `IEvaluator.IsCachingEnabled`. Ite as always available from the conctrete types implementing `IEvaluator` and now it is moved directly to the interface. - Added `-servers:start` and `-servers:stop` command to control both Roslyn and csc build servers at the same time - CSScriptLib: Native API `CacheEnabled` marked as obsolete - Issue #258: Can not run scripts after installing VS2022 - Issue #257: Ability to catch AppDomain.UnhandledException in a not-hosted script (cscs) - Issue #255: Relative path for cscs.exe -out option results in wrong output folder - Issue #254: Script merger for hosted scripts - Issue #253: Supports both .Net Framework and .Net 5 - Issue #252: System.NullReferenceException: Object reference not set to an instance of an object. (updated API doc) - Added auto-generation of the CLI MD documentation with `-help cli:md`. To be used to generate GitHub wiki page during the build - Fixed Debian packaging problem (`/n/r` needed replacement with `\n`)
### Misc - Added auto-generation of the CLI MD documentation with -help cli:md. To be used to generate GitHub wiki page during the build - Fixed Debian packaging problem (/n/r needed replacement with \n) - Issue #253: Supports both .Net Framework and .Net 5 ### CLI - Updated -speed and -code with the complete support -ng:* switches - Added -servers:start and -servers:stop command to control both Roslyn and csc build servers at the same time - Issue #258: Can not run scripts after installing VS2022 - Issue #257: Ability to catch AppDomain.UnhandledException in a not-hosted script (cscs) - Issue #255: Relative path for cscs.exe -out option results in wrong output folder - Issue #254: Script merger for hosted scripts - Issue #252: System.NullReferenceException: Object reference not set to an instance of an object. (updated API doc) ### CSScriptLib - Native API CacheEnabled marked as obsolete - Added IEvaluator.IsCachingEnabled. It is always available from the concrete types implementing IEvaluator and now it is moved directly to the interface.
I'd like to be able to catch unhandled exceptions in a script, just like in a regular console application (where try-catch was missing for some reason). For example (run by cscs.exe):
The result of running script is:
I would rather get something like this:
I think I understand why this is happening. When the script is run through cscs there are really no unhandled exceptions because they are all caught on the way:
So, I would like to ask, if it would be possible for cscs to check, after catching an unhandled exception:
If there is a delegate in CurrentDomain.UnhandledException, then pack the exception into UnhandledExceptionEventArgs and call that delegate ?
Or maybe there is some other way to achieve this?
The text was updated successfully, but these errors were encountered: