Results 1 to 4 of 4

Thread: Exception Class

  1. #1
    Join Date
    Jul 2009
    Posts
    154

    Exception Class

    Hi,
    I want to write a program that adds, subtracts, multiplies, and divides. If the program does any mistake I want java standard exception. So, what are the exception classes for negative numbers, operations by zero and entering letter in place of numbers. Any advice or help will be appreciated. Thanks in advance.

  2. #2
    Join Date
    Feb 2008
    Posts
    1,852

    Re: Exception Class

    There is a NumberFormatException. This exception class is automatically thrown. Refer the code below
    Code:
    String strNumber = "addfeff";
      try {
        double aDouble = Double.parseDouble(strNumber);
      }
      catch (NumberFormatException ex) {
        // the user entered something that was not parseable into a number (e.g. letters)
      }
    There is an exception for divide by zero, plz check this code below for operations by zero
    Code:
    double numerator = 5;
        double denominator = 0;
        
        double quotient = numerator / denominator;
        
        System.out.println("result: " + quotient);
    I am posting another code, this is a general example for exceptions classes
    Code:
    double numerator = -6;
        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);
    Hope this may help you

  3. #3
    Join Date
    May 2008
    Posts
    2,389

    Re: Exception Class

    I think we will get a java.lang.ArithmeticException, when numerator and denominator are both ints. I am talking about this example.
    Code:
    double numerator = 6;
        double denominator = 0;
        
        double quotient = numerator / denominator;
        
        System.out.println("result: " + quotient);

  4. #4
    Join Date
    Jul 2009
    Posts
    154

    Re: Exception Class

    Thanks for the help
    I got a basic idea of how to create exceptions in classes. Truly speaking I could not understand 100% of this, but as I will practice on coding I will come to know 100% what you are talking about. Thanks again.

Similar Threads

  1. Replies: 8
    Last Post: 08-10-2011, 11:06 PM
  2. Replies: 5
    Last Post: 12-02-2010, 06:23 PM
  3. Explain the System.Exception class of C#
    By Jesus2 in forum Software Development
    Replies: 3
    Last Post: 17-11-2009, 03:51 AM
  4. Ultra solid drives:Imation M-Class and S-Class
    By Regina in forum Portable Devices
    Replies: 1
    Last Post: 03-04-2009, 10:34 AM
  5. Good news for CBSE CLASS X & CLASS IX - visit learnnext
    By surya_expert in forum Education Career and Job Discussions
    Replies: 0
    Last Post: 13-12-2008, 12:09 PM

Tags for this Thread

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
  •  
Page generated in 1,751,872,873.03011 seconds with 16 queries