Exception handling

Ali Çehreli via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Jul 14 13:22:21 PDT 2017


On 07/14/2017 12:36 PM, ANtlord wrote:
 > Hello! I've tried to use nothrow keyword and I couldn't get a state of
 > function satisfied the keyword. I have one more method that can throw an
 > exception; it is called inside nothrow method. Every type of an
 > exception from the throwable method is handled by the nothow method.
 >
 > ubyte throwable_fn(ubyte state) {
 >     if(state < 2) {
 >         return 1;
 >     } else if(state == 3) {
 >         throw new MyException1("qwe");
 >     } else {
 >         throw new MyException2("asd");
 >     }
 > }

Although it's obvious to us that there are only those two exceptions, 
the compiler cannot in general know that.

 > void nothrowable_fn() nothrow {
 >     try {
 >         auto val = throwable_fn(3);
 >         // do success staff
 >     } catch(MyException1 e) {
 >         // handle error 1
 >     } catch(MyException2 e) {
 >         // handle error 2
 >     }

All you need is to catch Exception there as well:

     catch(Exception) {
         assert(false, "throwable_fn threw something unexpected");
     }

Ali



More information about the Digitalmars-d-learn mailing list