Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Runtime exception

 
Arjun Shastry
Ranch Hand
Posts: 1898
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am working on Exception related problem occuring in application. The application java program listens on a port waiting for third party XML message. After receiving the message, program checks for "<" tag and then performs necessary transformation. Program is using String.indexOf() method to do this.If third party does not send message containging "<" then ArrayIndexOutofBoundsException is thrown and thread is killed.
My question is how does program recovers from Runtime Exception?

Thanks
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how does program recovers from Runtime Exception

It doesn't, unless you explicitly catch the exception. But if the exception is thrown by your application, isn't there a way to avoid it to happen ? What is the cause of the exception ?
 
Jaydeep Mazumdar
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A RuntimeException indicates something bad with your programming logic/code. This type of exception can be completely avoided by fixing the code/changing the code logic.

In your particular case, String.indexOf() method returns index of "<" in your message or -1 when it does not find it. I believe you are trying to access the array with this index returned. All you need to do is before accessing the array check that the index value returned is >= 0.

That's all!
 
Arjun Shastry
Ranch Hand
Posts: 1898
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for quick response.
The exception thrown is "java.lang.StringIndexOutOfBoundsException: String index out of range: -1" Are you saying similar following code is required?


 
Jaydeep Mazumdar
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Correct! That would work!

I was suggesting this:

 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Or log the error, or throw a checked exception that you would handle somewhere else. It depends on what you want to do with this unexpected message.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or use a real web service library with a real parser.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic