• Advertisement
Sign in to follow this  

[java] Java Exception Handling

This topic is 4838 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I'm having trouble catching an exception. How do you catch RuntimeExcpetions like StringIndexOutOfBounds - I've tried this but it does'nt work.
try
{
    retVal = invokeMethod();
}
catch(Exception1 e)
{
   // handle it
}
catch(StringIndexOurOfBounds e2)
{
   // handle it
}
The problem is that I get a compile error that reVal isint initialized (I dont get that if I remove the second catch block), and the method docuementation says it throws only Exception1. But occasionally I get the RuntimeException::StringIndexOutOfBounds. How do I handle this ?? thanks

Share this post


Link to post
Share on other sites
Advertisement
try setting retVal to null or new String(Assuming it's a String object) where it's defined before the try-catch block

Share this post


Link to post
Share on other sites
Also, you should catch the StringIndexOurOfBounds first, because all exceptions are of type Exception, so it would be caught in the first catch block.

Share this post


Link to post
Share on other sites
well, I think he was using the execption Exception1 as an example, I don't think the 1 was a typo.

Share this post


Link to post
Share on other sites
Touche.

I didn't even notice the 1. So in that case, if the 1 was intentional, disregard what I said (well, only disregard it if you already knew it).

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement