Exceptions names list

Ali Çehreli acehreli at yahoo.com
Mon Nov 15 22:44:35 UTC 2021


On 11/15/21 2:28 PM, pascal111 wrote:
 > I want to know where the list of exceptions names that "throw" statement
 > uses.

Such a list is not very useful. Normally all we need is Exception and 
occasionally one or two other specific types.

There can be hundreds of specific types of Exception a program may 
throw, sometimes unknowingly by calling a library that it uses. That 
list can change dramatically simply by adding a single line to the 
program. (That line may call a new module and that module may throw 
dozens of different Exception types.)

What matters is they are all Exceptions. So, most of the time all one 
needs to do is to catch Exception in main and be done with it:

int main() {
   try {
     work();

   } catch (Exception ex) {
     stderr.writefln!"ERROR: %s"(ex.msg);
     return 1;
   }

   return 0;
}

All of my programs use that idiom. (Also note: Error is not being 
caught; just Exception.)

That try-catch block covers all Exception types that may be thrown. The 
user gets a useful message and we are done.

Except, in some cases a specific exception type is useful. For example, 
I needed to catch ConvException that I know std.conv may throw. (This is 
documented on std.conv documentation.)

In that case, I catch that exception in a lower-level function to give 
the user an understandable error message like "Failed to convert 'hello' 
to an integer" so that the user can try entering new data instead of 
exiting the whole program.

Ali



More information about the Digitalmars-d-learn mailing list