Errors with Exceptions


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: Errors with Exceptions

  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!!!!!!!!



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