Exception Handling Topics
1)Introduction
2)Run time stack mechanism
3)Default Exception handling in java
4)Exception Hierarchy
5)Customised exception handling by using
6)Control flow in try-catch
7)Methods to print exception info
8)Try with multi catch blocks
9)Finally block
10)Difference between final, finally, finalize
11)Various possible combination
12)Control flow in try catch finally
13)Control flow in nested try catch finally
14)Throw keyword
15)Throws keyword
16)Exception handling keywords summary
17)Various possible compile time errors in exception handling
18)Customized or user denied exceptions
19)Top-10 exceptions
20)1.7 versions enhancements
1.Try with resources
2.Multi-catch block
1)Introduction
Unwanted and unexpected event disturb the normal flow of the programme is called Exception
Purpose of Exception Handling: -Graceful termination of the programme
Meaning of Exception Handling-Defining alternative rest of the programme run normally
2)Run time stack mechanism
In Every thread JVM create separate STACK a
Each entry is called Activation Record or Stack Frame
3)Default Exception handling in java
4)Exception Hierarchy
Throwable class is root class of java exception Hierarchy & error
Throwable contains two child classes 1) exception 2) error
1)Exception are caused by our programme and its recoverable
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
Under runtime exception
3.1.1) Arithmetic exception
3.1.2) Null pointer exception
3.1.3) Class cast exception
3.1.4) Index outofbound exception
3.1.4.1) Array Indexoutofbound exception
3.1.4.2) String Indexoutofbound exception
3.1.5) Illegal Argument exception
3.1.5.1) NumberFormatexception
3.2) Under IOException (Checked exception)(fully checked exception)
3.2.1) End of file exception
3.2.2) FileNotFound exception
2)Error are there may child classes are available(unchecked)
2.1) VM error
2.1.1) StackoutflowError
2.1.2) OutOfMemory error
2.2) Linkage Error
2.2.1) Verify Error
2.3) Assertion Error
2.4) ExceptionIntializeError
Checked vs Unchecked Exception
Weather Checked or Unchecked exception always occurs runtime only
Where exceptions checked by compiler weather programmer handling or not for smooth execution
those exceptions are called checked exceptions.
Ex: File not found exceptions, interpreted exceptions
Where exceptions not checked by compiler weather programmer handling or not these are un-
checked exceptions
Ex: Arithmetic exception
The exception checked by runtime
All errors are unchecked exceptions
Runtime exceptions and child classes are unchecked exceptions
Note: Where exceptions occur in compile time those are syntactical errors
What is fully checked exceptions and partially checked exception
Throwable and Exception are only partially checked exceptions in java
And IOException are fully checked exceptions
5)Customised exception handling by using try catch
7)Methods to print exception info
There are three methods are available to print messages in console
These methods are available in throwable class
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
8)Try with multi catch blocks
Yes its possible and highly recommended
9)Finally block
Purpose of the finally block to maintain clean up code(resource re-allocation code)
Will execute this block irrespective of Weather exceptions handled or not handled and raised or
not raised
Finally block vs return statement
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
Finally block dominates the return statement
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
10)Difference between final, finally, finalize ()
Final-is access modifier is applicable for classes, methods and variables
if we declare class as final, that class is not able extend to any class it means not applicable for
inheritance
if we method declared as final, we can’t override in the child class
if we variable declared as final we cant change the value
finally- is block and associated with catch block
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
Purpose of Throw keyword for customized exception
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
To handle checked exception by
To delegate responsible of exception handling to the caller(main method or JVM)
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
Its applicable for throwable exception only
1)try-tomaintain risky code
2)catch-to maintain handling code
3)finally-to maintain clean up code
++++++++++++++++++++++++++++++++++++++++++++++++++++++
Interface: Any service requirement specification (srs)
Interface never talks about implementation
Every method by default public or abstract in interface
From 1.9->static and private methods are allowing