aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes try/catch?? 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 "try/catch??" Watch "try/catch??" New topic
Author

try/catch??

sonir shah
Ranch Hand

Joined: Nov 01, 2001
Posts: 435
public class TestClass
{
public static void main(String args[])
{
try{
m1();
}catch(IndexOutOfBoundsException e){
System.out.println("1");
throw new NullPointerException();
}catch(NullPointerException e){
System.out.println("2");
return;
}catch (Exception e) {
System.out.println("3");
}finally{
System.out.println("4");
}
System.out.println("END");
}
// IndexOutOfBoundsException is a subclass of RuntimeException.
static void m1()
{
System.out.println("m1 Starts");
throw new IndexOutOfBoundsException( "Big Bang " );
}
}

After running the program it prints : "m1Starts","1", and "4" in that order
Why?Need explainations.
Rex Rock
Ranch Hand

Joined: Jun 25, 2001
Posts: 82
Sonir, for the benefit of you and other ranchers, start using the [ CODE ] and [ / CODE ] tags.
Before posting code, press the CODE button that you can see next to the emoticons, and then place your those two tags.
Posting a question and asking "why???", doesn't make any sense. You wont learn anything by it. Write your explanation, and what you think is the answer. If you don't agree with the answer given in the book, you tell us "why?" you don't agree. This would lead to much useful discussion., and you will find more people participating.
And for now, I'm not telling you "why?" untill you give your explanation!!!

Rashmi Tambe
Ranch Hand

Joined: Aug 07, 2001
Posts: 418
Originally posted by sonir shah:
public class TestClass
{
public static void main(String args[])
{
try{
m1();
}catch(IndexOutOfBoundsException e){
System.out.println("1");
throw new NullPointerException();
}catch(NullPointerException e){
System.out.println("2");
return;
}catch (Exception e) {
System.out.println("3");
}finally{
System.out.println("4");
}
System.out.println("END");
}
// IndexOutOfBoundsException is a subclass of RuntimeException.
static void m1()
{
System.out.println("m1 Starts");
throw new IndexOutOfBoundsException( "Big Bang " );
}
}

After running the program it prints : "m1Starts","1", and "4" in that order
Why?Need explainations.

Hi sohir,
initially m1() is called and "m1starts" is printed . Then the Exception is thrown. control comes to catch block that catches IndexOutOfBoundsException and "1" is printed.
Now NullPointerException is thrown which is not handled in the catch block
Remember that catch (NullPointerException e) block is related to try block and not to exception thrown in previous catch.
Therefore method teminates abnormally (before that finally is always executed so "4" is printed) control goes back to caller ie. main.
Therefore the o/p.
HIH
Rashmi
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: try/catch??