Results 1 to 3 of 3

Thread: Exception class

  1. #1
    mmarkym is offline Member
    Join Date
    Nov 2009
    Posts
    20
    Rep Power
    0

    Default Exception class

    I'm writing a program that adds, subtracts, multiplies, and divides. What exception classes are there that would account for 1) negative numbers 2) operations by zero 3) entering letters in place of numbers.

    mark

  2. #2
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    6

    Default

    for #3, there is the NumberFormatException. this is automatically thrown from the Integer, Double object wrappers for int, double

    Java Code:
      String strNumber = "12345sdgf";
      try {
        double aDouble = Double.parseDouble(strNumber);
      }
      catch (NumberFormatException ex) {
        // the user entered something that was not parseable into a number (e.g. letters)
      }
    2. there isn't usually an exceptions when operations by zero, here I assume you mean divide by zero.
    Java Code:
        double numerator = 4;
        double denominator = 0;
        
        double quotient = numerator / denominator;
        
        System.out.println("result: " + quotient);
    what is displayed is "result: infinity"

    A good general exception that I like to use for general validation of what my program expects, like my business rules, is the IllegalArgumentException. It extends RuntimeException, so that means you don't have to explicitly wrap things into a try/catch all the time.

    for example, back to this divide example, i now hande a check for negative numbers, and divide by zero

    Java Code:
        double numerator = -5;
        double denominator = 0;
        
        if (numerator < 0) {
          throw new IllegalArgumentException("numerator cannot be less than zero: " + numerator);
        }
    
        if (denominator <= 0) {
          throw new IllegalArgumentException("denominator cannot be negative or zero: " + denominator);
        }
    
        // in this example, because of the checking above, we would not get here, exception would be thrown instead.
    
        double quotient = numerator / denominator;
        
        System.out.println("result: " + quotient);

  3. #3
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    Quote Originally Posted by travishein View Post
    Java Code:
        double numerator = 4;
        double denominator = 0;
        
        double quotient = numerator / denominator;
        
        System.out.println("result: " + quotient);
    I will just point out that if numerator and denominator were BOTH ints, you would get a java.lang.ArithmeticException
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

Similar Threads

  1. Need Help with Class Cast Exception
    By soxfan714 in forum New To Java
    Replies: 2
    Last Post: 11-03-2011, 04:33 PM
  2. exception class
    By arulmozs in forum AWT / Swing
    Replies: 2
    Last Post: 10-29-2009, 02:18 PM
  3. Class.forName Exception
    By Moncleared in forum Advanced Java
    Replies: 5
    Last Post: 02-21-2009, 06:08 AM
  4. class cast exception
    By venkatallu in forum New To Java
    Replies: 2
    Last Post: 09-02-2008, 09:50 PM
  5. How to create your own Exception class
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-17-2008, 07:40 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •