Silent exceptions in try/catch in debug mode (howto)

Ali Çehreli acehreli at yahoo.com
Thu Mar 7 14:38:05 PST 2013


On 03/07/2013 01:31 PM, D-ratiseur wrote:
 > Hello, is it possible to hide an exception with an empty "catch"
 > block ?
 > ---
 > try{
 > //somestuff
 > }
 > catch{
 > // an error ocurred but make someotherstuff and continue
 > }

import std.stdio;

unittest
{
     try {
         writeln("throwing");
         throw new Exception("unfortunate");

     } catch (Exception exc) {
     }

     writeln("life goes on");
}

void main()
{}

Note that some exceptions are not descendents of Exception:

     Throwable
      /     \
  Error    Exception

If you really have to, you can catch by Error or Throwable. However, 
catching by Error or Throwable is not advisable because you can't be 
sure of the state of the program when such an exception is thrown.

 > ---
 > I'm in a console program and running some unittests with the
 > -debug switch also set(but I don't debug I just run the appli.).
 > the console app does not continue while I would need the appli to
 > get on running. Actually I need to determinate if a segmentation
 > error is triggered in the try block or not and let the program
 > continue. But the program always stops...
 > Is it possible ? How could I shut down an exception in D ?

Unfortunately, segmentation faults are not translated to exceptions.

Ali

P.S. This thread would be interesting to people who follow the D.learn 
newsgroup. :)



More information about the Digitalmars-d mailing list