How to intercept and handle all exception of Windows phone Application? - windows-phone-7

We did't have any Test team support for our Product Development.
so.
we need intercept and handle all Exception for improve User experience.
is There have any Soluction in windows phone Application?
as Fllow in app.xaml.cs file. we found :
// Code to execute on Unhandled Exceptions
private void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)
{
if (e.ExceptionObject is QuitException)
return;
if (System.Diagnostics.Debugger.IsAttached)
{
// An unhandled exception has occurred; break into the debugger
System.Diagnostics.Debugger.Break();
}
if (System.Diagnostics.Debugger.IsAttached)
{
// An unhandled exception has occurred; break into the debugger
System.Diagnostics.Debugger.Break();
}
}

Yes, this should catch all the exceptions that you missed in your app. Considering that you obviously are not catching many exception somewhere else and are looking for a simple solution, this event handler might work, but I seriously don't recommend it.
This event handler catches the exceptions and then should crash/quit your app. If you handled your exceptions only here, this would lead to a huge crash count. Sometimes exceptions happen, but the app can continue working normally. That's why I recommend that you handle them as they happen in your code, and not here. That way you have a full control of how your app continues and if it continues at all, and reduce the number of "unhandled exceptions" and app crashes.

Put your code in Try-Catch Block. I was also facing such problem, but then handled by Exception Handling Method.
try
{
// your code
}
catch (Exception ex)
{
throw (ex);
}

Related

How to make Web API Stop working on occurance of specific exception

In some scenarios I am getting exceptions in Startup.cs file.
So I want to catch that exceptions and do not show specific errors to Users.
I tried this:
I catch exceptions and throw as Custom exceptions, even though it shows stack-trace and all things into browser.
And if I left catch block empty then Web API still remains available (working).
I want to pause or stop my service for this scenarios.
Please help me.
I've No rights to comment so I'm going to write my answer here
as far as I understood your question why don't you use Application Error in global.asax file
Just like this
private void Application_Error(object sender, EventArgs e)
{
Exception ex = Server.GetLastError();
if (ex is HttpAntiForgeryException)
{
Response.Clear();
Server.ClearError();
Response.Redirect("/error/errorPage", true);
}
}
Hope I understood your question

How to catch System.InvalidOperationException: Connection was disconnected before invocation result was received

I´m getting this error exactly how the exception message says.
If a mobile client loses connection before the proxy.Invoke() result arrive, the exception is raised. That´s ok, but I need to catch this to avoid an app crash.
I try/catch all the proxy.Invoke() and proxy.Invoke<T>() calls, with no effect though.
How can I catch that exception?
Note: I´m using SignalR client 2.2.0 in a Xamarin client (PCL)
If you are calling your proxy.Invoke() without await proxy.Invoke() then the exception won't bubble up from the Invoke task to your executing code.
I've had to deal with this error before (with Xamarin in a PCL), and simply executing my Invoke like such worked for me:
try
{
await hubProxy.Invoke("SomeMethod", args);
}
catch (InvalidOperationException ex)
{
// Do what you need to with the exception
}
There is also a conversation about it here on the SignalR GitHub.

Jenkins reports a test build as a success, even though there are failures caused by exceptions?

I have SeleniumWebdriver/TestNG/Maven/Java continuous integration tests that are being run every time after a deploy. Sometimes an element is missing from the user interface and the tests throw an exception (which is later caught in the code, because in the catch statement I turn off the browser), so the build is marked as a success.
The strange thing is, I had failures in tests caused by exceptions before as well, and the build was still considered a successfull one.
How can I configure my maven pom.xml file or the jenkins build in order for it to mark every test that has thrown an exception, a FAILURE?
EDIT: After getting robjohncox's responce, I now have another thing I need to do:
How exactly do I throw the error again?
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
quit(driver);
sendMail();
}
Is it done this way?
throw e;
I think the problem relates to the fact that you are catching the exception in the code. Because you are handling the exception, it doesn't propagate up to your test runner, and therefore the test runner isn't aware that an exception was thrown.
After catching the exception and turning off your browser, you should re-throw the exception and then the test failures should be reported by your testing framework. The code would look something like this:
public void myTestCase() {
try {
// Do the testing
}
catch(Exception ex) {
// Turn off the browser
throw ex
}
}

C# windows service crashes without hitting catch block

I have a c# windows service that crashes without logging almost everyday after running for a few hours. Recently I added catch blocks to literally every method and still it doesn't help. Since I am using asynchronous callbacks on MSMQ, I guess there could be some multi-threading issues, but I have no clear clue. Any insight into this problem will be very helpful. Here is the pseudo code:
public MyService : ServiceBase
{
onStart()
{
try
{
someQueue.BeginReceive()
}
catch(Exception e)
{
log error and throw
}
}
void someQueue_ReceiveCompleted(object sender, ReceiveCompletedEventArgs e)
{
try
{
//process the message
}
catch(Exception e)
{
//log
}
finally
{
someQueue.Refresh()
someQueue.BeginReceive();
}
}
}
You can check Event Viewer to know the reason of Service getting stopped.
Open start menu and start Event Viewer, in application section you'll find the error
I know years passed since this request. But I got the same issue and no solution worked for me, other than the following.
In my case, the Event Viewer was not giving any details about the crash, and all user rights were ok.
the issue was a line of code in the OnStart method:
Debugger.Launch();
It was trying to launch the debugger, but that was not installed on that server. As such, it never completed and no exception was caught by my code.
Got rid of that, and it started working.

Debugging an exception in an empty catch block

I'm debugging a production application that has a rash of empty catch blocks sigh:
try {*SOME CODE*}
catch{}
Is there a way of seeing what the exception is when the debugger hits the catch in the IDE?
In VS, if you look in the Locals area of your IDE while inside the catch block, you will have something to the effect of $EXCEPTION which will have all of the information for the exception that was just caught.
In Visual Studio - Debug -> Exceptions -> Check the box by "Common Language Runtime Exceptions" in the Thrown Column
You can write
catch (Exception ex) { }
Then when an exception is thrown and caught here, you can inspect ex.
No it is impossible, because that code block says "I don't care about the exception". You could do a global find and replace with the following code to see the exception.
catch {}
with the following
catch (Exception exc) {
#IF DEBUG
object o = exc;
#ENDIF
}
What this will do is keep your current do nothing catch for Production code, but when running in DEBUG it will allow you to set break points on object o.
If you're using Visual Studio, there's the option to break whenever an exception is thrown, regardless of whether it's unhandled or not. When the exception is thrown, the exception helper (maybe only VS 2005 and later) will tell you what kind of exception it is.
Hit Ctrl+Alt+E to bring up the exception options dialog and turn this on.
Can't you just add an Exception at that point and inspect it?
#sectrean
That doesn't work because the compiler ignores the Exception ex value if there is nothing using it.

Resources