• 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
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Exception- Doubt Help Please....

 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class ColorException extends Exception {}

class WhiteException extends ColorException {}

class White
{
void m1() throws ColorException {throw new WhiteException();}
void m2() throws WhiteException {}

public static void main (String[] args)
{
White white = new White();
int a,b,d,f; a = b = d = f = 0;

try
{
white.m1();
a++;
}
catch (ColorException e)
{
b++;
}

try
{
white.m2();
d++;
}
catch (WhiteException e)
{
f++;
}
System.out.print(a+","+b+","+d+","+f);
}
}

I think the Answer willbe 0101 But the Given answer is 0,1,1,0.

Can anybody explain please.....

With Regards,
Deepak
 
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why do you think m2 is required to throw a WhiteException?

The method m2 doesn't actually throw the WhiteException, so it just returns normally. The throws keyword only says "Look out! I could throw this Exception if things go wrong!" It doesn't mean that the Exception WILL be thrown, only that it CAN be.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic