unit3newLMS
unit3newLMS
unit3newLMS
Throwable
Exception Error
RuntimeException
Uncaught Exception
• Any exception that is not caught by your program will ultimately be processed by
the default handler.
• The default handler displays a string describing the exception, prints a stack trace
from the point at which the exception occurred, and terminates the program.
Using try and catch
• To guard against and handle a run-time error, simply enclose the code that you
want to monitor inside a try block.
• Immediately following the try block, include a catch clause that specifies the
exception type that you wish to catch.
Multiple catch Clauses
• In some cases, more than one exception could be raised by a single piece of code.
To handle this type of situation, you can specify two or more catch clauses, each
catching a different type of exception.
• When an exception is thrown, each catch statement is inspected in order, and the
first one whose type matches that of the exception is executed.
• After one catch statement executes, the others are bypassed, and execution
continues after the try /catch block.
Nested try Statements
• The try statement can be nested. That is, a try statement can be inside the block
of another try.
• Each time a try statement is entered, the context of that exception is pushed on
the stack.
• If an inner try statement does not have a catch handler for a particular
exception, the stack is unwound and the next try statement’s catch handlers are
inspected for a match.
• This continues until one of the catch statements succeeds, or until all of the
nested try statements are exhausted.
• If no catch statement matches, then the Java run-time system will handle the
exception.
Using throw
• The program can throw an exception explicitly, using the throw statement.
• The general form of throw is throw ThrowableInstance;
• Here, ThrowableInstance must be an object of type Throwable or a subclass of
Throwable.
• Primitive types, such as int or char, as well as non-Throwable classes, such as String and
Object, cannot be used as exception
• There are two ways you can obtain a Throwable object: using a parameter in a catch
clause or creating one with the new operator.
• The flow of execution stops immediately after the throw statement; any subsequent
statements are not executed.
• The nearest enclosing try block is inspected to see if it has a catch statement that
matches the type of exception.
Using throws
• If a method causes an exception that it does not handle, it must specify this
behavior so that callers of the method can guard themselves against that
exception.
• A throws clause lists the types of exceptions that a method might throw.
• This is necessary for all exceptions, except those of type Error or
RuntimeException, or any of their subclasses.
This is the general form of a method declaration that includes a throws clause:
type method-name(parameter-list) throws exception-list
{
// body of method
}
Using finally
• Any code that absolutely must be executed after a try block
completes is put in a finally block.
• Any exception that is not caught by your program will ultimately be
processed by the default handler.
Checked Exceptions in Java
• These are the exceptions that are not checked at compile time. In
C++, all exceptions are unchecked, so it is not forced by the compiler’s
to either handle or specify the exception.
• In Java, exceptions under Error and RuntimeException classes are
unchecked exceptions.
Built-in Exception
Object
inherits
Scanner
Different methods in Scanner Class
Method Description