Int overFlowException and UnderFlow exception

  • (2 Pages)
  • +
  • 1
  • 2

22 Replies - 2078 Views - Last Post: 03 May 2013 - 10:55 AM Rate Topic: -----

#1 Astro52a9  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 79
  • Joined: 24-March 13

Int overFlowException and UnderFlow exception

Posted 01 May 2013 - 09:38 PM

Define two CHECKED exception OverFlow and UnderFlow
Write two separate class to test them

public static int add( int a, int b ) //throws Underflow, Overflow 


Recall that the range of type int is :
-2,147,483,648 to + 2,147,483,648

How to make a checked exception

thanks
Is This A Good Question/Topic? 0
  • +

Replies To: Int overFlowException and UnderFlow exception

#2 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10376
  • View blog
  • Posts: 38,415
  • Joined: 27-December 08

Re: Int overFlowException and UnderFlow exception

Posted 01 May 2013 - 10:01 PM

Checked Exceptions are those that extend the Exception class, but not RuntimeException. Unchecked Exceptions extend RuntimeException.
Was This Post Helpful? 0
  • +
  • -

#3 Astro52a9  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 79
  • Joined: 24-March 13

Re: Int overFlowException and UnderFlow exception

Posted 01 May 2013 - 10:29 PM

this is what i have come up with so far but dont know how to do the negative part


public class OverFlowException extends Exception {

    public OverFlowException() {
    }

    public OverFlowException(String msg) {
        super(msg);
    }

}



public class UnderFlowException extends Exception{

    public UnderFlowException()
    {}

    public UnderFlowException(String message){
        super(message);
    }
}



public class Test {

    public static int add(int a, int B)/> throws OverFlowException,UnderFlowException{

        int sum = a + b ;

        try{
            if(sum < 0){
                throw new OverFlowException
            }else{
                return sum;
            }


        }catch (OverFlowException e){
            e.getMessage();
        }

    }
}


Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Int overFlowException and UnderFlow exception

Posted 02 May 2013 - 03:51 AM

You don't try and catch the exeception yourself... it is useless.
Also your Exception call missed the ().
Should be

public class Test {

    public static int add(int a, int B)/> throws OverFlowException,UnderFlowException{

        int sum = a + b ;

        if(sum < 0){
           throw new OverFlowException("Sorry too bog");
        return sum;
    }
}



for underflow/overflow difference you will have to check if a and b are both positive or both negative

if both positive and sum is negative: oveflow
if both negative and sum is positive: underflow
Was This Post Helpful? 2
  • +
  • -

#5 Gungnir  Icon User is offline

  • Your Imaginary Friend

Reputation: 152
  • View blog
  • Posts: 527
  • Joined: 21-May 11

Re: Int overFlowException and UnderFlow exception

Posted 02 May 2013 - 04:38 AM

Within a certain range.

int a = 2147483647 + 1; 
int b = 2147483647 + 1;
System.out.print(Test.add( a, b ));

//OUTPUT
0


This post has been edited by Gungnir: 02 May 2013 - 07:07 AM

Was This Post Helpful? 0
  • +
  • -

#6 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2088
  • View blog
  • Posts: 3,176
  • Joined: 21-June 11

Re: Int overFlowException and UnderFlow exception

Posted 02 May 2013 - 06:54 AM

View PostGungnir, on 02 May 2013 - 01:38 PM, said:

I would make a slight adjustment to the above example:
public class Test {

    public static int add(int a, int b ) throws OverFlowException,UnderFlowException {
        //Cast to long to extend the range of accuracy
        long sum = (long)a + (long)b ;

        if(sum < 0){
           throw new OverFlowException("Sorry too big");
        return sum;
    }
}



Your adjustment breaks the code. First you need a cast at the end for the code to compile (and incidentally, you don't need the casts on line 5) and second using long to store the result means the exception will never occur (the sum of two ints will always fit into a long), so any overflow would happen on the return line (when the long is converted back to an int) and silently swallowed, making the whole thing pointless.

Quote

As pbl mentioned, you won't be able to catch the exception, as the JVM doesn't throw it itself.


He's able to catch the exception just fine (he's throwing it himself if you look at the code). It just doesn't make any sense for him to do so.
Was This Post Helpful? 0
  • +
  • -

#7 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7564
  • View blog
  • Posts: 12,688
  • Joined: 19-March 11

Re: Int overFlowException and UnderFlow exception

Posted 02 May 2013 - 07:11 AM

I would prefer to use something like Gungnir's approach and comparing the result to Integer.MAX_VALUE and MIN_VALUE. This communicates intent much better than relying on the inherent int overflow.

One sensible thing to do, by the way, with the OverFlow and UnderFlow Exceptions, is to report a String representation of the resulting value so the user at least gets an idea of the answer they were looking for. You can do this if you handle the internal arithmetic as a long.

This post has been edited by jon.kiparsky: 02 May 2013 - 07:16 AM

Was This Post Helpful? 2
  • +
  • -

#8 Astro52a9  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 79
  • Joined: 24-March 13

Re: Int overFlowException and UnderFlow exception

Posted 02 May 2013 - 09:29 AM

this is what my method is now but it doesn't catch anything

public class Test {

