• 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
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

return type

 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
this is a question from geocities.com/ganesh30/
class NewClass{

static int tomb( char a){
throw new NumberFormatException();
}

public static void main(String args[]){
try
{
tomb('a');
}
catch (Exception e)
{
System.out.println("Done");
}
}
};
even if the tomb function is not returning any thing(it is declared to return integer )there is no compiler error. why?
[ April 23, 2007: Message edited by: Prahlad Joshi ]
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



Prahlad says,
even if the tomb function is not returning any thing(it is declared to return integer )there is no compiler error. why?



Because compiler is smart enough to understand the case when you type anything after the line:



If you put any thing after the throw ..., compiler will complaint that
... line is not reachable.


BTW, Welcome to JavaRanch!

Regards,
cmbhatt
 
Prahlad Joshi
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i am not getting .when a function has a return type it must have to return that type or covarinat one otherwise compiler gives an error.
 
Ranch Hand
Posts: 447
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi
Prahlad Joshi

---------------------------------------------------------------------
when a function has a return type it must have to return that type or covarinat one otherwise compiler gives an error
--------------------------------------------------------------------

it is correct if your code does not contains

throw new NumberFormatException();

the above statement.

If you put any statement after to that ,then you will get compaile time error.

Because at run time after executing the throw statement.The contol will go to the method which call's this method.

The compiler will know that the statements after to throw won't execute.
I think you have understood.
reply
    Bookmark Topic Watch Topic
  • New Topic