I created a program that sends data between clients and when ever a runtime exception occurs, the whole program just exits.
I want to create a global catcher for all runtime exceptions so my program doesn't exit anytime a runtime exception occurs. When an exception occurs, the program exits and this message is printed:
Process finished with exit code -532,462,766.
This is a shortened view of my error stack:
Unhandled exception. System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.
---> System.IO.InvalidDataException: Found invalid data while decoding.
at Game.Packet.PacketReader.ReadString(Int32 length) in PATH:line 53
--- End of inner exception stack trace ---
at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor, Boolean wrapExceptions)
--- End of stack trace from previous location where exception was thrown ---
...
at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback()
I read some articles about how errors will bubble through so the first thing I tried was wrapping my main statement in program.cs but that didn't work neither. The next thing I tried was a solution I saw on another thread that said to use the delegates in AppDomain.CurrentDomain so I did and it didn't work either, the program exits. These were the delegates:
AppDomain.CurrentDomain.FirstChanceException += (sender, eventArgs) => {
Debug.WriteLine(eventArgs.ToString());
};
AppDomain.CurrentDomain.UnhandledException += (sender, exception) => {
Debug.WriteLine(exception.ToString());
};
I came from Java and the program didn't exit after a runtime error; I am unsure why this is the case with c# and would appreciate tips and or suggestions. Thank you!
Edit: This is a Console Application