Exception Handling Java
Exception Handling Java
1)Introduction
4)Exception Hierarchy
9)Finally block
14)Throw keyword
15)Throws keyword
19)Top-10 exceptions
2.Multi-catch block
1)Introduction
Unwanted and unexpected event disturb the normal flow of the programme is called Exception
4)Exception Hierarchy
2)Error are not caused by our programme and it’s not recoverable, its due to lack of system
resources
3)Under exceptions they are several exceptions classes there 3.1) runtime exception 3.2) IO
exception 3.3) Interrupted exception 4) Remote Exception
3.1.5.1) NumberFormatexception
2.1) VM error
2.1.1) StackoutflowError
2.4) ExceptionIntializeError
Where exceptions checked by compiler weather programmer handling or not for smooth execution
those exceptions are called checked exceptions.
Where exceptions not checked by compiler weather programmer handling or not these are un-
checked exceptions
Note: Where exceptions occur in compile time those are syntactical errors
1) e.printstackTrace()
Out put will be :Name of the exception, Description and stack trace
2) e.toString() or e
sop(e) or sop(e.toString)
Output will be: Name of exception and description
3) e. getmessage ()
sop(e.getmessage());
output will be Only description
9)Finally block
Will execute this block irrespective of Weather exceptions handled or not handled and raised or
not raised
If return statement is there in try block, even though finally block will execute, first finally block
will execute after that only return statement will execute
If try block returns some value and catch block returns some value and finally block return some
value, Always finally block return will be consider ,its always highest priority
Whenever system. exit(0) in code JVM not able to execute finally block ,it’s the only chance not to
execute finally block ,system.exit(0) dominates
if we declare class as final, that class is not able extend to any class it means not applicable for
inheritance
finalize () is a method and associated with carbage collector and calls to clean up activity
Garbage collector calls the finalize () method just before destroying the non ref object
14)Throw keyword
Programmer Create exception explicitly and handover to exception object to JVM manually via
Throw keyword
Ex: Throw new Arthimeticexception (“/ zero”); mostly for unchecked exception
15)Throws keyword
Its required only for checked exceptions ,for unchecked exception there is no use
We can use throws keyword applicable for methods and constructors not for classes
++++++++++++++++++++++++++++++++++++++++++++++++++++++