Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

try/catch??

 
Ranch Hand
Posts: 435
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!!!

 
Ranch Hand
Posts: 418
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
And then the flying monkeys attacked. My only defense was this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic