wood burning stoves 2.0
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Regarding Exceptions! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Regarding Exceptions!" Watch "Regarding Exceptions!" New topic

Regarding Exceptions!

palla sridhar
Ranch Hand

Joined: Oct 15, 2007
Posts: 111
I've a silly doubt regarding Exception.

When we're handling an exception, what will happen after the exception is caught. Will the JVM continue with the next line of code after doing what's in the catch clause or will it stop there doing what's in the catch clasue.

For e.g just look at this code:

import java.text.*;
import java.util.*;

class Dates3 {
public static void main(String[] args) {
Date d1 = new Date(1000000000000L);
try {
DateFormat[] dfa = new DateFormat[6];
dfa[0] = DateFormat.getInstance();
dfa[1] = DateFormat.getDateInstance();
dfa[2] = DateFormat.getDateInstance(DateFormat.SHORT);
dfa[3] = DateFormat.getDateInstance(DateFormat.MEDIUM);
dfa[4] = DateFormat.getDateInstance(DateFormat.LONG);
dfa[5] = DateFormat.getDateInstance(DateFormat.FULL);
dfa[6] = DateFormat.getDateInstance(DateFormat.DAY_OF_WEEK_FIELD); # 1
for (DateFormat df : dfa)
} catch (Exception ex) {

Here 1 produces an IllegalArgumentException and stops there.

Shouldn't the code continue as we're handling the exception ??

Thanks and Regards,
*Nothing is CONSTANT in life, except CHANGE*
Kelvin Chenhao Lim
Ranch Hand

Joined: Oct 20, 2007
Posts: 513
Hi Palla,

When an exception is thrown, control will basically transfer to the first catch clause that matches the exception. After the catch block finishes execution, control will then transfer to the finally block (if present), and then the next statement outside the entire try..catch statement. In other words:

will produce the output "1 3 4 5". As you can see, control does not return to the statement that threw the exception. This is similar to how most other popular OO languages perform exception handling, but you might find this confusing if you're used to a programming language that resumes at the point after the error/exception (e.g. BASIC's "ON ERROR ... RESUME NEXT" construct).

SCJP 5.0
palla sridhar
Ranch Hand

Joined: Oct 15, 2007
Posts: 111
Thanks Kelvin!
You really solved my doubt..
Great response!!
I agree. Here's the link: http://aspose.com/file-tools
subject: Regarding Exceptions!
It's not a secret anymore!