Quick Summary of Catching Errors in Java - dummies

Quick Summary of Catching Errors in Java

By John Paul Mueller

Knowing when an error can occur in your Java program is important. You need to understand when to look for a particular problem in your application. However, it’s also important to understand how errors occur.

The kind of error determines the strategy you use to find it and what you can do to fix it. Although some errors will require that you fix the code and update the application, some errors are fixed by retrying the operation or interacting with the user in some way.

Here are the key points you should remember about catching errors in Java:

  • Syntactical (syntax) errors normally result from writing Java code incorrectly.

  • Semantic errors normally result when you write the Java code correctly but apply Java programming concepts incorrectly.

  • Logical errors normally result when the Java code is both written and applied correctly, but the implementation of the task is incorrect.

  • Syntactical errors are the easiest to fix, semantic errors come next, and logical errors are the hardest to fix.

  • Trapping an error means detecting an exception object and doing something with it.

  • Handling an error means resolving the exception by displaying a message to the user or relying on other means.

  • All applications should include the appropriate error handling code.

  • When providing complete error handling, always handle the most specific errors first, and then move on to less specific errors.

  • Use Optional objects to help reduce the potential for errors in your application.

  • Provide specific information whenever possible when throwing an error.

  • Java makes it possible to catch multiple exceptions using a single catch clause.

  • When the current level of the application can’t handle an exception, make sure you pass it up to the next level.

  • Checked exceptions ensure that anyone using the associated method provides the required error handling.

And here is the tech talk you need to know to work with errors in Java programs:

  • background processing: Tasks that an application performs behind the scenes. For example, when a user requests that an application print a document, the application performs this task in the background while the user continues to interact with the application in the foreground.

  • call stack: A listing of the methods that preceded the current method. Each method called the next in line in order to get to the current level of the call stack. Most applications have a large number of levels, so the call stack is an essential tool in fixing application errors.

  • checked exception: An exception that the JRE forces the user of a class to handle. The compiler actually checks to verify that you have provided handling for the exception and won’t create a .class file for you if you don’t provide the handling. The reason to use checked exceptions is to ensure that a class and the objects created from it perform reliably.

  • error: Any unexpected or unanticipated application behavior, including actions or results that fall outside the realm of expected application output.

  • error handling: The act of resolving an exception that the application has trapped by displaying a message to the user or taking other action.

  • error trapping: The act of detecting an exception and passing this information to a catch statement that resolves the correct exception object.

  • exception: A special object that defines an error condition within an application. The exception tells where the error has happened and what sort of error has happened. In some cases, the exception may provide additional bits of information that the recipient can use to resolve the error.

  • foreground processing: Priority tasks that an application performs as part of interacting with the user (including the system). For example, the user interface for any application is a foreground processing task. In most cases, foreground processing occurs at a higher priority than background processing to ensure that the application can meet user demands in a timely manner.

  • parse: The act of interpreting input and producing an output based on that input. For example, when a programmer provides code to a Java compiler, the compiler produces Java byte code as output. Likewise, when a user provides a numeric string as input, a parser can produce a numeric value as output.

  • throw: The act of creating an exception object and passing it to the caller of the current code. Throwing an exception means that the current code can’t resolve the error and requires help to fix it. The exception object will help the caller understand what has happened and enable the caller to fix the problem in many situations.