www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 16799] New: try-catch blocks fail to catch exception in

https://issues.dlang.org/show_bug.cgi?id=16799

          Issue ID: 16799
           Summary: try-catch blocks fail to catch exception in druntime
                    unittests
           Product: D
           Version: D2
          Hardware: x86_64
                OS: All
            Status: NEW
          Severity: enhancement
          Priority: P1
         Component: druntime
          Assignee: nobody puremagic.com
          Reporter: lucia.mcojocaru gmail.com

The code below was meant to be a unittest in druntime/src/lifetime.d:
+unittest
 +{
 +    import core.exception : UnicodeException;
 +
 +    try
 +    {
 +        string ret;
 +        int i = -1;
 +        ret ~= i;
 +    }
 +    catch(UnicodeException e)
 +    {
 +        assert(e.msg == "Invalid UTF-8 sequence");
 +    }
 +}

The code in the try block throws a UnicodeException, but the catch block fails
to catch it. Thus the tests fail.

However, the test passes if a template is defined for assertThrown and used
instead of the inline code:
static void assertThrown(T : Throwable = Exception, E)(lazy E expr, string msg)
 +    {
 +        try
 +            expr;
 +        catch (T e) {
 +            assert(e.msg == msg);
 +            return;
 +        }
 +    }
assertThrown!UnicodeException(f(), "Invalid UTF-8 sequence");

This assertThrown template is defined in 2 other places in druntime with slight
variations:

src/core/time.d:version(unittest) void _assertThrown(T : Throwable = Exception,
E)

src/rt/minfo.d:    static void assertThrown(T : Throwable, E)(lazy E expr,
string msg)

--
Nov 27 2016