DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 2 of 2
  1. #1
    Lindsey Guest

    Errors with Exceptions


    Here is my code that had the problem.......

    Rationals.java:16: unreported exception java.lang.Exception; must be caught
    or declared to
    be thrown
    throw new Exception(" Zero in denominator.");
    ^
    1 error



    Here is the error i get......

    public Rationals(int num, int den){
    if(den == 0){
    throw new Exception(" Zero in denominator.");
    }
    numerator = num;
    denominator = den;
    if (denominator < 0){
    numerator *= -1;
    denominator *= -1;
    }
    }
    PLEASE HELP!!!!!!!!

  2. #2
    Ako Guest

    Re: Errors with Exceptions


    In the method header you should say which Exceptions your method might throw.
    Just add this to your method header

    public Rationals(int num, int den) throws Exception {

    it would be a better practice if you define your own exception, DividedByZeroException
    and then throw it if your test fails.

    Ako

    "Lindsey" <Zave4@aol.com> wrote:
    >
    >Here is my code that had the problem.......
    >
    >Rationals.java:16: unreported exception java.lang.Exception; must be caught
    >or declared to
    >be thrown
    > throw new Exception(" Zero in denominator.");
    > ^
    >1 error
    >
    >
    >
    >Here is the error i get......
    >
    > public Rationals(int num, int den){
    > if(den == 0){
    > throw new Exception(" Zero in denominator.");
    > }
    > numerator = num;
    > denominator = den;
    > if (denominator < 0){
    > numerator *= -1;
    > denominator *= -1;
    > }
    > }
    >PLEASE HELP!!!!!!!!



Bookmarks

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


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


Sponsored Links