• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

which catch start first ?

 
Nakata kokuyo
Ranch Hand
Posts: 472
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Alangudi Balaji Navaneethan
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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]
 
Alangudi Balaji Navaneethan
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Alangudi Balaji Navaneethan
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic