[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

Try catch on iterator

Posted on 2004-11-16
9
Medium Priority
?
260 Views
Last Modified: 2012-05-05
If I have an iteraror, how do I assign it a null value if the try fails?????

try{
            Iterator irankedrows  =      getRankingListIterator()!= null ?getRankingListIterator():null;
      }catch (Exception e) {
            Iterator irankedrows = null;
      }
0
Comment
Question by:894359
  • 5
  • 3
9 Comments
 
LVL 92

Expert Comment

by:objects
ID: 12600508
you don't actually need to as the variable is not visible outside the try/catch as it is declared inside the try
0
 
LVL 92

Expert Comment

by:objects
ID: 12600512
ie. it is out of scope outside the try block.
0
 

Author Comment

by:894359
ID: 12600526
SO what do I need to do???
Iterator irankedrows ;
try{
         irankedrows  =     getRankingListIterator()!= null ?getRankingListIterator():null;
     }catch (Exception e) {
         irankedrows = null;
     }
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 92

Expert Comment

by:objects
ID: 12600545
why exactly do you want to set it to null, I see no reason.

0
 
LVL 92

Expert Comment

by:objects
ID: 12600551
> irankedrows  =     getRankingListIterator()!= null ?getRankingListIterator():null;

that also is creating two iterator unnecessarily (though only one gets used)
better off with something like:

irankedrows  = getRankingListIterator();
if (irankedrow!= null)
{
   // iterate
}

0
 

Author Comment

by:894359
ID: 12600735
Can't do that because something in the code is throwing an exception!
0
 

Author Comment

by:894359
ID: 12600738
fyi.. I have to set it to null if it fails because I do some logic on the page and it checks if it is null or not.
0
 
LVL 92

Expert Comment

by:objects
ID: 12600767
> Can't do that because something in the code is throwing an exception!

No reason why you can't, I just left off the try/catch because I was only addressing that particular line

> I have to set it to null if it fails because I do some logic on the page and it checks if it is null or not.

But if you declare it in the scope of the try block you cannot access it outside that scope
Perhaps you should be declaring it outside the scope, though hard to say without seeing your full requirements.

irankedrows  = getRankingListIterator();
if (irankedrow!= null)
{
   try
   {
      // iterate
   }
   catch (Exception ex)
   {
       irankedrows = null;
   }
}
0
 
LVL 2

Accepted Solution

by:
somasekhar earned 2000 total points
ID: 12601951
Hi

Iterator irankedrows = null;

try
   {
        irankedrows  =     getRankingListIterator();
     }catch (Exception e)
    {
         
    }

if getRankingListIterator() method throws some exception it will be left out and "null" will be assigned to irankedrows ;

you can check

if( null != irankedrows )
{

//operation you want to perform
// your code

}



hope this will work
0

Featured Post

Learn to develop an Android App

Want to increase your earning potential in 2018? Pad your resume with app building experience. Learn how with this hands-on course.

Question has a verified solution.

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

The following article sheds light on easy-to-use steps to recover non-responding hard drives without data loss. Count on these approaches to fix undetectable, not responding, or non-working hard drives.
Data security in the cloud is very much like a security in an on-premises data center - only without costs for maintaining facilities and computer hardware.
How to fix display issue, screen flickering issue when I plug in power cord to the machine. Before I start explaining the solution lets check out once the issue how it looks like after I connect the power cord. most of you also have faced this…
Free Data Recovery software is an advanced solution from Kernel Tools to recover data and files such as documents, emails, database, media and pictures, etc. It supports recovery from physical & logical drive after a hard disk crash, accidental/inte…

612 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