-
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);
^
-
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);
> ^
>
-
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
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks