Jack ilkgu
Jack ilkgu

Reputation: 5

Java Try & Catch example

So I have this code right here but I do not understand why my Try statement is underlined.When I hover over it, it says b3 is already defined in method main. What would I have to do to make it work the way it is shown. ( Everything Else works)

public class ReviewTest {
public static void main(String[] args)
{
boxes b1 = new boxes();
System.out.printf("My first Box = %s\n", b1.toString());
System.out.printf("My first Box = %s\n", b1);

boxes b2 = new boxes(10.0, 12.4, 13.9);
System.out.printf("My second Box = %s\n" , b2);

System.out.printf("The volumne of b2 = %.3f\n", b2.volume());
System.out.printf("The surface Area of b2 = %.3f\n", b2.surfaceArea());

boxes b3 = null;

try {
   boxes b3 = new boxes(-1,4.3, 12);
}
catch (IllegalArgumentException ex)
{
  System.out.println(ex.getMessage());
}       
}

Upvotes: 0

Views: 110

Answers (2)

DripDrop
DripDrop

Reputation: 1002

This is because you have defined b3. What you need to do to fix this is this:

Right before your try/catch block, you defined boxes b3 = null;. What you need to do is go to inside your try block, and remove the boxes part before b3 = new boxes(-1,4.3, 12);.

Upvotes: 1

iMysak
iMysak

Reputation: 2228

please change boxes b3 = new boxes(-1,4.3, 12); on b3 = new boxes(-1,4.3, 12);

Upvotes: 0

Related Questions