Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • 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
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Lock Method Issue

 
Ranch Hand
Posts: 181
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have the following lock method, but I cannot compile it because it says it must return a value long. Here is the code:


It calls the method isRecordValid to test and see if the record in the cache is valid. If it is, it will return true, otherwise it will throw a RecordNotFoundException if the record is deleted or doesn't exist. However, the compiler says it must return a value....how can I fix this problem, because theoretically this code could execute and be at a spot where it is expecting some value to return. Maybe it's just late and I cannot think straight. Thanks for helping.
[ January 07, 2005: Message edited by: Daniel Simpson ]
 
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does it compile if you change your code as follows?


} catch (RecordNotFoundException rnfEx) {
throw new RecordNotFoundException(rnfEx.getMessage());
}
return 0L; // to keep the compiler happy
}



Some compilers expect a non-void method to end with a return statement and don't take into account that in certain circumstances that line can never be reached.
 
Ranch Hand
Posts: 357
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Daniel,

It calls the method isRecordValid to test and see if the record in the cache is valid. If it is, it will return true, otherwise it will throw a RecordNotFoundException if the record is deleted or doesn't exist.



I am assuming that your isRecordValid() method throws the exception if the recNo is not a valid record number (which means that the method either returns true or throws an exception, but never ever returns false).

The compiler is considering the situation where isRecordValid() does return false without throwing an exception (it is not smart enough to tell that this can never happen). In that case the lock() method is exited without an explicit return statement and that is what the compiler does not accept.

Adding an extra return statement at the end as Dieskun suggested will solve your problem. You might want to put an assert false before this return statement to demonstrate that in practice the return statement is never used.

HTH,

Frans.
 
Daniel Simpson
Ranch Hand
Posts: 181
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Frans Janssen:
Hi Daniel,



I am assuming that your isRecordValid() method throws the exception if the recNo is not a valid record number (which means that the method either returns true or throws an exception, but never ever returns false).

The compiler is considering the situation where isRecordValid() does return false without throwing an exception (it is not smart enough to tell that this can never happen). In that case the lock() method is exited without an explicit return statement and that is what the compiler does not accept.

Adding an extra return statement at the end as Dieskun suggested will solve your problem. You might want to put an assert false before this return statement to demonstrate that in practice the return statement is never used.

HTH,

Frans.



Thanks for the help, guys. I, too, was thinking about doing that solution, although I know returning an "error" code or a number that isn't the right number is bad software engineering. Maybe, I'll just add a comment above the line saying that this line will never execute and it only is used to satisfy the compiler. Thanks!
 
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Maybe, I'll just add a comment above the line saying that this line will never execute and it only is used to satisfy the compiler."

Maybe the return at the end of the function should never execute, but what happens if isRecordValid(recNo) returns false?
 
Daniel Simpson
Ranch Hand
Posts: 181
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by J Borderi:
"Maybe, I'll just add a comment above the line saying that this line will never execute and it only is used to satisfy the compiler."

Maybe the return at the end of the function should never execute, but what happens if isRecordValid(recNo) returns false?



My method isRecordValid never returns false. If the record is valid, it will return true. Otherwise, it will just throw a RecordNotFoundException.
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


My method isRecordValid never returns false. If the record is valid, it will return true. Otherwise, it will just throw a RecordNotFoundException.



In that case do not declare it to return boolean
 
Daniel Simpson
Ranch Hand
Posts: 181
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Nikolay Petrov:


In that case do not declare it to return boolean



What would I change it to then? Because such methods as lock, update, delete, unlock rely on this isRecordValid to proceed.
 
Frans Janssen
Ranch Hand
Posts: 357
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Daniel Simpson:


What would I change it to then? Because such methods as lock, update, delete, unlock rely on this isRecordValid to proceed.



You could so something like:



And your lock method would then be:



The if-statements are eliminated and you don't need a dummy return statement.

Frans.
 
Try 100 things. 2 will work out, but you will never know in advance which 2. This tiny ad might be one:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic