Debugging an exception in an empty catch block - debugging

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.

Related

How to catch OleObject exception in Inno Setup?

So I try to make a post request having no internet connection using next modified code:
WinHttpReq := CreateOleObject('WinHttp.WinHttpRequest.5.1');
WinHttpReq.Open('POST', '<your_web_server>', false);
WinHttpReq.SetRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
try
WinHttpReq.Send('data');
except
bla:= 'e';
finally
bla := 'f';
end;
Yet exception does not get catched and I get crush of my setup application with next image:
How to handle OleObject exception in Inno Setup?
Your code is incomplete, but try..except block catches all the exceptions, including those thrown by OLE objects. However, your screenshot shows the line number, where the exception was thrown, and so you were running debugger.
And debugger shows all exception messages regardless they are in a try..except block, unless you uncheck "Pause on exceptions" option in Inno Setup IDE settings:
By default is this option enabled (which I would recommend to keep), which means that all exceptions are reported as exception messages and that's what might have mislead you. If you were running your setup without debugger attached, you wouldn't see that exception message.

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
}
}

visual studio doesnt catch ThreadAbortException

I've set up ThreadAbortException in the VS debug exceptions dialog, but it never breaks, even if I Thread.Abort() explicitly in my code.
I can see entries in the console such as:
A first chance exception of type 'System.Threading.ThreadAbortException' occurred in System.dll
An exception of type 'System.Threading.ThreadAbortException' occurred in mscorlib.dll but was not handled in user code
Is there any way to get VS to break on these exceptions? (I'm using VS Express 2012 for desktop, but have access to the full VS if needed)
Redirect() calls End() which throws a ThreadAbortException exception upon completion. This is one source of such exceptions.
To break on exceptions select
Debug->Exceptions and for the Common Language Runtime Exceptions place a check in the Thrown column.
You can verify your settings are correct by running this sample code in a console application. The debugger should halt at the point where the thread is aborted.
using System.Threading;
using System.Threading.Tasks;
namespace AbortThread
{
class Program
{
static void Main( string[] args )
{
Task.Run( () => Thread.CurrentThread.Abort() );
}
}
}

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

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);
}

Access denied error ( Visual Studio and WatiN )

I'm using the WatiN testing tool with Visual Studio 2005. When I try to select a value from my list box I am getting an "access denied" error.
I have seen this a lot with select lists recently when using the WatiN 2.0 beta. Instead of using the aSelectList.Select(strText) option, it seems to work better when you do this:
ie.SelectList(Find.ById("MySelect")).Option(Find.ByText("Option 1")).Select();
This can also happen when changing an ASP.NET control that cause an auto-postback. The first change will register, but the next element you try to access will throw an "Access Denied" error because it is still trying to access the old page. In this case you can try using ie.WaitForComplete(), but sometimes this is required:
ie.SelectList(Find.ById("AutoPostBackSelect")).Option(Find.ByText("Option")).Select();
System.Threading.Thread.Sleep(200); //Sleep to make sure post back registers
ie.WaitForComplete();
ie.SelectList(Find.ById("MySelect")).Refresh()
ie.SelectList(Find.ById("MySelect")).Option(Find.ByText("Option 1")).Select();
This is a bug in the select list where if the list is not ready to accept input, and it can throw one several exception types. We solve it like this:
try
{
_domContainer.SelectList(_control.WatinAttribute).Focus();
_domContainer.SelectList(_control.WatinAttribute).Select(value);
}
catch (Exception e)
{
Console.WriteLine("Select list eception caught: " + e.Message + e.StackTrace);
// we have tried once already and failed, so let's wait for half a second
System.Threading.Thread.Sleep(500);
_domContainer.SelectList(_control.WatinAttribute).Select(value);
}
And yes I know that swallowing all exceptions like this is normally bad, but if the exception occurs again, it is thrown to the test code and the test fails.
I noticed this happens if you try and select a value that is already selected.
You can work around this with a pre-check:
if(_sel_ddlPeriodFromDay.GetValue("value")!="1")
_sel_ddlPeriodFromDay.SelectByValue("1");
or maybe use a try catch?
try{_sel_ddlPeriodFromDay.SelectByValue("1");}
catch{}

Resources