Throw an exception but "hide" the top frame?
ketmar via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Wed Dec 24 06:09:23 PST 2014
On Wed, 24 Dec 2014 14:04:50 +0000
aldanor via Digitalmars-d-learn <digitalmars-d-learn at puremagic.com>
wrote:
> On Wednesday, 24 December 2014 at 13:48:26 UTC, ketmar via
> Digitalmars-d-learn wrote:
> > the `object.Exception at wrap.d` is not a backtrace result, this
> > is the
> > result of Exception class constructor:
> >
> > this (string msg, string file=__FILE__, usize line=__LINE__,
> > Throwable next=null)
> >
> > it registering the file and line of exception object creation in
> > COMPILE time. so you can do nothing with it, as there is no way
> > to know
> > what called what in compile time.
> >
> > p.s. if you can use GDC, for example, and turn on debug info,
> > backtrace will show you files and line numbers for every
> > address.
>
> Hmm, that makes sense. After some pondering, I think I've hacked
> together a workaround though:
>
> template check(alias func) {
> auto check(string file = __FILE__, int line =
> __LINE__)(int x) {
> auto result = func(x);
> if (result < 0) // throw on negative return vaule
> throw new Exception("%d < 0".format(result),
> file, line);
> return x; // otherwise, pass the result through
> }
> }
>
> which produces:
>
> 2
> object.Exception at wrap.d(23): -3 < 0
> ----------------
> .. (_Dmain+0x20) [0x448de4]
>
>
> Are there any hidden downsides to doing it this way (aside from
> generating additional code on every call)?
template bloat. now compiler can't use one instance of parameterized
template function for each call, but has to generate new template each
time.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 181 bytes
Desc: not available
URL: <http://lists.puremagic.com/pipermail/digitalmars-d-learn/attachments/20141224/14227022/attachment.sig>
More information about the Digitalmars-d-learn
mailing list