Programming Java: Understanding Error Sources - dummies

Programming Java: Understanding Error Sources

By John Paul Mueller

Except for the simplest of Java applications, it’s highly unlikely that you’ll ever encounter an application that is error free. Errors in coding happen for a wide variety of reasons. It isn’t enough to create an application that uses the correct code. The application must also have the right logic and use arguments in precisely the correct manner.

In some cases, an error actually exists outside of the application, so the application doesn’t even have control over whether the error occurs. The best the application can do is to handle the error so that the application doesn’t crash and potentially damage the user’s data.

A coding error represents any action that the application takes that is outside of the anticipated behavior for that application. Errors can be dramatic, such as an application crash. However, errors are often more subtle, such as displaying incorrect output or producing an unexpected result.

In some cases, errors aren’t noticed by the user at all, but they do present opportunities for nefarious individuals to cause damage to the application, its data, or the system that supports it.

There are situations where the code in a particular part of your application can’t handle an error. Perhaps the code doesn’t have enough information or it doesn’t have the required rights to perform a task. In some cases, you need to interact with the user to allow the user to make the decision.

These are just a few of the circumstances where your application actually has to signal an error. A Java application uses an exception to signal an error. Throwing an exception means to create one and send it to the caller.

An exception is an alert of a sort. It describes an exceptional condition in your application and tells you that an error has occurred. Exceptions can happen for all sorts of reasons. The error might be in your code or in the application environment.

For example, if your application tries to open a file that doesn’t exist, the Java Runtime Environment (JRE) will generate an exception that tells your application about the error.

Throwing an exception means to create the exception object and then send it to the part of the application that called the current code. When your application throws an exception, it creates an object that tells others that it has encountered an error it can’t fix. As the term suggests, throwing an exception is something your code does for exceptional conditions, when no other alternative is available.

There are many sources of error in an application. You may assume that the error is in the code, but often it isn’t. The error could exist in a library or in some other code over which the application has no control. In some cases, the error has nothing to do with the code, but with the application environment.

For example, the application may not have access to a resource that it requires to work. A file might be missing, or the network connection might be down. Users also cause errors by providing incorrect input.

For the most part, there aren’t any errors that an application can’t handle in some way when the application code is robust enough and has enough information to resolve it. However, before an application can do anything with an error, it must have the information required to resolve it.

One of the best tools that a developer has is to make an application robust enough to handle any errors is to classify the error and understand why it happens.