File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes which catch start first ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "which catch start first ?" Watch "which catch start first ?" New topic
Author

which catch start first ?

Nakata kokuyo
Ranch Hand

Joined: Apr 13, 2005
Posts: 442
hi, when expection happen in try block, the compiler will check first catch first right ? if first catch happen ...means second catch will not check ? thank you

Georgy Bolyuba
Ranch Hand

Joined: Feb 18, 2005
Posts: 162
Yes you are right.

Note:


This will not compile cause the first catch is "wider" than the second catch is.
[ August 02, 2005: Message edited by: George Bolyuba ]

SCJP 1.4 (100%) Done.<br />SCJD (URLyBird 1.2.3 Started)
Alangudi Balaji Navaneethan
Ranch Hand

Joined: Apr 28, 2004
Posts: 42
Originally posted by George Bolyuba:


In your code, catches catch(Exception e) should come after catch(IOException ioe); otherwise the compiler will give error message like, IOException is already caught.

And we can also inlcude finally block after all catches. Whether an error is caught or not the code inside finally will run.

try this out
[CODE]
public class Test
{
public static void main(String args[])
{
try
{
throw new Exception();
}
catch(IOException ioe)
{
System.out.println("IOException");
}
catch(Exception e)
{
System.out.println("Exception");
}
finally
{
System.out.println("Finally");
}
}
}
[\CODE]

This will not compile cause the first catch is "wider" than the second catch is.

[ August 02, 2005: Message edited by: George Bolyuba ][/QB]


if you think you can you r right<br />if you think you can not you r double right
Alangudi Balaji Navaneethan
Ranch Hand

Joined: Apr 28, 2004
Posts: 42
Originally posted by Alangudi Balaji Navaneethan:
[QB]

In your code, catches catch(Exception e) should come after catch(IOException ioe); otherwise the compiler will give error message like, IOException is already caught.

And we can also inlcude finally block after all catches. Whether an error is caught or not the code inside finally will run.

try this out
[CODE]
public class Test
{
public static void main(String args[])
{
try
{
throw new Exception();
}
catch(IOException ioe)
{
System.out.println("IOException");
}
catch(Exception e)
{
System.out.println("Exception");
}
finally
{
System.out.println("Finally");
}
}
}
[\CODE]
Dharmesh Gangani
Ranch Hand

Joined: Feb 20, 2004
Posts: 30
Hi,

The catch(IOException ioe) should come first and then the catch(Exception e) should follow. Otherwise, it will give a compilation error.

The catch with the narrowest Exception class should appear first in the code.


-=-=-=-=-=-=-=-=-=-<br />Thanks & Regards,<br />Dharmesh G.
Alangudi Balaji Navaneethan
Ranch Hand

Joined: Apr 28, 2004
Posts: 42
Originally posted by Dharmesh Gangani:
Hi,

The catch(IOException ioe) should come first and then the catch(Exception e) should follow. Otherwise, it will give a compilation error.

The catch with the narrowest Exception class should appear first in the code.


That's what I did in my sample code.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: which catch start first ?