1 assert({ throw new Exception("my message"); }().getException!Exception.msg == "my message"); 2 3 static class MyExc : Exception 4 { 5 this(string file) 6 { 7 this.file = file; 8 super(""); 9 } 10 11 string file; 12 } 13 14 assert({ throw new MyExc("file.txt"); }().getException!MyExc.file == "file.txt"); 15 16 try 17 { 18 assert(getException!MyExc({ }()).file == "file.txt"); 19 } 20 catch (Exception exc) 21 { 22 assert(exc.msg == "Error: No exception was thrown."); 23 }
Return the exception of type Exc that is expected to be thrown when expr is evaluated.
This is useful to verify the custom exception type holds some interesting state.
If no exception is thrown, then a new exception is thrown to notify the user of the missing exception.