Results 1 to 4 of 4
  1. #1
    SindhuPal is offline Member
    Join Date
    Oct 2012
    Posts
    2
    Rep Power
    0

    Default 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.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,825
    Rep Power
    19

    Default 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.

    ** This space for rent **

  3. #3
    SindhuPal is offline Member
    Join Date
    Oct 2012
    Posts
    2
    Rep Power
    0

    Default 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.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,825
    Rep Power
    19

    Default 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.
    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
    }
    Obviously the above is not Java, but that's the logic I would use.

    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.

    ** This space for rent **

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •