Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 06-11-2009
Member
 
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.
Reply With Quote
  #2  
Old 06-11-2009
Member
 
Join Date: Feb 2008
Posts: 1,848
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
Reply With Quote
  #3  
Old 06-11-2009
Member
 
Join Date: May 2008
Posts: 2,383
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);
__________________
The FIFA Manager 2009 PC Game
Reply With Quote
  #4  
Old 06-11-2009
Member
 
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.
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Exception Class"
Thread Thread Starter Forum Replies Last Post
Upgrade Sony Ericsson Xperia Play Micro SD card from class 2 8GB to class 4 32GB Aaryan2011 Portable Devices 8 08-10-2011 11:06 PM
How to initialize a derived class with an instance of the base class in c#? KAIRU26 Software Development 5 12-02-2010 06:23 PM
Explain the System.Exception class of C# Jesus2 Software Development 3 17-11-2009 03:51 AM
Ultra solid drives:Imation M-Class and S-Class Regina Portable Devices 1 03-04-2009 10:34 AM
Good news for CBSE CLASS X & CLASS IX - visit learnnext surya_expert Education Career and Job Discussions 0 13-12-2008 12:09 PM


All times are GMT +5.5. The time now is 08:00 PM.