File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Regarding Exceptions!" Watch "Regarding Exceptions!" New topic
Author

Regarding Exceptions!

palla sridhar
Ranch Hand

Joined: Oct 15, 2007
Posts: 111
Hey!
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)
System.out.println(df.format(d1));
} catch (Exception ex) {
ex.printStackTrace();
}
}
}


Here 1 produces an IllegalArgumentException and stops there.

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


Thanks and Regards,
[url]www.techlikes.com[/url]
*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!!
 
wood burning stoves
 
subject: Regarding Exceptions!
 
Similar Threads
About DateFormat from K&B
DateFormat
doubt in parsing
DateFormat class
Date formatting