Repository navigation
An exception is an abnormal condition that arises in a code sequence at run time.
An exception is a run time error.
Keywords:
Exception handling is managed by five keyword
try
catch
throw
throws
finally
Java exception hierarchy
All java exception class hierarchy inherits directly or indirectly from class exception.
Programmers can extend this hierarchy to create their own exception class.
Superclass Throwable
-Subclass Exception
-Exceptional situation
-Should be caught by program
-Subclass Error
-typically not caught by program
Inheritance hierarchy for class Throwable
Throwable<-Exception<-RuntimeException
<-IOException
<-Error <-AWTError
<-ThreadDeath
<-OutOfMemoryError
Unchecked Exception & Checked Exception
Unchecked Exception:
All exception types that are directly or indirectly subclass of class RuntimeException.
Checked Exception:
All class that inherits from class Exception but not class Runtime Exception.
Multiple catch clauses:
If more than one exception could be raised in a single piece of code. We can specify multiple catch block.
After one catch statement executes, the other are bypassed.\
try:
Program statement that you want to monitor for exception is contained within try block.
catch: your program can catch the exception and handle it.
throw: Use to throw an exception manually.
throws: A method may throw an exception must be specified by a throws clause
throw keyword
Using 'throw' keyword, it is possible for your program to throw an exception explicitly.
General form: throw ThrowableInstance.
Here, ThrowableInstance must be an object of type Throwable or a subclass of Throwable.
throws Keyword :
A method may throw an exception must be specified by a throws clause.
finally keyword: There are some code in a program that must be executed, whether the exception is occurred or not occurred. finally creates a block that will be executed after try/catch has completed.
finally block will be executed whether or not the exception is thrown.
finally block is optional.