• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Why it does not cimpile without finally ?

 
Ranch Hand
Posts: 348
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Live ordinary life in an extraordinary way. Details embedded in this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic