Ch11 Slides JHTP11
Ch11 Slides JHTP11
converted to number.
SQLException,PatternSyntaxException
© Copyright 1992-2018 by Pearson Education, Inc. All Rights
Reserved.
© Copyright 1992-2018 by Pearson Education, Inc. All Rights Reserved.
Example: Divide by Zero without Exception Handling
Exceptions are thrown (i.e., the exception occurs) by a method detects
a problem and is unable to handle it.
Stack trace—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.
the debugging symbols for that method’s class were not available to
the JVM—this is typically the case for the classes of the Java API.
methods.
◦ But if not, use a try/catch to handle the exception
◦ input.next() OR input.nextLine();
If the user makes a mistake, the program catches and handles (i.e.,
deals with) the exception—in this case, allowing the user to try to
enter the input.
process.
◦ The parameter’s name enables the catch block to interact with a caught
exception object.
messages.
◦ By default, displays data to the command prompt.
block.
Known as the termination model of exception handling.
◦ Some languages use the resumption model of exception handling, in which, after
an exception is handled, control resumes just after the throw point.
Termination model is like a go-to, when handling is done, it exits the try/catch.
Resumption model is like a function call, when handling is done, it returns.
exception, and that try block’s catch blocks attempt to handle it.
method declaration.
Stating the preconditions and postconditions before writing a method
final, because its enclosing method never modifies the variable after
it’s declared and initialized, then the variable is effectively final
◦ Frequently are used with lambdas (Chapter 17, Lambdas and Streams).
As of Java SE 9, you can create an AutoCloseable object and assign
it to a local variable that’s explicitly declared final or that’s
effectively final
Then, you can use it in a try-with-resources statement that releases