• Post Reply Bookmark Topic Watch Topic
  • New Topic

Complie_time_error  RSS feed

 
Sabber bhatia
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • 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???
 
Harshit Rastogi
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • 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
 
Veeresh Hosur
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • 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
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!