Error-Handling Overview
}Programsfrequently test conditions to determine how program execution should proceed.
}Considerthe following pseudocode:
§Performa task
If the preceding task did notexecute correctly
Perform error processing
Perform next task
§If the preceding task did not execute correctly
Perform error processing
§…
Begins by performing a task; then tests whether it executed correctly. If not, perform error processing.
Otherwise, continue with the next task.
}Intermixing program and error-handling logic in this manner can make programs difficult toread, modify, maintain and debug—especially in large applications.
}Exception handling enables you to remove error-handling code from the “main line” of program execution
§Improves program clarity
§Enhances modifiability
}Handle any exceptions you choose
§All exceptions
§Allexceptions of a certain type
§All exceptions of a group of related types (i.e., related through a superclass).
}Such flexibility reduces the likelihood that errors will be overlooked, thus making programs more robust.
}Exceptions are thrown(i.e., the exception occurs) when a method detects a problem and is unable to handle it.
}Stacktrace—information displayed when an exception occurs and is not handled.
}Information includes:
§The name of the exception in a descriptive message that indicates the problem that occurred
§The method-call stack (i.e., the call chain) at the time it occurred. Represents the path of execution that led to the exception method by method.
}This information helps you debug the program.
.

