6 Replies - 407 Views - Last Post: 06 April 2013 - 04:43 AM Rate Topic: -----

#1 jinified  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 26-February 13

When to use 'throws' or try catch

Posted 06 April 2013 - 03:25 AM

I was trying to implement my clone() using the Cloneable interface. I encountered an error when I was following the code from Core Java 8th edition. Here is teh code:


public class Soldier implements Cloneable throws CloneNotSupportedException
{
private int attack;
public Soldier(int a){attack = a;}
public void getAttack(){System.out.println(attack);}
public Soldier clone() 
{
Soldier obj = (Soldier)super.clone();
return obj;
}
}



The error is unreported exception CloneNotSupportedException; must be caught or declared to be thrown

However, when I use the try anc catch method of exception handling. It works fine.


public class Soldier implements Cloneable  
{
private int attack;
public Soldier(int a){attack = a;}
public void getAttack(){System.out.println(attack);}
public Soldier clone() 
{
try{
Soldier obj = (Soldier)super.clone();
return obj;
}catch(CloneNotSupportedException e){System.out.println("fail");return null;}
}
}



From what I know, you use try and catch when you want to do something when you encounter an exception. The question is why doesn't throws declaration works as written in the book. Maybe I mess up the syntax. Anyone please enligthen me. I am new to Java but I have had experience with C++. Thnx

Is This A Good Question/Topic? 0
  • +

Replies To: When to use 'throws' or try catch

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2205
  • View blog
  • Posts: 5,239
  • Joined: 10-September 10

Re: When to use 'throws' or try catch

Posted 06 April 2013 - 03:33 AM

The book shows adding the throws declaration to the class, or was that your idea?
Was This Post Helpful? 0
  • +
  • -

#3 jinified  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 26-February 13

Re: When to use 'throws' or try catch

Posted 06 April 2013 - 03:38 AM

I just found out that if I did not include the try and catch block in the Class file and instead use the throws declaration. Then, I need to try and catch it in the testClass file. Oops. My bad for not reading the whole page. besides the first program using the throws declaration for javadoc declaration, what is the difference with a throw exception inside body of the class ?
Was This Post Helpful? 0
  • +
  • -

#4 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2205
  • View blog
  • Posts: 5,239
  • Joined: 10-September 10

Re: When to use 'throws' or try catch

Posted 06 April 2013 - 03:45 AM

I think this answers your question, but you might want to review the whole trail from the beginning to get a different take on the topic.
Was This Post Helpful? 0
  • +
  • -

#5 jinified  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 26-February 13

Re: When to use 'throws' or try catch

Posted 06 April 2013 - 03:48 AM

The book actually uses the throw declaration as part of its sample program. Here is the code snippet
[code]

public static void main(String[] args)
{
try
{
Employee original = new Employee("John Q. Public", 50000);
original.setHireDay(2000, 1, 1);
Employee copy = original.clone();
copy.raiseSalary(10);
copy.setHireDay(2002, 12, 31);
System.out.println("original=" + original);
System.out.println("copy=" + copy);
}
catch (CloneNotSupportedException e)
{
e.printStackTrace();
}
}


public Employee clone() throws CloneNotSupportedException
{
// call Object.clone()
Employee cloned = (Employee) super.clone();
// clone mutable fields
cloned.hireDay = (Date) hireDay.clone();
return cloned;
}
Was This Post Helpful? 0
  • +
  • -

#6 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2205
  • View blog
  • Posts: 5,239
  • Joined: 10-September 10

Re: When to use 'throws' or try catch

Posted 06 April 2013 - 03:55 AM

To put things in a more recognizable context (and code tags), assuming the two methods you posted are enclosed in the same class, the construction would look something like:
public class TestClass2
{
    public static void main(String[] args)
    {
        try
        {
            Employee original = new Employee("John Q. Public", 50000);
            original.setHireDay(2000, 1, 1);
            Employee copy = original.clone();
            copy.raiseSalary(10);
            copy.setHireDay(2002, 12, 31);
            System.out.println("original=" + original);
            System.out.println("copy=" + copy);
        }
        catch (CloneNotSupportedException e)
        {
            e.printStackTrace();
        }
    }

    public Employee clone() throws CloneNotSupportedException
    {
        // call Object.clone()
        Employee cloned = (Employee) super.clone();
        // clone mutable fields
        cloned.hireDay = (Date) hireDay.clone();
        return cloned;
    }
}

The second method, clone() is specified to throw an exception so that its callers must handle them. This is consistent with the explanation given at the link I sent you.
Was This Post Helpful? 0
  • +
  • -

#7 jinified  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 26-February 13

Re: When to use 'throws' or try catch

Posted 06 April 2013 - 04:43 AM

Thanks for the link. It really clears things up.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1