Why I can't catch the exception?

ag0aep6g via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Jun 5 11:16:29 PDT 2016


On 06/05/2016 08:02 PM, Suliman wrote:
> I really can't understand why try-catch block do not handle exception.
> digit 1 is printing, so exception is accrue after it, but why nothing in
> catch block?
>
> http://img.ctrlv.in/img/16/06/05/57546861d8e81.png
>
> Here is my code:
>
> void dbSetup()
> {
>      try
>      {
[...]
>      }
>
>      catch(Exception e)
>      {
>          writeln("Can't setup DB");
>          writeln(e.msg);
>      }
>
> }

An AssertError is not an Exception (in the narrow sense) [1], but an 
Error. Both Exception and Error [2] derive from Throwable [3].

An Error signals an unrecoverable problem. You should not try to catch 
and handle them.

An AssertError in particular is thrown when an `assert` fails. In your 
case the assert is in database\storage.d, line 312. A failing assert 
signals means that there's an error in the program. A condition is not 
met that should be met at all times.

The compiler will let you catch Error and Throwable, but you really 
shouldn't do that. Instead fix your code so that it doesn't fail the assert.


[1] http://dlang.org/phobos/object.html#.Exception
[2] http://dlang.org/phobos/object.html#.Error
[3] http://dlang.org/phobos/object.html#.Throwable


More information about the Digitalmars-d-learn mailing list