• 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
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

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
 
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic