Solved

Try/Catch block value accessible

Posted on 2006-07-13
11
216 Views
Last Modified: 2010-03-31
Hi
I have a try/catch block to open a file and get values from it. I try to put an if statement and return False/True on some condition in if statement. When I end the block the method is showing error because I placed the return value statement in incorrect place, that is if statement should be outside try block but then it wont get the values from the try/catch block or would that be ok (confusing..... but the code is below:)

----
CODE

try {
            // Open and read the Feature.properties
            Properties props = new Properties();
            props.load( fis= new FileInputStream (new File ("Feature.properties")) );
           
            if ("Medium".equals(props.getProperty("Strength")) ){

                     return (bcs.getFeatureId());   // will return true or false
            } else {
                      return txtValue; //value is set to false
            }  
        } catch (IOException e){
                  e.printStackTrace();
        }

 -----

This method needs to return a boolean value. Right now because the if is inside try block therefors its not getting the return value, but if I place if outside the try block then it may not get value of "prop". I can not test this class as individual right now, thats why I need help to get the code right::)))
Thank you:-)
0
Comment
Question by:atwork2003
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
  • 3
11 Comments
 
LVL 23

Accepted Solution

by:
Ajay-Singh earned 500 total points
ID: 17103021
> if ("Medium".equals(props.getProperty("Stre<wbr/>ngth")) ){
should be
return "Medium".equals(props.getProperty("Strength"));
0
 

Author Comment

by:atwork2003
ID: 17103067
Thank you for urgent response:)

I am still getting error that no return value is provided. Maybe because the return value  within try/catch block is not considered as return value for the method. Any help:)
0
 
LVL 23

Expert Comment

by:Ajay-Singh
ID: 17103102
try returning false at the end of try/catch block:

try {
...
} catch() {
}

return false;
0
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

Author Comment

by:atwork2003
ID: 17103116
Question: That would work but wouldnt it override the return from try/catch and always return false. Thank you:)
0
 
LVL 23

Expert Comment

by:Ajay-Singh
ID: 17103137
no - as we are returning in from the try block, the outer return will be executed only when there is IOException.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 17103158
>>
else {
                      return txtValue; //value is set to false
            }  
>>

isn't that an odd name for a boolean?
0
 

Author Comment

by:atwork2003
ID: 17103166
Great:) One last thing that return statement that you gave, would it return true/false if the values are not equal, right?
>>return "Medium".equals(props.getProperty("Strength"));
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 17103171
You must keep the reference to the InputStream and make sure it's closed or you could get memory leaks
0
 
LVL 23

Expert Comment

by:Ajay-Singh
ID: 17103189
this statement returns true if the property "Strength" has "Medium" value
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 17103210
You could increase the robustness (unless you've a reason not to) with

return "Medium".equalsIgnoreCase(props.getProperty("Strength"));
0
 

Author Comment

by:atwork2003
ID: 17103317
Thanks a bunch every one. CEHJ thank you. Thanks Ajay for your patience and great solution:)
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
The viewer will learn how to implement Singleton Design Pattern in Java.

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question