This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Exception Handing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Exception Handing" Watch "Exception Handing" New topic

Exception Handing

Ma JianHui

Joined: Mar 01, 2008
Posts: 6
public class Plane {
static String s = "-";

public static void main(String[] args) {
new Plane().s1();

void s1() {
}catch(Exception e) {
s +="c";

void s2()throws Exception {
s3(); s +="2";
s3(); s +="2b";

void s3() throws Exception {
throw new Exception();


The output is :"-c",what is going on ?
Please help me!Thank you!
David O'Meara

Joined: Mar 06, 2001
Posts: 13459

"king king",
Welcome to the JavaRanch.

We're a friendly group, but we do require members to have valid display names.

Display names must be two words: your first name, a space, then your last name. Fictitious names are not allowed.

Please edit your profile and correct your display name since accounts with invalid display names get deleted.
Anubhav Anand
Ranch Hand

Joined: May 18, 2007
Posts: 341

The output is :"-c",what is going on ?

Ideally you should have debugged to checkthe flow, but, still.
When in the main method you say then the control gets to the s1() method of class Plane. Now s1 in turn calls s2() which calls s3(). But, as both S2 and s3 don't do exception handling and just throw the exception the exception is finally handled in the catch block of method s1()

Now, since String s is a member variable and is initially declared as "-". Thus in the catch block it becomes

So, here comes the output "-c".

Hope, that solves your query.
Kaydell Leavitt
Ranch Hand

Joined: Nov 18, 2006
Posts: 689

Welcome to the Java Ranch,

What is happening is that

1. The main() method is the entry point.
2. The main() method creates an object of type Plane.
3. Immediately, the method s1() is called.
4. s1() calls s2() within a try block to catch any exceptions that might be thrown.
5. s2() calls s3().
6. s3() throws an exception. Control is thrown back to s2().
7. In s2(), control goes back to the first call of s3().
8. s2() doesn't catch the exception, it immediately stops executing and the exception is thrown back to s1().
9. The exception is caught in s1() in the "catch" block. Here, the exception is handled by appending the string "c" to the String variable s which has a value of "-", thus s is assigned a value of "-c".
10. s1() has caught the exception so it does not throw it.
11. s1() terminates normally and control goes back to main() at the point that s1() was called.
12. The next line in main() is executed and the value of the variable s, "-c" is output.

If you don't understand, I can make an easier example to teach you how exceptions are thrown and are caught.
[ March 05, 2008: Message edited by: Kaydell Leavitt ]
I agree. Here's the link:
subject: Exception Handing
It's not a secret anymore!