Link to home
Start Free TrialLog in
Avatar of sigma19
sigma19Flag for United States of America

asked on

java exception

Hi,

Ia m getting an exception at this line:

String teststris=(String)originalstring.get(0);
=
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
        at java.util.ArrayList.RangeCheck(ArrayList.java:547)
        at java.util.ArrayList.get(ArrayList.java:322)      
=
I have       public static void main(String[] args) throws IOException {
}
===
After this exception I dont want my program to terminate ? how can I do that so that even after the exception the program continues?
Avatar of sigma19
sigma19
Flag of United States of America image

ASKER

I am looking in general how in java we can continue even after a exception ...
If you don't want the program to terminate you need to catch the exception
You may then print the exception and do whatever you want next


try {
//here line which throws exception

}catch(Exception x) {
System.out.println(ex.toString();
}
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

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
> After this exception I dont want my program to terminate ? how can I do that so that even after the exception the program continues?

you don't want to do that, its poor practice
Instead ensure that the exception does not occur. Exceptions are called that for a reason :)

see the code I posted above.

If you catch iut the way I showed the program will not exit

In general it would not exit even if you don't print exception and just put nothing
between the catching braces.

But if you just ignore exception - this is a very bad practice - it ican never be recommeded

Yor exception is probably caused by the facte that your list has no elements at all
Avatar of sigma19

ASKER

Thanks objects, I agree I should not have exceptions but I should be ready incase if some thing happens write....now I am practicing file operations and looks some times when I read junk data I will run to this case?

Looks As Yan is suggesting  I need to use try /catch at all  places I want to capture and continue?
Avatar of sigma19

ASKER

Thanks Yan, than I will use try/catch
> Looks As Yan is suggesting  I need to use try /catch at all  places I want to capture and continue?

no you definitely do *not* want to do that.
try the code I posted it. It is better to stop the exception being thrown
> but I should be ready incase if some thing happens write.

in your case there is nothing that could happen. The exception you are getting is because of a problem with your. You need to fix the problem instead of ignoring the problem. Thats a terrible misuse of exceptions.
Yes, you use try/catch in all places - and compiler will prompt you to do it.
But keep in mind that you want to print the exception, and even it
is very coveninet  to have also
ex.printStackTrace();
inside the catch braces
This will tell you exact line where exception happend,
which very much helps


> Yes, you use try/catch in all places - and compiler will prompt you to do it.

Thats incorrect

> This will tell you exact line where exception happend,
> which very much helps

help doing what exactly?  You're suggesting ignoring any errors in the code
Of course what I'm saying doesn't meant that you shouldn't think about your code and avoid such sityuations which you showed
- say retrivring something from the list which can happen
to be empty. Of course such situation cannot be remedied
by just catching the exception - you should try to avoid it analyzing the logic
of your program and correcting it if they do happen
The change I suggested above will stop the exception *ever* occurring.
A far more sensible approach.
In general, when writing programs there is no reason to be dogmatic.
Instead you should alwys try to be reasonable.
You should normally not write any code which you know may be causing
an exception, say of the type you showed above. You should try to consider all cases and avoid
such situations. Nevertheless, there may be situations, when
you may expect certain kind of exceptions to be happening in some cases.
If you know what is the cause of these exceptions and if you are sure
that they will not affect further function of your program and the only issue
that this casues the program to exit and you don't see simple
way to avoid such cases altogether in other way, then catching and ignoring exception may be also an option.
Java gives all these options for a good reason and to use them with thought and without dogmatic following this or that rule - that is what is in fact the best practice.
A rough rule of thumb to follow is that RuntimeException and its subclasses (your case) should NEVER be caught. It indicates a programming error if it occurs
which is what I already stated above