Advertisement Jump to content
Sign in to follow this  
thegame

[java] Java Exception Handling

This topic is 5198 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
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
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!