Runtime Exceptions

Runtime exceptions generally occur as a result of coding bugs. By far the most common type of runtime exception you will encounter is the NullPointerException. This occurs when you attempt to access a field or method on an object reference that has a null value, i.e. it is not a reference to an object on the heap.

The following is a simple program that generates a NullPointerException:

package exceptions;

public class Runtime {

    public static void main(String[] args) {

        StringBuffer sb = new StringBuffer("Hello");

        StringBuffer sb2 = null;

        appendWorld(sb);

        appendWorld(sb2);

    }

     

    private static void appendWorld(StringBuffer sb) {

        sb.append(" World");

    }

}

If you run this program it will generate the following output:

Exception in thread "main" java.lang.NullPointerException

      at exceptions.Runtime.appendWorld(Runtime.java:11)

      at exceptions.Runtime.main(Runtime.java:7)

This is referred to as a stack trace – it is a dump of the call stack of the program when the exception occurred. Stack traces are the most common mechanism available for diagnosing an exception after the fact.

//

All exceptions contain stack traces, not just RuntimeExxception.

The first line of the stack trace tells you the type of exception that has occurred and the thread that it occurred in. This is a single threaded program, but when I introduce multi-threading you will see why this is important.

The remaining lines of the stack trace show the call-stack of the program at the point the error occurred. You always read stack traces from top to bottom. The first line of the stack-trace is the line that the exception occurred on (line 11 of the Runtime class). The next line tells you the line in the program that called this method (line 7 in the Runtime class).

In real-world programs stack traces can become very long, sometimes 20 or 30 levels deep, but usually the first couple of lines are sufficient to understand what was happening when the exception occurred.

Once you understand the context of the exception you can quickly determine what has happened. If you look at line 11 of Runtime, the only reason that this exception could have occurred would be if sb was null. You can therefore determine that at line 7 of the Runtime class you are passing a null StringBuffer to appendWorld.

You will notice that methods do not need to declare the fact that they throw runtime exceptions. The reason for this is that the list of possible runtime exceptions that could conceivably be thrown by a method could be large (virtually all methods could generate a NullPointerException).

The fact that you do not have to handle runtime exceptions leads some programmers to favor them over checked exceptions – even in cases where checked exceptions are more appropriate. Try not to fall into this trap – if an exception represents a business rule violation, rather than a coding bug, it should not be a RuntimeException.

I mentioned earlier that RuntimeExceptions such as NullPointerExeption should not be handled. The reason for this is that they are bugs: you never should have passed a null StringBuffer to the appendHello method. Instead of adding exception-handling logic, therefore, you should change your code. For instance, you may decide that the best way to handle this exception is for appendWorld to check whether it has been passed a null reference:

private static void appendWorld(StringBuffer sb) {

    if (sb != null) {

        sb.append(" World");

    }

}

The following are the most common RuntimeExceptions you will encounter in Java:

IllegalArgumentException: this occurs when an invalid value is passed to a method. For instance, if the method only accepts positive numbers, it may throw an IllegalArgumentException if it is passed a negative number.

IllegalStateException: this indicates that the method has been invoked at an illegal time. I could have used this exception instead of UninitializedException earlier, because I was essentially stating that an invocation had occurred at the wrong time (before the object was initialized).

ArrayIndexOutOfBoundsException: this occurs when you access an index in an array that is greater than the length of the array.

ClassCastException: this occurs when you cast an object to be an invalid type: for instance if you cast a String to a StringBuffer.

UnsupportedOperationException: as its name suggests, this is thrown when an operation is not supported. For instance, if a method required by an interface has not been implemented yet it could throw an UnsupportedOperationException.

ArithmeticException: this most commonly occurs when you divide an integer by 0.

There is nothing to stop you throwing these exceptions in your own code. In fact, before creating your own specific exception types it always makes sense to find out if an existing exception type exists in Java.

A Software Engineer Learns Java and Object Orientated Programming
titlepage.xhtml
part0000_split_000.html
part0000_split_001.html
part0000_split_002.html
part0000_split_003.html
part0000_split_004.html
part0000_split_005.html
part0000_split_006.html
part0000_split_007.html
part0000_split_008.html
part0000_split_009.html
part0000_split_010.html
part0000_split_011.html
part0000_split_012.html
part0000_split_013.html
part0000_split_014.html
part0000_split_015.html
part0000_split_016.html
part0000_split_017.html
part0000_split_018.html
part0000_split_019.html
part0000_split_020.html
part0000_split_021.html
part0000_split_022.html
part0000_split_023.html
part0000_split_024.html
part0000_split_025.html
part0000_split_026.html
part0000_split_027.html
part0000_split_028.html
part0000_split_029.html
part0000_split_030.html
part0000_split_031.html
part0000_split_032.html
part0000_split_033.html
part0000_split_034.html
part0000_split_035.html
part0000_split_036.html
part0000_split_037.html
part0000_split_038.html
part0000_split_039.html
part0000_split_040.html
part0000_split_041.html
part0000_split_042.html
part0000_split_043.html
part0000_split_044.html
part0000_split_045.html
part0000_split_046.html
part0000_split_047.html
part0000_split_048.html
part0000_split_049.html
part0000_split_050.html
part0000_split_051.html
part0000_split_052.html
part0000_split_053.html
part0000_split_054.html
part0000_split_055.html
part0000_split_056.html
part0000_split_057.html
part0000_split_058.html
part0000_split_059.html
part0000_split_060.html
part0000_split_061.html
part0000_split_062.html
part0000_split_063.html
part0000_split_064.html
part0000_split_065.html
part0000_split_066.html
part0000_split_067.html
part0000_split_068.html
part0000_split_069.html
part0000_split_070.html
part0000_split_071.html
part0000_split_072.html
part0000_split_073.html
part0000_split_074.html
part0000_split_075.html
part0000_split_076.html
part0000_split_077.html
part0000_split_078.html
part0000_split_079.html
part0000_split_080.html
part0000_split_081.html
part0000_split_082.html
part0000_split_083.html
part0000_split_084.html
part0000_split_085.html
part0000_split_086.html
part0000_split_087.html
part0000_split_088.html
part0000_split_089.html
part0000_split_090.html
part0000_split_091.html
part0000_split_092.html
part0000_split_093.html
part0000_split_094.html
part0000_split_095.html
part0000_split_096.html
part0000_split_097.html
part0000_split_098.html
part0000_split_099.html
part0000_split_100.html
part0000_split_101.html
part0000_split_102.html
part0000_split_103.html
part0000_split_104.html
part0000_split_105.html
part0000_split_106.html
part0000_split_107.html
part0000_split_108.html
part0000_split_109.html
part0000_split_110.html
part0000_split_111.html
part0000_split_112.html
part0000_split_113.html
part0000_split_114.html
part0000_split_115.html
part0000_split_116.html
part0000_split_117.html
part0000_split_118.html
part0000_split_119.html
part0000_split_120.html
part0000_split_121.html
part0000_split_122.html
part0000_split_123.html
part0000_split_124.html
part0000_split_125.html
part0000_split_126.html
part0000_split_127.html
part0000_split_128.html
part0000_split_129.html
part0000_split_130.html
part0000_split_131.html
part0000_split_132.html
part0000_split_133.html
part0000_split_134.html
part0000_split_135.html
part0000_split_136.html
part0000_split_137.html
part0000_split_138.html
part0000_split_139.html