# Java: Exceptions - More

Exceptions | Exception Usage | Exceptions - More

## Kinds of Exceptions

There are many exceptions, but they can be put into two groups: checked exceptions and unchecked exceptions. There is some controversy about which type you should use. A discussion of some of the issues can be found at Java theory and practice: The exceptions debate.

- Unchecked Exceptions -- These exceptions are usually something that should have been prevented by more careful programming. For example, you should never get `NullPointerException` or `ArrayIndexOutOfBoundsException`. If you do, there is something wrong with your program, and you need to fix it. You usually don't catch unchecked exceptions. Instead, fix your program so it can't produce one of these. However, `NumberFormatException` is the one exception of this type that is usually caught.
- Checked Exceptions -- These are usually errors in the input data. The programmer has no control over the input the user gives you, eg, file names, .... If the user gives you a bad value, it may cause an exception when you use it. You need to check for bad input using a `try` statement.

## Exception hierarchy

[Not yet written]

## Multiple catch clauses

[Not yet written]

## Order of catch clauses

[Not yet written]

## Printing the stack trace

[Not yet written]

## Throwing exceptions

[Not yet written]

## Danger from the intermediate layers - `finally`

Exceptions provide a good infrastructure for error processing. The simplicity of throwing an exception at a deep level and catching it at a high level may generate problems at the intermediate, skipped, levels. Did any of these methods leave any part of a data structure or resource in an inconsistent state? Each place that this may be true of needs to enclose critical code in a `try...finally` block.

## Define libarary exceptions to tell programmer they should fix their code

If you write a library that is used by others, throw your own execeptions if your code is called with illegal values, eg, something isn't initialized. Your exceptions are a way to tell the applications programmer that they should fix their code.

## Defining your own exceptions

You can also create your own exceptions. [Not yet written]