• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Exceptions

 
challa
Greenhorn
Posts: 23
  • 0
  • Mark post as helpful
  • send pies
  • 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);
}
}

This output of the above program is 0,1,1,0.

can anyone explain how the program works.
 
Mathangi Shankar
Ranch Hand
Posts: 56
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

In the first try block the method m1 is called since the method body throws WhiteException which is a subclass of ColorException it is caught in the corresponding catch block and 'b' is incremented to 1.

In the second try block when the method m2 is called though the exception is declared in the throws clause the method body does not throw any exception hence the next line is executed.

Therefore d is incremented to 1.

Since all the exceptions are handled properly the println statement is executed outputting 0,1,1,0 as a=0,b=1,d=1,f=0


Hope you understood the flow.

Mathangi....
 
Joe Sondow
Ranch Hand
Posts: 195
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kalyani, if you look closely you'll see that m2() does not throw an exception. It only declares that it *could* throw an exception, but it never does, which is why d gets incremented and f does not.
 
challa
Greenhorn
Posts: 23
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shankar,
I understood ur answer.I have a small doubt in the i think a++ is never reached because m1() throws exception explictly then why doesn't the compiler show error.

Please any one make clear this point.
 
vidya sagar
Ranch Hand
Posts: 580
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
---------------------------------------------------------------------
i think a++ is never reached because m1() throws exception explictly then why doesn't the compiler show error.
--------------------------------------------------------------------

Hi Purnima

According to JLS... Compiler simply checks whether code in current method is reachable or not reachable.It doesnot checks for calling method,it simply have it in a mind that it returns sucessfully
 
Mathangi Shankar
Ranch Hand
Posts: 56
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi purnima,

The compiler doen't give error because a++ is a reachable statement. The reason being the whiteexception thrown in the method m1 is declared in the throws clause of the method

So in a way it is directly being absorbed hence the next line is executed.

The exception thrown is carried till a corresponding catch block is found.

Note:You cannot catch a WhiteException following the ColorException the compiler will throw error.
 
Saurabh Khanna
Ranch Hand
Posts: 30
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
a++ is not reached as white.m1() throws an exception, exception is catched and b++ is executed.

white.m2() does'nt throw any exception and d++ is executed, leaving the catch block alone.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic