Understanding Exceptions in Java
An exception is an error that happens when an application is running. When an exception condition happens, the exception is said to be thrown, and it changes the normal execution control flow of the program. Java offers a very comprehensive and flexible system for exception handling. The main benefit of such a system is that it largely automates the process of error handling, which otherwise would need to be coded into each application: what a waste of resources that would be. All exceptions are represented by classes organized into a hierarchy tree. Let’s take a look at that tree…well, a part of it.
The Exception Tree in Java s they say, everything in Java is a class (or object), and the exceptions are no exception. They are organized into a hierarchy of classes, a part of which is shown in Figure below.
Checked Exceptions and Runtime Exceptions
When in a program, if you perform an operation that causes an exception—that is, an exception is hrown—you can always catch the exception (you will see how later in the chapter) and deal with it n the code. This is called handling the exception. Based on whether or not you are required to handle hem, the exceptions in Java are classified into two categories: checked exceptions and runtime xceptions.
Checked Exceptions
This is the category of exceptions for which the compiler checks (hence the name checked exceptions) o ensure that your code is prepared for them: prepare for unwelcome but expected guests. These exceptions are the instances of the Exception class or one of its subclasses, excluding the runtime Exception subtree. Checked exceptions are generally related to how the program interacts with its environment; for example, URISyntaxException and ClassNotFoundException are checked exceptions.
The conditions that generate checked exceptions are generally outside the control of your program, and hence they can occur in a correct program. However, you can anticipate (expect) them, and thus you must write the code to deal with them. The rule is: when a checked exception is expected, either declare it in the throws clause of your method or catch it in the body of your method, or do both; i.e. you can just throw it without catching it, you can catch it and recover from it, or you can catch it and rethrow it after doing something with it. Just throwing it without catching it is also called ducking it. You will get more comfortable with the throw and catch terminology by the end of this chapter.
Runtime Exceptions
The exceptions of type RuntimeException occur due to program bugs. The programmer is not required to provide code for these exceptions because if the programming were done correctly in the first place, these exceptions wouldn’t occur, anyway. Because a runtime exception occurs as a result of incorrect code, catching the exception at runtime is not going to help, although doing so is not illegal. However, you would rather write the correct code to avoid the runtime exceptions than write the code to catch them. An exception represented by the ArithmeticException class is an example of runtime exceptions. Again, you do not need to declare or catch these exceptions.
Runtime exceptions (exceptions of type RuntimeException or its subclasses) and errors (of type Error or its subclasses) combined are also called unchecked exceptions and they are mostly thrown by the JVM, whereas the checked exceptions are mostly thrown programmatically. However, there is no rigid rule.
Posted in Java














I, Faisal Basra, keen of Java development. My area of interest are Enterprise java development using latest frameworks like Hiberantes, Struts, JSF and Spring. Currently, I am working in NetSol working in Java EE field.
Apart from the java development, I also have interest in Internet marketing specially in the field of Search Engine Marketing / Search Engine Optimization.
The, very next interest I have is of Web Development side. The Internet speaks the language of PHP, MySQL, Apache and Linux thus mostly said simply its LAMP. So, I also have developed several websites using PHP and related technologies.
