Exception
Exception
called Exception
- It is highly recommended to handle Exceptions and the main objective of Exception
Hnadling is graceful termination of the program
-
Exception Handling : Exceptio Hndling doesn't mean repairing an exception we have
to provide alternative way to continue rest of the program normally is a concept of
Exception Hnadling
Throwable class : -Throwable class access root for java exception hierarchey
-Throwable class defines two child classes 1) Exception 2) Error
Exception : Most of the times Exceptions are caused by our program and these are
recoverable
Error : Most of the times errors are not caused by our program and these are due to
lack of system resources. Errors are non-recoverable
Checked Exceptions :exception which are checked by compiler for smooth execution of
program at run time
-In our program if there is a chance of rising checked exception then compulsory we
should handle that checked exception (either my try/catch or throws keyword)
otherwise we wll get compile time error
-Example : FileeNotFoundException,Halllticketnotfoundexception,penforgotexception
etc..
-Types
1)Fullychecked : A checked exception is said to be fully checked if and only if all
its child classes also checked
Example : IOExpeption,InteruptedException
Unchecked Exceptions : the exceptions which are not checked by compiler wheather
programmer handling it or not such types of exceptions are called unchecked
exceptions
-Example : ArthematicExcepton, GasBlastexception etc.
Note: -RE and its child classes,Errors and its child classes are unchecked expect
remainning are checked Exception (pleaserefere diagram durgajava 46:00 )
................................................................................
Customized Exception Hndling using try-Catch :
-Note : a code which may cause exception is called risky code and we have to define
that code inside try block and the corresponding handling code we have to define
inside catch block
case 1: if exception occurs at stmt 2 and corresponding catch block match :- 1,4,5
statement will execute
Note : within the try block if anywhere an exception rised then rest of the try
block wont be executed when though we handled that exception hence within the try
block we have take only risky code and the try block should be as less as possible
case 2: if exception occurs at stmt 2 and corresponding catch block not match :- 1
statement will execute (Abnormal termination)
case 3: if exception occurs at stmt 4 or 5 then it is always abnormal termination
Example 1: will give compile time error saying CE :Exception java.lanag.AE has
already been caught
try
{
risky code
}
catch(Exception e)
{
}
catch(ArthemeticException e)
{
}
..........................................................
final : -final is the modifier applicable for classes methods and variables
-if a class declared as final then we cannot extend that class
-if a method declared as final we cannot override that method in the child class
-if a variable declared has final then we cannot perform reassignment for that
variable
finally : -it is a block always associated with the try-catch to maintain cleanup
code
-the speciality of finally block is it will be executed always irrespective of
wether exception rises or not wether it is handled or not but finally always
execute
...................................................................................
...............................
Example : possible try,catch and finally block combinations
-ex1 : // CE: try without catch or finally
try
{
}
.....
ex2: //CE :catch without try
catch()
{
}
......
ex3 :
finally
{
}
......
....................
ex7 ://CE: finally without try
try
{
}
catch()
{
}
SOP("hi")
finally
{
}
..........................................
ex8 : // no problem in this code
try
{
try
{
}
catch()
{
}
}
catch()
{
}
,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
}
catch()
{
}
..............................
ex10 : No problem
try
{
try
{
}
fiannly
{
}
}
catch()
{
}
........................................
ex11 : no problem
try
{
}
catch()
{
try
{
}
finally
{
}
}
................................
ex12 : CE : finally without try
try
{
}
catch(0
{
finally
{
}
}
..............................................
ex13 : No problem
try
{
}
catch()
{
}
finally
{
try
{
}
catch()
{
}
}
...............................
ex14: CE : finallywithout try
try
{
}
catch()
{
}
finally
{
finally
{
}
}
...............................................
ex15 : CE : for each and every try,catch and finally block curly brackets are
required even if there is single statment present
try
SOP("hu");
catch()
{
SOP("hi");
}
finally
{
}
........................................................
throw keyword :- Sometimes we can create Exception object explcitly and we can
handover to the JVM manually for this we have to use throw keyword
-(using thorw handover our created exception object to the JVM manually)throw new
= AE("/ by zero") : here created AE object explicitly
-hence the main objective of throw keyword is to handover our created exception
object to the JVM manually instead of default exception handler
-visit 17:30 part 5 for program
-best use of throw keyword is for user defined exceptions
-examples
1)case 1 : //RE : saying AE
class Test
{
static AE a = new AE();
P S V M()
{
throw e;
}
}
3)case 3 : will give AE at runtime but wont give any compile time error
class Test
{
p s v m()
{
SOP("10/0")
SOP("Hello");
}}
4)case 4 : will give CE saying unreachable statement because we cannot right any
statement after thow keyword
class Test
{
p s v m()
{
throw e
AE e = new AE("/ by zero")
SOP("Hello"); //here look
}}
case 5: throw keyword only be used with throwable object so here we will get CE:
incompatible types found
class Test
{
p s v m(String[] args)
{
throw new Test();
}
}
case 6: //RE : exception in thread main" Test because Test object is now throwable
object
class Test extends RuntimeException
{
p s v m(String[] args)
{
throw new Test();
}
}
...................................................................................
......................
Example 3 : In above example if we remove any one throws statement then the code
wont compile CE : unreported exception
class Test
{
p s v m () thows IE
{
doStuff();
}
public void doStuff()thows IE
{
domoreStuff();
}
public void domoreStuff() thows IE
{
Thread.sleep(1000)
}
...................................................................................
............................
-case 1 : we can use throws keyword for methods and constructors but not for
classes
class Test throws Exception //not possible will get CE
{
test() throws Exception
{
}
public void m1() throws Exception
{
}
}
--case 2 : throws keyword only for throwable types if we are tryig to use for
normal java classes then we will get CE : incompatible types
class test
{
public void m1() throws Test
{
}
}
...................................................................................
.................
class Test
{
p s v m()
{
throw new Exception();
}
}
class Test
{
p s v m()
{
throw new Error();
}
}
...................................................................................
.................
-case 3 : CE :
class Test
{
p s v m()
{
try {
sop("Hello")
}
catch(IOException e)
{
}
}
}
-case 4 : // CE :
class Test
{
p s v m()
{
try {
sop("Hello")
}
catch(InterruptedException e)
{
}
}
}
Note : witin a try block if there is no chnace in rising a exception then we can't
right catch block for that exception otherwise we will compile time error saying :
Exception XXX is never thrown in body of corresponding try statement
Note but above mentioned rule is applicale only for fullychecked exceptions
................................................................
Customized Exception :Sometimes to meet the programming requirmnets we can define
our own exceptions such types of exceptions are called customized or user defined
exceptions
Example :
class TooOldException extends RuntimeException
{
TooOldException(String s)
{
super(s); //to make description available to default exception handler
}
}
throw keyword is best suitable for user defined exception but not for pre-defined
exceptions
Note : Highly recomended to define customized as unchecked that is we have to
extends RuntimeException but not Exception
Note : super() we use this because to pass string to parent throwable class where
printstacktrace method is present
...................................................................................
........
Top 10 Exceptions :-based on person who is rising an exception all exceptions are
divided into 2 types
1)JVM Exceptions :- the exceptions which are rised automatically by JVM whenever a
particular event occurs are called JVM exception e.g AE,NPE,etc
7)Illegal
...................................................