sigma19
asked on
java exception
Hi,
Ia m getting an exception at this line:
String teststris=(String)original string.get (0);
=
Exception in thread "main" java.lang.IndexOutOfBounds Exception: Index: 0, Size: 0
at java.util.ArrayList.RangeC heck(Array List.java: 547)
at java.util.ArrayList.get(Ar rayList.ja va: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?
Ia m getting an exception at this line:
String teststris=(String)original
=
Exception in thread "main" java.lang.IndexOutOfBounds
at java.util.ArrayList.RangeC
at java.util.ArrayList.get(Ar
=
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?
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
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.toSt
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
> 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.
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
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?
Looks As Yan is suggesting I need to use try /catch at all places I want to capture and continue?
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
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.
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
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
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
- 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.
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.
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
ASKER