    public static int add(int a, int B)/> throws OverFlowException,UnderFlowException{

        int sum = a + b ;

        try{
            if(sum < Integer.MAX_VALUE){

                throw new OverFlowException("Sorry too big ");
            }

            if(sum > Integer.MIN_VALUE){

                throw new UnderFlowException("Sorry too Small");
            }

                return sum;

        }catch (OverFlowException e){
            e.getMessage();
        }

        return  sum;
    }
}




my test drive
import java.util.Scanner;

public class OverUnderFlowTestDrive {

    public static void main(String[] args) {

        int maxInt = Integer.MAX_VALUE;
        int minInt = Integer.MIN_VALUE;
        int userNum = 0;

        Scanner scn = new Scanner(System.in);
        try {
            System.out.print("Enter a number :");
            userNum = scn.nextInt();
            
            if (userNum > 0){

                Test.add(userNum,maxInt);
                
            }
            
            if(userNum < 0) {

                Test.add(userNum,maxInt);
            }


        } catch (OverFlowException e) {

            e.getMessage();

        } catch (UnderFlowException e) {

            e.getMessage();
        }

    }//end of main

}// end of class



Exception class

public class UnderFlowException extends Exception{

    public UnderFlowException(String message){
        super("UnderFlowException been called !");
    }
}



public class OverFlowException extends Exception {

    public OverFlowException(String msg) {
        super("OverFlowException is been called !");
    }

}



but it doesn't work
Was This Post Helpful? 0
  • +
  • -

#9 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7564
  • View blog
  • Posts: 12,688
  • Joined: 19-March 11

Re: Int overFlowException and UnderFlow exception

Posted 02 May 2013 - 09:38 AM

If you catch the overflow in the driver, you won't be able to test it in the add() method.
Was This Post Helpful? 0
  • +
  • -

#10 Astro52a9  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 79
  • Joined: 24-March 13

Re: Int overFlowException and UnderFlow exception

Posted 02 May 2013 - 09:43 AM

this is what i did but still no result i can not get the message to print
public class Test {

    public static int add(int a, int B)/> throws OverFlowException,UnderFlowException{

        int sum = a + b ;


            if(sum < 0){

                throw new OverFlowException();
            }

            if(sum > 0){

                throw new UnderFlowException();
            }

        return  sum;
    }
}

Was This Post Helpful? 0
  • +
  • -

#11 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2650
  • View blog
  • Posts: 11,179
  • Joined: 20-September 08

Re: Int overFlowException and UnderFlow exception

Posted 02 May 2013 - 01:57 PM

I'm getting confused as to who suggested what. e.g. i can't see what this is quoting. Anyway, whoever suggested using long is i think right. I don't see how you could possibly detect under/overflow otherwise (other than using BigInteger)
Was This Post Helpful? 0
  • +
  • -

#12 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7564
  • View blog
  • Posts: 12,688
  • Joined: 19-March 11

Re: Int overFlowException and UnderFlow exception

Posted 02 May 2013 - 02:02 PM

View Postg00se, on 02 May 2013 - 03:57 PM, said:

I'm getting confused as to who suggested what. e.g. i can't see what this is quoting. Anyway, whoever suggested using long is i think right. I don't see how you could possibly detect under/overflow otherwise (other than using BigInteger)



Well, pbl's approach is based on the notion that if i+j < 0 then you've overflowed. Which works, assuming you are adding just two ints, but it's not very communicative.
Was This Post Helpful? 0
  • +
  • -

#13 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2650
  • View blog
  • Posts: 11,179
  • Joined: 20-September 08

Re: Int overFlowException and UnderFlow exception

Posted 02 May 2013 - 02:13 PM

Quote

if i+j < 0 then you've overflowed
Why? Let i = 0, j = -1. Both values are a long long way from over/underflow, as indeed is their sum
Was This Post Helpful? 0
  • +
  • -

#14 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7564
  • View blog
  • Posts: 12,688
  • Joined: 19-March 11

Re: Int overFlowException and UnderFlow exception

Posted 02 May 2013 - 02:40 PM

You've got a point... :)
Was This Post Helpful? 0
  • +
  • -

#15 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2088
  • View blog
  • Posts: 3,176
  • Joined: 21-June 11

Re: Int overFlowException and UnderFlow exception

Posted 02 May 2013 - 04:54 PM

View Postg00se, on 02 May 2013 - 11:13 PM, said:

Quote

if i+j < 0 then you've overflowed
Why? Let i = 0, j = -1. Both values are a long long way from over/underflow, as indeed is their sum


Right. You'd have to check the signs of the operands. If you add two numbers of the same sign, the result will have the same sign iff there was no overflow. If you add two numbers of different signs, there can be no overflow.

That works reliably and unlike the solution of using longs, you can use the same logic if you wanted to add two longs and check for overflow. On the other hand, as Jon pointed out, the intent is more clear from looking at the code when using longs (that is when using longs the way that Jon suggested -- the way that Gungnir suggested (originally before he edited his post) wouldn't work as I pointed out).
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2