What really happens in a try { return x; } finally { x = null; } statement?

The finally statement is executed, but the return value isn’t affected. The execution order is: Code before return statement is executed Expression in return statement is evaluated finally block is executed Result evaluated in step 2 is returned Here’s a short program to demonstrate: using System; class Test { static string x; static void Main() … Read more

Getting Cross-thread operation not valid [duplicate]

You can only make changes to WinForm controls from the master thread. You need to check whether InvokeRequired is true on the control and then Invoke the method as needed. You can do something like this to make it work: public void CheckUnusedTabs(string strTabToRemove) { if (TaskBarRef.tabControl1.InvokeRequired) { TaskBarRef.tabControl1.Invoke(new Action<string>(CheckUnusedTabs), strTabToRemove); return; } TabPage tp … Read more

JSF 2 Global exception handling, navigation to error page not happening

It’s most likely because the current request is an ajax (asynchronous) request. The exception handler which you’ve there is designed for regular (synchronous) requests. The proper way to change the view in case of an ajax exception is as follows: String viewId = “/error.xhtml”; ViewHandler viewHandler = context.getApplication().getViewHandler(); context.setViewRoot(viewHandler.createView(context, viewId)); context.getPartialViewContext().setRenderAll(true); context.renderResponse(); This is however … Read more

In Java how can I validate a thrown exception with JUnit?

In JUnit 4 it can be easily done using ExpectedException rule. Here is example from javadocs: // These tests all pass. public static class HasExpectedException { @Rule public ExpectedException thrown = ExpectedException.none(); @Test public void throwsNothing() { // no exception expected, none thrown: passes. } @Test public void throwsNullPointerException() { thrown.expect(NullPointerException.class); throw new NullPointerException(); } … Read more

What should I know about Structured Exceptions (SEH) in C++?

They are the Win32 equivalent to Unix signals, and let you catch CPU exceptions such as access violation, illegal instruction, divide by zero. With the right compiler options (/EHa for Visual C++), C++ exceptions use the same mechanism as stack unwinding works properly for both C++ (user) exceptions and SEH (OS) exceptions. Unlike C++ exceptions, … Read more