Two Laptop Bag*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Lock Method Issue Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "Lock Method Issue" Watch "Lock Method Issue" New topic
Author

Lock Method Issue

Daniel Simpson
Ranch Hand

Joined: Sep 02, 2004
Posts: 181
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 ]

SCJP 1.4<br />SCJD 1.4
Dieskun Koper
Ranch Hand

Joined: Aug 15, 2004
Posts: 85
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.
Frans Janssen
Ranch Hand

Joined: Dec 29, 2004
Posts: 357
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.


SCJP 1.4, SCJD
Daniel Simpson
Ranch Hand

Joined: Sep 02, 2004
Posts: 181
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!
Joe Borderi
Ranch Hand

Joined: Oct 23, 2004
Posts: 151
"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

Joined: Sep 02, 2004
Posts: 181
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.
Nikolay Petrov
Greenhorn

Joined: Jan 07, 2005
Posts: 9

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


SCJP 1.2 Done<br />SCJD 1.4 (URLyBird 1.2.1) (In Progress)
Daniel Simpson
Ranch Hand

Joined: Sep 02, 2004
Posts: 181
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

Joined: Dec 29, 2004
Posts: 357
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Lock Method Issue
 
Similar Threads
nx:All of URLy Bird1.1.3 about record lock
Some lock doubts
Beta Question: unlock
Class Cast Exception
Passed SCJD