wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Runtime exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Runtime exception" Watch "Runtime exception" New topic
Author

Runtime exception

Arjun Shastry
Ranch Hand

Joined: Mar 13, 2003
Posts: 1874
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


MH
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

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 ?


[My Blog]
All roads lead to JavaRanch
Jaydeep Mazumdar
Greenhorn

Joined: Aug 26, 2009
Posts: 23
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

Joined: Mar 13, 2003
Posts: 1874
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

Joined: Aug 26, 2009
Posts: 23
Correct! That would work!

I was suggesting this:

Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16


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

Joined: Sep 29, 2008
Posts: 12617

Or use a real web service library with a real parser.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Runtime exception
 
Similar Threads
Code review
Axis2 - Unable to engage rampart
change result xml element name
class loading issue in Jboss
Unable to parse setupCmdLine: null\bin\setupCmdLine.bat