peterlck / hamcrest

Automatically exported from code.google.com/p/hamcrest
0 stars 0 forks source link

Exception from String.valueOf() in BaseDescription makes some Exceptions un-reportable. #184

Closed GoogleCodeExporter closed 10 years ago

GoogleCodeExporter commented 10 years ago
In org.hamcrest.BaseDescription, the method appendValue will fail with an 
exception if String.valueOf throws an exception.

This cause the JUnit test to fail with the not very helpful message: "Exception 
in 'main'".

This makes an expectation failure almost impossible to track down (with out 
sitting in the debugger) if the reporting of that failure also causes an 
expectation failure.  This is simple to encounter if an object involved has a 
non-trivial toString method that runs into an expectation error.

The appendValue method needs to rap the String.valueOf with a catch(Throwable) 
and report in some safe way that toString caused an error.  Otherwise, trying 
to report an exception causes that same exception to be thrown again.

Example below:

        } catch (Throwable t) {
            try {
                System.out.println(t);
            } catch (Throwable t2) {
                //here: t2==t, thus the exception cant be logged.
                System.out.println(t2);
            }
        }

Original issue reported on code.google.com by chris.h...@hcs.us.com on 23 May 2012 at 6:10