It will have two constructors, one default- value and one explicit value that uses the message "Illegal dimension for the figure you specified.
It sounds like what you really need for your two constructors are a default (parameter-less) one as well as one which accepts a String
parameter. e.g.,
public IllegalDimensionException() {
super(Eplicitm);
}
public IllegalDimensionException(String m) {
message = m;
}
The super()
call in the default constructor will call Exception
's constructor, which accepts a String
, and pass up your Eplicitm
to it.
However you are making several fundamental flaws in the logic of your code snippet above which I'd like to point out to you:
You can not specify an extra return type for a constructor (as you do in your "constructor two" with public void IllegalDimensionException
). This is because IllegalDimensionException
essentially is the return type, as you are creating a new instance of the object with it. Your above signature will instead create a new method called IllegalDimensionException
which returns void
and must be called by an instance of this exception... so you've essentially created a setter method with an overly complex (and poorly formatted) name.
It does not make sense for an Exception
to have a main(String[])
method. A main()
is essentially the kick start for an entire program, while an Exception
is something that is created when something goes wrong within a program. As you create more complex projects, you will want to keep the function of each class fundamentally separate, distinct, and logical.
A constant such as a default value should be declared as private static final
. As your code stands, any code that can obtain an instance of your IllegalDimensionException
can change the default message by simply calling theException.Eplicitm = "Yo yo yo, wassup."
. Not a very helpful error message. This is because the default privacy of variables is public
. Fortunately, you have not declared it static
yet, so such a change would only affect one instance of the exception... but it is much better practice for there to be only one immutable version of this across all possible exceptions that are created.
Variable names should be in camel case (variableName
) while class names should begin with all capitals (MyClass
). Constants should be in all upper-case with separations as underscores (MY_CONSTANT_VALUE
).
Some of the above might sound nit picky, but it's really not. All of the above either close loopholes in your code (some of which are pretty dangerous) or make your code substantially more readable (which you'll find is a great boon - because even you will forget why in the heck you wrote some section of code when you go back to look at it three months later). For example, I'm sure I'm not the only professional programmer who took a while to figure out what you meant by Eplicitm
(apart from the spelling), because it looks like the name of a complex, custom-defined object... not a String
!