Why Are There Bad Number Exceptions in Java?

Why Are There Bad Number Exceptions in Java? thumbnail
Java programs often parse input data such as numbers.

Exceptions can cause Java programs to crash, but when used correctly, they also allow programmers to create applications that can cope with unexpected events. Number exceptions often occur when programs attempt to parse numbers, converting text strings into numerical values. If a Java program uses a method in which a number is not of the expected type, it may throw a Number Format Exception. By understanding exceptions, you can learn how to create programs that provide reliable functionality.

  1. Exception Types

    • Java programs can throw many different types of exception. The Number Format Exception is the most common numerical exception. A runtime exception, it extends the Illegal Argument Exception class. Normally this exception arises when you use a method to attempt to parse a string as a number, but the content of the string is not a number of the correct type. Because it is an unchecked exception, your programs may compile without your Integrated Development Environment warning you about the possible error. The Number Format Exception only occurs if a program passes inappropriate input to a method, so you may only discover it when your application runs.

    Causes

    • The Java language wrapper classes provide object representations of the primitive number types, including integer, float and double. Using these classes, programs can parse string values as numerical types, as follows:

      Integer.parseInt("3");

      This code executes without error, because the passed string parameter contains a number value. However, the following method call causes Java to throw a Number Format Exception because the string does not contain a number:

      Integer.parseInt("r");

      The following code also causes the exception, because although the string is a number, it is not an integer:

      Integer.parseInt("3.5");

      All of these code excerpts compile and run, so the programmer may only become aware of the error at runtime. The values are not normally supplied explicitly, but are read from an external source or variable value.

    Catching

    • You can catch Number Format Exceptions if your programs are likely to throw them. By adding your number parsing code to a try block, then supplying a catch black after it, you can create programs that will continue to function even if a Number Format Exception arises. The following sample code demonstrates this structure:

      try {
      Integer.parseInt(numString);
      }
      catch(NumberFormatException numExc) { System.out.println(numExc.getMessage()); }

      The parse string in this case is represented as a variable reference, so its content is unknown. If the string content cannot be parsed as an integer, the code immediately jumps to the catch block, where you can provide alternative processing.

    Throwing

    • As well as catching exceptions, developers can create Java programs in which they throw custom exceptions. This is a common tactic with unchecked exceptions like Number Format. The following amended code demonstrates:

      catch(NumberFormatException numExc) { throw new BadNumberException(); }

      This allows programmers to define their own custom exception class, which may be a checked type, and to provide a suitable coping strategy for the application.

Related Searches:

References

Resources

  • Photo Credit Hemera Technologies/AbleStock.com/Getty Images

Comments

Related Ads

Featured