Why is SwitchError an error and how is it unsafe to continue after catching it?

aliak something at something.com
Sun Feb 24 10:53:09 UTC 2019


Because from what I understand, an Error is something you should 
not be catching and represents something unrecoverable. And it 
the docs say that it's unsafe to continue execution. But the 
following code is very recoverable and I don't see how it's 
unsafe to continue executing:

import optional;
import core.exception: SwitchError;

enum Enum : string {
   one = "one", two = "two"
}

Optional!Enum makeEnum(string value) {
   try {
     final switch (value) {
     case Enum.one: return some(Enum.one);
     case Enum.two: return some(Enum.two);
     }
   } catch (SwitchError) {
     return no!Enum;
   }
}

unittest {
     assert(makeEnum("one") == some(Enum.one));
     assert(makeEnum("huh") == no!Enum);
}

Cheers,
- Ali


More information about the Digitalmars-d-learn mailing list