List of exceptions?

Ali Çehreli acehreli at yahoo.com
Sat Oct 10 14:56:31 UTC 2020


On 10/10/20 5:12 AM, DMon wrote:
> Is there a list of a list of the exceptions or what can be used with catch?
> 
> I'm thinking that I missed it and there is something easier than 
> breaking old code, scouring the site, or hypnotic regression.

Only Throwable and classes that are derived from it can be thrown and 
caught.

It has two decendants:

    Throwable
     /     \
  Error   Exception

Throwable and Error are caught very rarely in special situations. For 
example, a thread may catch all types of exceptions to report the reason 
why it's about to die.

So the only type that is and should be used in most programs is Exception:

void main() {
   try {
     foo();

   } catch (Exception e) {
     writefln!"Something bad happened: %s"(e.msg);

     // You can continue if it makes sense
   }
}

So, programs base their exceptions on Exception:

class MyException : Exception {
   this(int i) {
     import std.format;
     super(format!"Bad int happened: %s"(i));
   }
}

void foo() {
   throw new MyException(42);
}

void main() {
   foo();
}

There are helpers in std.exception e.g.

class MyException : Exception {
   import std.exception;
   mixin basicExceptionCtors;
}

void foo() {
   throw new MyException("a problem happened");
}

void main() {
   foo();
}

I have some basic information here:

   http://ddili.org/ders/d.en/exceptions.html

Ali


More information about the Digitalmars-d-learn mailing list