www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Problem with Exception inheritance

reply Sjoerd van Leent <svanleent wanadoo.nl> writes:
Hello All,

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







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)
Oct 02 2004
next sibling parent Sjoerd van Leent <svanleent wanadoo.nl> writes:
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:
 





 
 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)
Oct 03 2004
prev sibling parent Regan Heath <regan netwin.co.nz> writes:
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:







 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/
Oct 03 2004