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;
      }
894359Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

objectsCommented:
you don't actually need to as the variable is not visible outside the try/catch as it is declared inside the try
0
objectsCommented:
ie. it is out of scope outside the try block.
0
894359Author Commented:
SO what do I need to do???
Iterator irankedrows ;
try{
         irankedrows  =     getRankingListIterator()!= null ?getRankingListIterator():null;
     }catch (Exception e) {
         irankedrows = null;
     }
0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

objectsCommented:
why exactly do you want to set it to null, I see no reason.

0
objectsCommented:
> 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
894359Author Commented:
Can't do that because something in the code is throwing an exception!
0
894359Author Commented:
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
objectsCommented:
> 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
somasekharCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JSP

From novice to tech pro — start learning today.