I have a client server application. The clients log in an account on my server. The thing is that my server sometimes crashes and although I have all kind of wrapped in a try-catch statement, I get no message.
The server I have implemented is here: simple threaded tcp server.
Did anyone encounter a crash like this? Is there a place that I can find an error log for the app?
I'm kind of desperate, so ANY ideas would be great. Thanks.
The application is possible to crash because of unhandled exceptions even if you have all functions wrapped in try-catch block because of multi-threading operations.
try
{
var t = new Thread(()=>
{
Thread.Sleep(5000);
throw new Exception();
});
t.Start();
//t.Join();
}
catch
{
//you can't deal with exception here
//even though you uncomment `t.Join`
//the application will crash even there is a try-catch wrapped
}
Have you kept this part of the example code?
Add a Console.WriteLine right after the catch...
catch
{
//a socket error has occured
break;
}
If you're running under Debug mode in Visual Studio, you might consider enabling CLR Exceptions to propagate to user control. I believe they're disabled by default, which sometimes causes hung applications without any debugger feedback. This article on MSDN might be of interest.
Related
I have these lines in my App.xaml.cs, to globally catch the exceptions:
this.Dispatcher.UnhandledException += new
DispatcherUnhandledExceptionEventHandler(
Current_DispatcherUnhandledException);
AppDomain.CurrentDomain.UnhandledException += new
UnhandledExceptionEventHandler(
CurrentDomain_UnhandledException);
Application.Current.DispatcherUnhandledException += new
DispatcherUnhandledExceptionEventHandler(
Current_DispatcherUnhandledException);
Most exceptions can be caught and a log file will be generated so that I know what is causing the error. But on my customer's PC (only windows 8 has
this problem), the WPF application sometimes crashed but I was not able to get the exception log, means the exception handlers above was not called.
Is there any handler I can set to catch exception? Or is there any log collector/reader like android DDMS for Windows/WPF app?
Thank you!
Since .NET 4.0 you could try and HandleProcessCorruptedStateExceptionsAttribute This attribute allows you to handle exceptions that since .Net 4 terminate the process. This is rather dangerous as you are allowing a process to continue (handle the exception) even when its memory has become corrupted.
In general: do not trust the application after catching such an exception. Log the exception and stop the application. Only when you know what is causing the exception and you are sure the application can recover you could apply this attribute to a method and make it resume normal execution.
I am having MSMQ on windows 2008. Messages are available in private queue. I have one WCF subscriber (written in C#) which is installed as windows service. Now problem is that sometimes the WCF subscriber stops picking messages from Queue. If I restart service again it works fine. Now I attached IError Handler to log the reason and exception.
Now to Handle this issue what I wanted to do is, I will set the recovery property to restart service on first failure and now problem is how to throw the error from HandleError() method of IErrorHandler class?
Please tell me best way to throw an exception in a window service so it can be restarted.
While it is probably better to address the underlying cause of your exceptions, it is certainly valid in certain scenarios to implement a fail fast methodology. Indeed, this ability to kill processes which have become "flawed" in some manner is critical to the concept of fault tolerance.
So, to make a windows service commit suicide:
void KillSelf()
{
try
{
// Code to close open connections/dispose
// of unmanaged resources etc
...
}
finally
{
Environment.Exit(1);
}
}
Service recovery options should be set to restart automatically. This will ensure your service comes straight back up again.
As far as I know one cannot throw an exception to restart a windows service.
I usually encapsulate a try catch (with logging) to prevent any exceptions crashing the service, which is the opposite to what you are suggesting.
It may be that you can catch an error and stop the service (not sure) and configure the service to restart if it stops?
I have the following code in my application that is running after an unhandled exception:
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
var exception = e.ExceptionObject as Exception;
if (exception != null) MessageBox.Show(exception.Message + " - " + exception.StackTrace);
}
but even if i catch unhandled exception my windows mobile application close. How to prevent closing application when i catch unhandled exception. I never want to close my app. I want to open Login form in this scenario or anything else not close app.
So what i want is to prevent closing application from unhandled exception like network is down,...
I can not use try catch in every code ....
Any idea how to prevent closing app when network is down or any other unhandled exceptions?
You don't. When you get an AppDomain unhandled exception, your app is no longer in a stable state. Where exactly would you resume to? When you've gotten to that point, your only option, with good reason, is to exit. You could reasonably schedule yourself to run again to make sure the app comes back, but a far better idea is to actually handle the exception at its source and prevent it from being an UnhandledException.
In your app.config add the following element
<runtime>
<!-- the following setting prevents the host from closing when an unhandled exception is thrown -->
<legacyUnhandledExceptionPolicy enabled="1" />
</runtime>
You can, and you should use try...catch and handle the exceptions in every situation where an exception might occur. (In languages like Java you can't even compile your code until you catch every exception that the called function might throw or declare explicitly that this particular function won't catch it, so the one that calls it should do it.)
If you want to minimize the cases where you use try...catch some bad situations can be prevented with testing if the necessary conditions are met so you won't even call a function that is known for throwing a particular exception. Like calling
if (myByteArray != null)
{
myIPAddress = new IPAddress(myByteArray);
}
because this way the IPAddress constructor won't throw an ArgumentNullException.
However in most of the cases this can't be done, especially with networking, because you can't predict if the cable will be cut, the signal will be lost, the database server will crush, etc. in the middle of the transaction. So you should try...catch every time you connect to the network or send data to or receive data from a connection.
AppDomain.CurrentDomain.UnhandledException fires on any unhandled exception on
any thread, but since CLR 2.0, the CLR forces application shutdown after your event
handler completes. However, you can prevent shutdown by adding the following to
your application configuration file:
<configuration>
<runtime>
<legacyUnhandledExceptionPolicy enabled="1" />
</runtime>
</configuration>
I think you need to use Application.ThreadException
When you subscribe to Application.ThreadException, unhandled exceptions wont hit the UnhandledException and the app will keep running.
My asp.net application gives an exception once in a while as a result application pool stopped. and it needs to be started manually.
Does anyone know how to trace it, how to find what cause the problem?
I don't see any errors in Event Viewer.
In IIS 7.0, events (configurable) and exceptions (always) are logged to the Windows event log when an application pool recycles.
Sccot Guthrie's round-up of Tess's related posts is great:
Outstanding Blog for Learning how to Debug ASP.NET App Issues (Memory Leaks, Crashes, Deadlocks, etc)
ASP.NET 2.0 Crash case study: Unhandled exceptions
Try putting an Application_Error method on Global.asax.cs like this:
protected void Application_Error(object sender, EventArgs e)
{
// Get the exception object.
Exception exception = Server.GetLastError();
_Log.Error(exception.Message, exception);
// Clear the error from the server
Server.ClearError();
}
The _Log line assumes you're using log4net or some other logging framework but you could do whatever you want with the error.
PS - There's some misinformation in some of the other comments. But I don't have enough rep to comment on them. All IIS / WCF web service apps that I've written will terminate if any thread has an unhandled exception. I can't speak to whether that's true of ASP.Net apps as well.
Are you absolutely certain your application pool is stopping due to an error? There are many reasons the application pool can stop. Unless configured otherwise by default it will stop if inactive after a set ammount of time. I think about 20 minutes.
I'm calling a web service from within a Silverlight Windows phone 7 application which works fine if the web service is available. If the web service is unavailable then an unhandled exception is caught but I don't seem able to catch it myself. My attempt is as follows:
//Get list of providers from Webservice
RSAServiceClient proxy = new RSAServiceClient();
proxy.GetSuppliersCompleted += new System.EventHandler<GetSuppliersCompletedEventArgs>(ProxyGetSuppliersCompleted);
try
{
proxy.GetSuppliersAsync();
}
catch (EndpointNotFoundException)
{
//TODO: Handle webserice not being available
}
catch (Exception)
{
throw;
}
But this doesn't catch the exception and obviously GetSuppliersCompleted never get's called so I can't catch it there.
I then thought I may be able to detect it by checking the connection state (proxy.State) but this despite the web service not running returns CommunicationState.Opened.
Any idea's where I can handle this?
Apologies if I've missed something but I have searched and not found a solution.
You should place your catch in your ProxyGetSuppliersCompleted method.
An attempt to access the result will throw the error you are expecting.
Alternatively in the ProxyGetSuppliersCompleted you can test the Error property of the EventArgs which will contain the exception.
I recommend to check if you you have access to Internet before you fire any peace of code. Just check if there is net connection and notifi the user if not.
I have exactly the same problem.
The behaviour is different from Desktop-Silverlight where the exception is handled, to WindowsPhone-Silverlight where connection exceptions are not handled at all.
I created a topic here : http://forums.create.msdn.com/forums/t/69485.aspx
Nobody has any real answer.
Ah, yes, there is a trick : you can manually modify the reference.cs in order to manually catch the exception.... But if you have lot of functions, it becomes a nightmare.
Maybe Microsoft developers didn't test all the connection usecases, and they forget to implement this one (which is a big one).