Introduction to Exception Handling Tutorial

Kaboom! · Crash! · @!#$%*!
Welcome to the wonderful world of programming. If your program crashes in a blaze of glory there is a Java term for that special moment – throwable – as in throwable your computer right out the window (don't really do that). The Java Virtual Machine (JVM) is a workhorse and it does its job well, but sometimes it just has a bad day and throwables its hands up in the air and calls it quits. And when it does, it lets you know about it; the JVM tells you exactly what happened and why it threw in the towel. Throwable is actually a class in the java.lang package and its purpose it to handle those special kaboom moments.
What could possibly make the JVM so mad? There are two basic categories for things that really irritate the JVM, errors and exceptions.

Errors

Errors generally happen when things outside of the control of our program occur. They usually occur because of some sort of fundamental problem with the environment the JVM is running in. Things such as malfunctioning hardware, running out of resources, missing class definitions, etc. are common causes of errors. There really isn't much we can do to anticipate errors, so I don't recommend wasting your valuable time trying to recover from them. Errors are handled by the Error class which is part of the java.lang package.

Exceptions

Exceptions expose the more forgiving side of the JVM. Exceptions sometimes occur when the JVM is instructed to do something stupid. For example, what happens if I instruct the JVM to get the substring characters 4 through 10 on a string that contains "Hello"? Suppose that we instruct the JVM to read through a file on a thumb drive when suddenly our super-bright user just yanks it out of the USB port. In either case, the JVM will scream out the issue and throw in the towel – or maybe we can ask it nicely to explore other options???
We can! Java provides us with the necessary tools to allow us to 'talk' to the JVM and resolve any exceptions before throwing in the towel. Exception handling is not an easy concept to master, but once you do, your programs will become robust and reliable. There are five Java keywords that directly relate to exception handling concepts: try, catch, throw, throws, and throwable.

The Stack

Just like we needed to understand the memory heap in order to grasp concepts like objects and garbage collection, we need to understand what the stack is before we can fully comprehend exception handling. My next tutorial will be dedicated entirely to exploring what stack memory is. I will be making an entire mini-series on exception handling and in my next tutorial will go over what the stack is and discuss why it is important to exception handling.



Open the command prompt (CMD - see the Getting Started ) and type in the following commands.

C:\Windows\System32>cd \
C:\>md Java
C:\>cd Java
C:\Java>
C:\Java>md ExceptionIntro
C:\Java>cd ExceptionIntro
C:\Java\ExceptionIntro>Notepad ExceptionIntro.java

Copy and Paste, or type the following code into Notepad and be sure to save the file when you are done.


class ExceptionIntro {
    public static void main(String args[]) {
        String hi = "Hello";
        String really = hi.substring(4,11);

        int x = 12, y = 0, z = 0;        
        z = x / y;
    }
}

Now switch back to the command prompt (CMD) and type in javac ExceptionIntro.java and press Enter.
Now type in java ExceptionIntro and press Enter.


C:\Java\ExceptionIntro>javac ExceptionIntro.java
C:\Java\ExceptionIntro>java ExceptionIntro
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String out of range: 11
    at java.lang.String.substring(Unknown Source)
    at ExceptionIntro.main(ExceptionIntro.java:4)

Exception in thread "main" java.lang.ArithmeticException: / by zero
    at ExceptionIntro.main(ExceptionIntro.java:7)


Final thoughts

The Throwable class is the top superclass in the hierarchy of crash management. The Error class and the Exception class both extend Throwable and thus they are subclasses of Throwable. I will not discuss the Error class and Error handling much if ever from here on out. I will discuss the Exception class and Exception handling in great detail. Stay tuned for my next tutorial on Exception Handling: Stack Memory Introduction.


Tutorials