Reputation: 87
I have read that it is possible to create a constructor that checks whether the input is correct or not and I tried doing so with an IllegalArgumentException. My code works fine as long as I do not create an object, it does not matter if the input is correct or not, the compiler will give an error. This is my code:
public class Knoten{
private String typ;
public Knoten(String typ) throws Exception{
try{
if (typ.equals ("BASE")||typ.equals ("WORD")||typ.equals ("ROOT")||typ.equals ("AFFIX")){
this.typ=typ;
}}catch(IllegalArgumentException ex){
System.out.println ("invalid typ");
}}
public String getTyp(){
return typ;
}
public String toString(){
return "typ: "+getTyp();
}
public static void main (String args[]){
Knoten Knot1= new Knoten("haha");//throws error
Knoten Knot2= new Knoten("BASE");//throws error
}}
I tried also doing this, getting the same result:
public class Knoten{
private String typ;
public Knoten(String typ) throws Exception{
if (typ.equals ("BASE")||typ.equals ("WORD")||typ.equals ("ROOT")||typ.equals ("AFFIX")){
this.type=type;
}else{
throw new IllegalArgumentException("invalid type");
}
}
public String getTyp(){
return typ;
}
public String toString(){
return "typ: "+getTyp();
}
public static void main (String args[]){
Knoten Knot1= new Knoten("haha");//throws error
Knoten Knot2= new Knoten("BASE");//throws error
}
}
So my question is if I have to handle the exception every time I create an object of that class or if there is a nicer way of doing this.
Upvotes: 0
Views: 936
Reputation: 133
you have a typo error in your code . Use
this.typ = typ; // instead of this.type = type;
and also remeber to catch the Exception that is being thrown by the constructor
Upvotes: 0
Reputation: 301
You need to catch the exception when you instantiate your new object, in this case your second solution is fine, except on main you would do:
public static void main (String args[]){
try {
Knoten Knot1= new Knoten("haha"); // This will throw error
Knoten Knot2= new Knoten("BASE");
}
catch (IllegalArgumentException e) {
// deal with exception here
}
}
Also, I would really look into using enums
for your solution, and then you would not need to validate your type when creating a new object. For instance, you could have an enum
like so:
public enum KnotenType {
BASE,
WORD,
ROOT,
AFFIX,
}
Then your constructor would look like:
public Knoten(KnotenType type) {
...
}
More info on Enums: https://docs.oracle.com/javase/tutorial/java/javaOO/enum.html
Upvotes: 0
Reputation: 7315
I believe you are getting compilation error like: Unhandled exception type Exception
and you are getting this as you are throwing a Checked Exception
(java.lang.Exception
) from the constructor
. Don't put any Exception in throws or try to throw the unchecked exception like :
public Knoten(String type) throws IllegalArgumentException { // throws unchecked exception is optional
if (type.equals("BASE") || type.equals("WORD") || type.equals("ROOT") || type.equals("AFFIX")) {
this.type = type;
} else {
throw new IllegalArgumentException("invalid type");
}
}
Or surround it Constructor try catch/finally
.
try {
Knoten Knot1 = new Knoten("haha");
} catch (Exception e) {
e.printStackTrace();
}
Upvotes: 1