You have so bad syntax :)
if
statement as it is doesn't throw exception of it's own
Lets repeat :)
If:
if(condition){
//commands while true
}
if(condition)
//if there is just 1 command, you dont need brackets
if(condition){
//cmd if true
}else{
//cmd for false
}
//theoretically (without brackets in case of single command following)
if(condition)
//true command;
else
//false command;
Try-Catch:
try{
//danger code
}catch(ExceptionClass e){
//work with exception 'e'
}
- There are more ways how to show the custom message:
try{
if (this.sides <= 0){
throw new NegativeSidesException();
}else{
//make code which you want- entered value is above zero
//in fact, you dont need else there- once exception is thrown, it goes into catch automatically
}
}catch(NegativeSidesException e){
System.out.println(e + "You entered 0 or less");
}
Or maybe better (you will do not loose information about exception thrown):
try{
if (this.sides > 0){
//do what you want
}else{
throw new NegativeSidesException();
}
}catch(NegativeSidesException e){
System.out.println(e + "You entered 0 or less");
}
Btw You can use java default Exception (that message is better to specify as constant in above of class):
throw new Exception("You entered 0 or less");
//in catch block
System.out.println(e.getMessage());