Why passing a string to the base Exception class?
Hi friends,
I want to know, why one should pass a string message to the base class exception constructor from the own exceptions class's constructor.
Like in the following example:
class MyException extends Exception
{
public MyException()
{
//Some code goes here
}
public MyException(String msg)
{
super(msg);//This step of passing string to base constructor why?
}
So in the above code, why the block line is needed? I mean for what reason are we passing the string parameter to the base constructor by calling it with super?
}
[653 byte] By [
zenwalkera] at [2007-11-26 22:53:41]

> Because if you just call super(), the message will
> not bedome part of the object's state.
Well actually my qs was why we calling super (Base class construtor) also even though if java specialises for us to call it, then why?
And if at all if we call it, then why are we passing string parameter to it?
(For this you might answer that Exception class has a construtor which takes a string parameter) but again why to call base class?
> > Because if you just call super(), the message will
> > not bedome part of the object's state.
>
> Well actually my qs was why we calling super (Base
> class construtor) also even though if java
> specialises for us to call it, then why?
There will always be a call to super(... something ...). SOME superclass constructor must be called. The superclass must have a chance to do its own initialization. If you don't explicitly call some super c'tor, then the compiler will implicitly insert a call to super().
> And if at all if we call it, then why are we passing
> string parameter to it?
Because we want that string to be part of the state of the object, and the superclass provides us a way to hold it, and a way to provide it (via the c'tor).
jverda at 2007-7-10 12:17:05 >

> > Because if you just call super(), the message will
> > not bedome part of the object's state.
>
> Well actually my qs was why we calling super (Base
> class construtor) also even though if java
> specialises for us to call it, then why?
>
> And if at all if we call it, then why are we passing
> string parameter to it?
> (For this you might answer that Exception class has a
> construtor which takes a string parameter) but again
> why to call base class?
That base class sets a String member variable which is the message
text. If you don't call it's corresponding ctor, no message will be set. e.g.public class YourException {
public YourException(String message) {
super(); // message will not be set
}
}
kind regards,
Jos
JosAHa at 2007-7-10 12:17:05 >
