| The Nuts and Bolts of the Java Language

## Introducing Exceptions

> The declaration for the `main()` method in the character-counting program has an interesting clause shown here in bold:

> >     public static void main(String[] args)
> >         throws java.io.IOException

> This clause specifies that the `main()` method can throw throw an exception called `java.io.IOException`. So, what's an exception? An exception is an event that occurs during the execution of program that prevents the continuation of the normal flow of instructions. For example, the following code sample is invalid because it tries to divide 7 by 0 which is an undefined operation.

> >     int x = 0;
> >     int y = 7;
> >     System.out.println("answer = " + y/x);

> The divide by zero operation is an exception because it prevents the code from continuing. Different computer systems handle exceptions in different ways; some more elegantly than others. In Java, when an error occurs, such as the divide by zero above, the program throws an exception. You can catch exceptions and try to handle them within a special code segment known as an exception handler.

> In Java, a method must specify the "checked" (non-runtime) exceptions, if any, it can throw. This rule applies to the `main()` method in our example. Thus the `throws` clause in the method's signature.

> You may have noticed that the `main()` method does not throw any exceptions directly. But it can throw a `throws java.io.IOException` indirectly through its call to `System.in.read()`.

> This has been a brief introduction to exceptions in Java. For a complete explanation about throwing, catching, and specifying exceptions refer to Handling Errors using Exceptions.

| The Nuts and Bolts of the Java Language