Thread overview
Problem with Exception inheritance
Oct 02, 2004
Sjoerd van Leent
Oct 03, 2004
Sjoerd van Leent
Oct 03, 2004
Regan Heath
October 02, 2004
Hello All,

If I want to inherit an exception, and want to do something simple as the following:

# module bootoptions.BootstrapOptionsException;
#
# public class BootstrapOptionsException : Exception {
#
# }

The compiler behaves weird. The following error is displayed:

---
constructor this (char[]msg) does not match argument types ()
Error: expected 1 arguments, not 0
---

As far as I know, the constructor is implemented in the runtime library, and since I am not overriding the constructor it should just use that constructor.

Am I doing something wrong over here?

Regards,
Sjoerd

DMD version: 0.102 (Windows)
October 03, 2004
B.t.w., The same error occurs on my Linux platform, which uses a somewhat outdated DMD compiler.

Regards,
Sjoerd

Sjoerd van Leent wrote:
> Hello All,
> 
> If I want to inherit an exception, and want to do something simple as the following:
> 
> # module bootoptions.BootstrapOptionsException;
> #
> # public class BootstrapOptionsException : Exception {
> #
> # }
> 
> The compiler behaves weird. The following error is displayed:
> 
> ---
> constructor this (char[]msg) does not match argument types ()
> Error: expected 1 arguments, not 0
> ---
> 
> As far as I know, the constructor is implemented in the runtime library, and since I am not overriding the constructor it should just use that constructor.
> 
> Am I doing something wrong over here?
> 
> Regards,
> Sjoerd
> 
> DMD version: 0.102 (Windows)
October 03, 2004
On Sat, 02 Oct 2004 21:02:10 +0200, Sjoerd van Leent <svanleent@wanadoo.nl> wrote:
> Hello All,
>
> If I want to inherit an exception, and want to do something simple as the following:
>
> # module bootoptions.BootstrapOptionsException;
> #
> # public class BootstrapOptionsException : Exception {
> #
> # }
>
> The compiler behaves weird. The following error is displayed:
>
> ---
> constructor this (char[]msg) does not match argument types ()
> Error: expected 1 arguments, not 0
> ---
>
> As far as I know, the constructor is implemented in the runtime library, and since I am not overriding the constructor it should just use that constructor.
>
> Am I doing something wrong over here?

I have had that same problem.

I assume it is because the Exception class only has 1 constructor and it takes a 'char[]'. This is a problem because...

When you declare a class like so:

class A : B {
}

it auto-magically has a constructor, and that constructor calls B's constructor like so:

this() {
  super();
}

note, super is not being passed any parameters, so, it would call the constructor for B that expected no parameters. In your case it tries to call the constructor for Exception that expects no parameters, unfortunately there are none, so you get an error.

Try adding this constructor to your class:

this() {
  super("BootstrapOptionsException");
}

Is it possible to do something like this:

class CustomException : Exception {
  this()
  {
    super(typeid(this));  //not sure what to put here
  }
}

class MyError : CustomException {
}

or better yet, add the constructor above to Exception, so that by default it gets initialised with the name of the exception.

Regan

-- 
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/