Link to home
Start Free TrialLog in
Avatar of MJ
MJFlag for United States of America

asked on

Try catch on iterator

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;
      }
Avatar of Mick Barry
Mick Barry
Flag of Australia image

you don't actually need to as the variable is not visible outside the try/catch as it is declared inside the try
ie. it is out of scope outside the try block.
Avatar of MJ

ASKER

SO what do I need to do???
Iterator irankedrows ;
try{
         irankedrows  =     getRankingListIterator()!= null ?getRankingListIterator():null;
     }catch (Exception e) {
         irankedrows = null;
     }
why exactly do you want to set it to null, I see no reason.

> 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
}

Avatar of MJ

ASKER

Can't do that because something in the code is throwing an exception!
Avatar of MJ

ASKER

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.
> 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;
   }
}
ASKER CERTIFIED SOLUTION
Avatar of somasekhar
somasekhar

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial