GeeCON Prague 2014*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Exceptions 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 "Exceptions" Watch "Exceptions" New topic
Author

Exceptions

challa
Greenhorn

Joined: Mar 28, 2005
Posts: 23
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

Joined: Nov 01, 2004
Posts: 56
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....


--------------------------------<br />SCJP1.4
Joe Sondow
Ranch Hand

Joined: Apr 10, 2005
Posts: 195
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.


SCJA 1.0 (98%), SCJP 1.4 (98%)
challa
Greenhorn

Joined: Mar 28, 2005
Posts: 23
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

Joined: Mar 02, 2005
Posts: 580
---------------------------------------------------------------------
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

Joined: Nov 01, 2004
Posts: 56
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

Joined: Apr 03, 2005
Posts: 30
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.
 
GeeCON Prague 2014
 
subject: Exceptions