3

So I got some problems implementing a try catch block in my program. It's quite simple, all I want is to throw an exception whenever the user enters a 0 or less into my dialog window. Here is my code:

try {
    if (this.sides <= 0);
} catch (NegativeSidesException exception) {
    System.out.println(exception + "You entered 0 or less");
}

The NegativeSidesException is my own defined exception.

When I make 0 the input the try catch block doesn't catch it and the compiler throws a normal exception and terminates the program.

vezunchik
  • 3,669
  • 3
  • 16
  • 25
Essej
  • 892
  • 6
  • 18
  • 33

3 Answers3

12

Change if (this.sides <= 0);

To if (this.sides <= 0) throw new Exception ("some error message");

And every thing will work as you want

Ramanlfc
  • 8,283
  • 1
  • 18
  • 24
boskras
  • 144
  • 3
9

Create a new object for the exception and throw it explicitly.

try{
    if (this.sides <= 0)
        throw new NegativeSidesException();
}
catch (NegativeSidesException exception)
{
    System.out.println(exception + "You entered 0 or less");        
}
Kush
  • 755
  • 7
  • 22
  • 1
    This solved it thanks! Do I always have to create a new object for the exception, or is it just when I have my own defined? – Essej Dec 01 '15 at 09:33
  • When you want to create an exception by yourself(explicitly) doesn't matter its a custom or java default exception then you need to create a new object and use `throw` keyword so that following catch can control the flow of execution from there. – Kush Dec 01 '15 at 09:36
8

You have so bad syntax :)

  1. 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'
}
  1. 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());
xxxvodnikxxx
  • 1,270
  • 2
  • 18
  • 37