Results 1 to 4 of 4
Thread: ErrorHandling in Java
- 10-16-2012, 11:02 AM #1
Member
- Join Date
- Oct 2012
- Posts
- 2
- Rep Power
- 0
ErrorHandling in Java
Hi All,
I have written a code where I am generating an XML file with FlatFile Data. The input FlatFile contains many records. (The data is converted into a Single XML with set of records). But, if any of the flatfile records contains errors, immediately the process of execution is getting stopped and I am getting an exception.
Now, I need to handle those exceptions and continue the execution process for furthur records Or Else
I should be able to display the particular record data along with the exception for which the exception is occured.
Could anyone please guide me to achive the above said scenarios.
Thanks in Advance,
Regards,
Sindhu.
- 10-16-2012, 12:37 PM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Re: ErrorHandling in Java
Handle the exception then.
Presumably this is in a loop where each loop represents a record in the flat file, so just handle the exception inside the loop rather than letting it escape out the top of your application. You'll want to store the raw failed record somewhere for later retrieval, possibly simply in another flat file of errors.Please do not ask for code as refusal often offends.
- 10-16-2012, 02:05 PM #3
Member
- Join Date
- Oct 2012
- Posts
- 2
- Rep Power
- 0
Re: ErrorHandling in Java
I am reading the flatfile data using readLine method. For each line am reading, I need to parse the line and put the data under XML Elements. I am creating a XML using DOM. As you said, if error occurs in any one of the records I need to store whole particular record into some other file. So, If I get any exception at the end of the record or any where in the middle of processing the record how can I stop generating XML structure for particular record for which I am getting the error. Could you please give me an idea.
- 10-16-2012, 02:25 PM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Re: ErrorHandling in Java
Depends what your code looks like I suppose.
If you have a method that takes a line of text and processes it into an XML element, then I would have that throw an exception if there's a problem.
The calling code would add that element to the Document, but if an exception is thrown then it won't.
Obviously the above is not Java, but that's the logic I would use.Java Code:while (lines to process) { try { xmlDocument.addElement(processLine()); } catch (SomeException ex) { } } private Element processLine(theLine) throws SomeException { // create the element. // if there's a problem throw SomeException }
ETA: Obviously inside the catch block is where you would do whatever you need to do with the failed data.Please do not ask for code as refusal often offends.


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks