one more problem


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: one more problem

  1. #1
    Lindsey Guest

    one more problem


    One more problem that I hope you can help me with. Iím getting an error with
    the last four lines in this code.

    public static void main (String[] args) {
    Rational r1 = new Rational ();
    Rational r2 = new Rational (5);
    Rational r3 = new Rational (3,7);
    Rational r4 = new Rational (4,-3);
    Rational r5 = new Rational (-5,5);
    Rational r6 = new Rational (3,7);


    Here are the errors. Basically the same thing for all four lines.

    TestRational.java:10: unreported exception java.lang.Exception; must be caught
    or declared
    to be thrown
    Rational r3 = new Rational (3,7);
    ^
    TestRational.java:11: unreported exception java.lang.Exception; must be caught
    or declared
    to be thrown
    Rational r4 = new Rational (4,-3);
    ^
    TestRational.java:12: unreported exception java.lang.Exception; must be caught
    or declared
    to be thrown
    Rational r5 = new Rational (-5,5);
    ^
    TestRational.java:13: unreported exception java.lang.Exception; must be caught
    or declared
    to be thrown
    Rational r6 = new Rational (3,7);
    ^


  2. #2
    Paul Clapham Guest

    Re: one more problem

    That's because the constructor of the Rational class that has two parameters
    has been coded something like this:

    public Rational(int numerator, int denominator) throws Exception {...}

    and probably it will throw an Exception if the denominator parameter is
    zero. Because it's declared to throw an Exception, Java requires you to
    catch this Exception and deal with it if you use this form of the
    constructor. So you have to write something like this:

    try {
    Rational r3 = new Rational (3,7);
    Rational r4 = new Rational (4,-3);
    Rational r5 = new Rational (-5,5);
    Rational r6 = new Rational (3,7);
    } catch(Exception ex) {
    // put code here to deal with zero denominator, if necessary
    }

    Personally, I would have considered having this constructor throw an
    ArithmeticException, which is what actually happens when you execute a
    division by zero in your Java program. That's an exception that you don't
    have to declare, because it's a subclass of RuntimeException.

    PC2

    "Lindsey" <Zave4@aol.com> wrote in message news:3ad099bc$1@news.devx.com...
    >
    > One more problem that I hope you can help me with. I'm getting an error

    with
    > the last four lines in this code.
    >
    > public static void main (String[] args) {
    > Rational r1 = new Rational ();
    > Rational r2 = new Rational (5);
    > Rational r3 = new Rational (3,7);
    > Rational r4 = new Rational (4,-3);
    > Rational r5 = new Rational (-5,5);
    > Rational r6 = new Rational (3,7);
    >
    >
    > Here are the errors. Basically the same thing for all four lines.
    >
    > TestRational.java:10: unreported exception java.lang.Exception; must be

    caught
    > or declared
    > to be thrown
    > Rational r3 = new Rational (3,7);
    > ^
    > TestRational.java:11: unreported exception java.lang.Exception; must be

    caught
    > or declared
    > to be thrown
    > Rational r4 = new Rational (4,-3);
    > ^
    > TestRational.java:12: unreported exception java.lang.Exception; must be

    caught
    > or declared
    > to be thrown
    > Rational r5 = new Rational (-5,5);
    > ^
    > TestRational.java:13: unreported exception java.lang.Exception; must be

    caught
    > or declared
    > to be thrown
    > Rational r6 = new Rational (3,7);
    > ^
    >




  3. #3
    Chris P. Guest

    Re: one more problem


    "Paul Clapham" <pclapham@core-mark.com> wrote:
    >That's because the constructor of the Rational class that has two parameters
    >has been coded something like this:
    >
    >public Rational(int numerator, int denominator) throws Exception {...}
    >
    >and probably it will throw an Exception if the denominator parameter is
    >zero. Because it's declared to throw an Exception, Java requires you to
    >catch this Exception and deal with it if you use this form of the
    >constructor. So you have to write something like this:
    >
    >try {
    > Rational r3 = new Rational (3,7);
    > Rational r4 = new Rational (4,-3);
    > Rational r5 = new Rational (-5,5);
    > Rational r6 = new Rational (3,7);
    >} catch(Exception ex) {
    >// put code here to deal with zero denominator, if necessary
    >}
    >
    >Personally, I would have considered having this constructor throw an
    >ArithmeticException, which is what actually happens when you execute a
    >division by zero in your Java program. That's an exception that you don't
    >have to declare, because it's a subclass of RuntimeException.
    >
    >PC2
    >
    >"Lindsey" <Zave4@aol.com> wrote in message news:3ad099bc$1@news.devx.com...
    >>
    >> One more problem that I hope you can help me with. I'm getting an error

    >with
    >> the last four lines in this code.
    >>
    >> public static void main (String[] args) {
    >> Rational r1 = new Rational ();
    >> Rational r2 = new Rational (5);
    >> Rational r3 = new Rational (3,7);
    >> Rational r4 = new Rational (4,-3);
    >> Rational r5 = new Rational (-5,5);
    >> Rational r6 = new Rational (3,7);
    >>
    >>
    >> Here are the errors. Basically the same thing for all four lines.
    >>
    >> TestRational.java:10: unreported exception java.lang.Exception; must be

    >caught
    >> or declared
    >> to be thrown
    >> Rational r3 = new Rational (3,7);
    >> ^
    >> TestRational.java:11: unreported exception java.lang.Exception; must be

    >caught
    >> or declared
    >> to be thrown
    >> Rational r4 = new Rational (4,-3);
    >> ^
    >> TestRational.java:12: unreported exception java.lang.Exception; must be

    >caught
    >> or declared
    >> to be thrown
    >> Rational r5 = new Rational (-5,5);
    >> ^
    >> TestRational.java:13: unreported exception java.lang.Exception; must be

    >caught
    >> or declared
    >> to be thrown
    >> Rational r6 = new Rational (3,7);
    >> ^
    >>

    Thanks, Paul, for your help here and for solving other problems in this group.
    You're a great gentleman!

    /cp
    DevX
    >



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center