DIP1001: Exception Handling Extensions

Superstar64 via Digitalmars-d-announce digitalmars-d-announce at puremagic.com
Sun Jul 10 14:55:04 PDT 2016


On Sunday, 10 July 2016 at 21:43:08 UTC, Chris Wright wrote:
> On Sun, 10 Jul 2016 19:55:37 +0000, Superstar64 wrote:
>
>> link: https://github.com/dlang/DIPs/pull/9 file: 
>> https://github.com/Superstar64/DIPs/blob/exception_extensions/DIPs/
> DIP1001.md
>
> So if my function calls any runtime functions -- it allocates 
> memory, slices an array, etc -- I can't use C-style exception 
> handling. Unless I manually do something like:
>
> struct ThrowableWrapper {
>   Throwable error;
> }
> int[] foo(int i) {
>   try {
>     return [i];
>   } catch (Throwable t) {
>     throw ThrowableWrapper(t);
>   }
> }

You could use both c style and d stack unwinding:
---
struct CustomErrorCode
{
     enum __ErrorCode = true;

}

struct CustomUnwind
{

}

int foo(int[] arr, int index) 
@throws(Throwable,CustomErrorCode,CustomUnwind)
{
     auto val = arr[index]; // may throw RangeError with d's 
existing handling
     if (val == 0xdead)
     {
         throw CustomErrorCode(); // throws using new error code 
handling
     }
     else if (val == 0xbad)
     {
         throw CustomUnwind(); // throws using new by value 
unwinding
     }
}
---

Or possible wrap your function in a template that catch Unwind 
exceptions and throws Error Codes.



More information about the Digitalmars-d-announce mailing list