Witscale Test Center

5.5 Exceptions


5.5 Exceptions

When you write an application to solve any real world problem, your code mainly consists of two parts. The first one is the application code that actually implements the solution to the problem. The second part is the error handling code that checks all sorts of known error conditions your program may face. While programming in many languages, the application code and error-handling code are mixed together. Such mangled up code is difficult to maintain. Java has an excellent mechanism to solve this long-lasting problem. It separates the error-handling code from actual application logic. The Java mechanism that deals with handling the errors in an organized fashion is called as the exception handling.

With exception handling, you can detect and handle errors without any need to write a complex conditional code to check each possible error condition. Moreover, you can write the same error-handling code to deal with range of possible error conditions. This separation of application code from error-handling code makes the code cleaner, readable and easier to maintain. Java provides the exception-handling constructs like try-catch, try-catch-finally to manage the foreseen errors. Let us see how you can actually handle the erroneous conditions with the help of these constructs.