Bug 16799 – try-catch blocks fail to catch exception in druntime unittests

Status
NEW
Severity
enhancement
Priority
P4
Component
druntime
Product
D
Version
D2
Platform
x86_64
OS
All
Creation time
2016-11-27T14:11:04Z
Last change time
2024-12-07T13:37:08Z
Assigned to
No Owner
Creator
Lucia Cojocaru
Moved to GitHub: dmd#17339 →

Comments

Comment #0 by lucia.mcojocaru — 2016-11-27T14:11:04Z
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)
Comment #1 by robert.schadek — 2024-12-07T13:37:08Z
THIS ISSUE HAS BEEN MOVED TO GITHUB https://github.com/dlang/dmd/issues/17339 DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB