• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • paul wheaton
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Tim Holloway
  • Carey Brown
  • salvin francis

Why it does not cimpile without finally ?

 
Ranch Hand
Posts: 348
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could someone explain why the code does not compile {error mesg : missing return tatement !!} until the finally clause with "return 0" is added ?
thanks
public class Test_9_3_02_2245 {
public static int testX(int i) {
try { exGenerator(i);
}
catch (ArithmeticException aex) {
System.out.println("Arith : "+aex);
return 1;
}
catch ( NullPointerException nex) {
System.out.println("Null : "+ nex);
return 2;
}
catch (RuntimeException rex) {
System.out.println("Runtime : "+ rex);
return 3;
}
//finally {System.out.println("Finally"); return 0; } }
static void exGenerator(int exType) {
switch(exType){
case 1 : throw new NullPointerException();
case 2 : throw new ArithmeticException();
case 3 : throw new RuntimeException();
default: throw new ArrayIndexOutOfBoundsException();
}
}
public static void main(String[] args) {
if(args.length!=1) { System.out.println(testX(5));}
if (args.length==1) {
int extype = Integer.parseInt(args[0]);
System.out.println(testX(extype));
}
}
}
 
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your method is declared to return an int, so all possible exit points from the method have to return an int.
You do not need to have a finally clause, but if you don't have one, the last statement of the method needs to return an int.
I don't think it's good style to use a return statement in the finally clause, because it will silently override the return statements in your exception-handling code.
[ September 04, 2002: Message edited by: Ron Newman ]
 
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need the finally block because the method testX is declared to return an int. Each of the catch blocks returns an int and so does the finally block, but the actual body of the method itself doesn't. So, if you compile it without the finally block the compiler will complain about the method signature.
 
Ron Newman
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Run that with an argument of 1, 2, or 3, and you may be very surprised by the return value printed out!
 
crispy bacon. crispy tiny ad:
Enterprise-grade Excel API for Java
https://products.aspose.com/cells/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!