Beruflich Dokumente
Kultur Dokumente
Exception
Handling
Explain
about
Exception
Handling
with
an
example.
Exception Handling helps us to recover from an unexpected situations File not found or network
connection is down. The important part in exception handling is the try catch block. Look at the example
below.
public
static
void
main(String[]
args)
{
method1();
System.out.println("Line
after
Exception
-
Main");
}
private
static
void
method1()
{
method2();
System.out.println("Line
after
Exception
-
Method
1");
}
private
static
void
method2()
{
try
{
String
str
=
null;
str.toString();
System.out.println("Line
after
Exception
-
Method
2");
}
catch
(Exception
e)
{
//
NOT
PRINTING
EXCEPTION
TRACE-
BAD
PRACTICE
System.out.println("Exception
Handled
-
Method
2");
}
}
Program
Output
Exception Handled - Method 2
Line after Exception - Method 1
Line after Exception - Main
When exception is handled in a method, the calling methods will not need worry about that exception.
Since Exception Handling is added in the method method2, the exception did not propogate to method1
i.e. method1 does not know about the exception in method2.
Few important things to remember from this example.
If exception is handled, it does not propogate further.
In a try block, the lines after the line throwing the exception are not executed.
In
what
kind
of
scenarios,
a
finally
block
is
not
executed?
Code in finally is NOT executed only in two situations.
1. If exception is thrown in finally.
2. If JVM Crashes in between (for example, System.exit()).
Is
a
finally
block
executed
even
when
there
is
a
return
statement
in
the
try
block?
private
static
void
method2()
{
Connection
connection
=
new
Connection();
connection.open();
try
{
//
LOGIC
However
a
try
block
without
both
catch
and
finally
is
NOT
allowed.
Below method would give a Compilation Error!! (End of try block)
private
static
void
method2()
{
Connection
connection
=
new
Connection();
connection.open();
try
{
//
LOGIC
String
str
=
null;
str.toString();
}//COMPILER
ERROR!!
}
class
AmountAdder
{
static
Amount
addAmounts(Amount
amount1,
Amount
amount2)
{
if
(!amount1.currency.equals(amount2.currency))
{
throw
new
Exception("Currencies
don't
match");//
COMPILER
ERROR!
//
Unhandled
exception
type
Exception
}
return
new
Amount(amount1.currency,
amount1.amount
+
amount2.amount);
}
}
Example
class
CurrenciesDoNotMatchException
extends
Exception{
}
Lets now create some sample code to use CurrenciesDoNotMatchException. Since it is a checked
exception we need do two things a. throw
new
CurrenciesDoNotMatchException();
b.
throws
CurrenciesDoNotMatchException
(in
method
declaration).
class
AmountAdder
{
static
Amount
addAmounts(Amount
amount1,
Amount
amount2)
throws
CurrenciesDoNotMatchException
{
if
(!amount1.currency.equals(amount2.currency))
{
throw
new
CurrenciesDoNotMatchException();
}
return
new
Amount(amount1.currency,
amount1.amount
+
amount2.amount);
}
}
Videos
We
have
created
more
than
20
videos
to
help
you
understand
these
topics
and
become
an
expert
at
them.
You
can
watch
these
videos
for
free
on
YouTube.
Visit
our
website
http://www.JavaInterview.in
for
complete
list
of
videos.
We
answer
the
top
200
frequently
asked
interview
questions
on
the
website.
Register
here
for
more
updates
:
https://feedburner.google.com/fb/a/mailverify?uri=RithusTutorials
Java
Interview
:
A
Freshers
Guide
-
Part
1:
https://www.youtube.com/watch?v=njZ48YVkei0
Java
Interview
:
A
Freshers
Guide
-
Part
2:
https://www.youtube.com/watch?v=xyXuo0y-xoU
Java
Interview
:
A
Guide
for
Experienced:
https://www.youtube.com/watch?v=0xcgzUdTO5M
Collections
Interview
Questions
1:
https://www.youtube.com/watch?v=GnR4hCvEIJQ
Collections
Interview
Questions
2:
https://www.youtube.com/watch?v=6dKGpOKAQqs
Collections
Interview
Questions
3:
https://www.youtube.com/watch?v=_JTIYhnLemA
Collections
Interview
Questions
4:
https://www.youtube.com/watch?v=ZNhT_Z8_q9s
Collections
Interview
Questions
5:
https://www.youtube.com/watch?v=W5c8uXi4qTw