This discussion is archived
4 Replies Latest reply: Feb 9, 2013 10:54 AM by 939520 RSS

How to get exact error message from print stacktrace?

shivasha Newbie
Currently Being Moderated
Hi,
While throwing Unique Contraint Exception, getMessage() throws entire statck trace like "insert into <table name> values(<column names>) schemaname.constraintname constarint violation exception

I need to get schemaname.constraintname constarint violation exception alone.

How to extract the message?
  • 1. Re: How to get exact error message from print stacktrace?
    800411 Newbie
    Currently Being Moderated
    shivasha wrote:
    Hi,
    While throwing Unique Contraint Exception, getMessage() throws entire statck trace like "insert into <table name> values(<column names>) schemaname.constraintname constarint violation exception

    I need to get schemaname.constraintname constarint violation exception alone.

    How to extract the message?
    Java doesn't have Unique Constraint Exception. That is database exception

    Maybe you should try getCause() instead of getMsg()

    Edited by: mycoffee on Feb 1, 2013 10:11 AM
  • 2. Re: How to get exact error message from print stacktrace?
    TPD-Opitz-Consulting-com Expert
    Currently Being Moderated
    shivasha wrote:
    I need to get schemaname.constraintname constarint violation exception alone.
    You have to extract it yourself by prsing the message.

    You could either use <tt>String.substring()</tt> and <tt>String.indexOf()</tt>
    or <tt>Pattern</tt> and <tt>Matcher</tt>.

    bye
    TPD
  • 3. Re: How to get exact error message from print stacktrace?
    aksarben Journeyer
    Currently Being Moderated
    Though parsing the reply will work, be warned: Parsing error messages is fragile, and almost always a bad idea for a long term solution.

    If you just need a quick debugging solution, fine. But don't rely on the approach for very long. Error messages have a nasty habit of changing over time & software versions. Parsing that works today may break next week, next month, or next year because the message has changed.
  • 4. Re: How to get exact error message from print stacktrace?
    939520 Explorer
    Currently Being Moderated
    Here's an idea I was toying with a while ago:

    Get all the constraint names, their expression, and description (if any) from the database's metaData when the application starts up, then copy it to a database table for easy lookup.

    When an exception is thrown, send the stack trace to the log file along with the sql statement that caused it and the primary key values used in the sql.
    Then if the stack trace has a constraint violation in it, the developer can quickly look up that constraint in the database table to help him quickly understand what went wrong.

    If you want to, I suppose your parser can look for those constraint names that are in the database table to parse through the stack trace so the developer doesn't have to manually look it up. Note the constraint names should be named uniquely enough that they will not appear in the stack trace other words.

    My question is, when an SQL exception is thrown, how often is it a constraint violation? If not too often, it may not be worth the time to add such a new feature.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points