Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 331
  • Last Modified:

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?
0
vkchaitu82
Asked:
vkchaitu82
  • 8
  • 6
  • 3
  • +1
1 Solution
 
vkchaitu82Author Commented:
I am looking in general how in java we can continue even after a exception ...
0
 
for_yanCommented:
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
0
 
for_yanCommented:


try {
//here line which throws exception

}catch(Exception x) {
System.out.println(ex.toString();
}
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
objectsCommented:
> I have       public static void main(String[] args) throws IOException {

thats got nothing to do with it

> String teststris=(String)originalstring.get(0);

change that to this:

String teststris=null;
if (originalstring.size()>0) {
  teststris = (String)originalstring.get(0);
}
0
 
objectsCommented:
> 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.
0
 
for_yanCommented:

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
0
 
vkchaitu82Author Commented:
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?
0
 
vkchaitu82Author Commented:
Thanks Yan, than I will use try/catch
0
 
objectsCommented:
> 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
0
 
objectsCommented:
> 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.
0
 
for_yanCommented:
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


0
 
objectsCommented:
0
 
objectsCommented:
> 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
0
 
for_yanCommented:
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
0
 
objectsCommented:
The change I suggested above will stop the exception *ever* occurring.
A far more sensible approach.
0
 
for_yanCommented:
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.
0
 
CEHJCommented:
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
0
 
objectsCommented:
which is what I already stated above
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 8
  • 6
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now