Cause of Java Exceptions

Java is like all other programming languages in that there are strict rules on how data is handled and logic is followed. If your program deviates from the rules or it receives a type of data that's not expected, an error is generated. In Java the error is called an exception. There are three types of exceptions in Java: checked, error and runtime.

  1. Checked Exception

    • The checked exception occurs due to a programming error. This is usually seen when the wrong type of data is passed to a Java API method. A programmer should anticipate these types of errors and check for correct data and gracefully handle the error. For example, if a program has to calculate gross pay by multiplying hours worked by pay rate and pay rate is defined as a character field, the computer would not know how to perform the multiplication since a character variable is not allowed in that operation. In Java this would generate a checked exception.

    Error Exception

    • Error exceptions are generated by circumstances outside of the Java program. If the program were to print a report but no printer were attached, this would generate an error exception. Error exceptions are unlike checked and runtime exceptions; error exceptions can not be caught. An error exception will always generate a stack trace (see below).

    Runtime Exception

    • The final class of exceptions is the runtime exception. This is an exception that is generated inside the Java application but is not anticipated. A prime example of a runtime exception would be trying to access an array element that is outside of the defined scope of the array.

    Stack Trace

    • If an exception is generated and not "caught," then the Java Virtual Machine (JVM) will generate a stack trace. The trace is very helpful to a programmer; it shows what steps the program had taken before the exception was caused. Many times the stack trace will list the exact line of code that caused the exception. From here a programmer can look at the code to determine what was wrong. While stack traces are very handy for programmers to use for debugging, a well-written Java program should never allow the user to see a stack trace. If a stack trace were displayed from a web-based Java application, it could give hackers a road map on how to hack the site.

    Try...Catch Block

    • Java considers certain operations like opening and reading files to be dangerous code. This danger code needs special treatment by Java. Danger code needs to be enclosed in a try...catch block. The catch portion allows the programmer to handle exceptions gracefully. If the exception is not handled by the programmer, Java will display a stack trace. A try...catch block looks like this:

      try
      {
      // open the file to write to
      Output = new PrintWriter( new FileWriter("output.dat"));
      } catch (IOexception e) {
      System.out.println("Can not open output file");

      Now if the open fails, the message "Can not open output file" is displayed, not a stack trace.

Related Searches:

Comments

You May Also Like

Related Ads

Featured