MJ
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;
}
try{
Iterator irankedrows = getRankingListIterator()!=
}catch (Exception e) {
Iterator irankedrows = null;
}
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.
ASKER
SO what do I need to do???
Iterator irankedrows ;
try{
irankedrows = getRankingListIterator()!= null ?getRankingListIterator(): null;
}catch (Exception e) {
irankedrows = null;
}
Iterator irankedrows ;
try{
irankedrows = getRankingListIterator()!=
}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
}
that also is creating two iterator unnecessarily (though only one gets used)
better off with something like:
irankedrows = getRankingListIterator();
if (irankedrow!= null)
{
// iterate
}
ASKER
Can't do that because something in the code is throwing an exception!
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;
}
}
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.