• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Complie_time_error

 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class C
{
private String get(String str)
{
try
{
throw new Exception();
return str;
}
catch (Exception e)
{
return null;
}
}
public static void main(String[] args)
{
try
{
System.out.println((new C()).get("C"));
}
catch (Exception e)
{
System.out.println("Exception");
}
}
}
This programm is givin compile time error.
>>>unreachable statement
>>>return str

Why???
 
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
it will because everytime you are going to throw the Exception from the catch block and in no case it can return str.
the correct way could be



this block of code will work fine
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
throw new Exception();
This is the main reason. Basically you are throwing the exception always and you are trying to return the string variable.

Thanks
Veeresh
 
reply
    Bookmark Topic Watch Topic
  • New Topic