0

When i do DOM XML Schema validation i get this error when there is a syntax error in my deliberately-incorrect test file.

[Fatal Error] :10:3: The element type "gizmos" must be terminated by the matching end-tag "</gizmos>".

However, I want to catch this so that it wouldn't give the red fatal error warning, instead a message that says something like "your xml is not valid." Is it possible?

Thanks,

Umut
  • 359
  • 3
  • 5
  • 17

4 Answers4

2
I believe you can catch Throwable  to handle your scenario

     try{
     ....
     ....
     ....
     }catch (Exception e) {
      e.printStackTrace();
     }catch (Throwable t) {
      t.printStackTrace();
    } 
upog
  • 4,309
  • 6
  • 30
  • 59
1

Errors can not be catched, only Exceptions can be catched.

According to Java Docs :

An Error is a subclass of Throwable that indicates serious problems that a reasonable application should not try to catch. Most such errors are abnormal conditions. The ThreadDeath error, though a "normal" condition, is also a subclass of Error because most applications should not try to catch it. A method is not required to declare in its throws clause any subclasses of Error that might be thrown during the execution of the method but not caught, since these errors are abnormal conditions that should never occur. That is, Error and its subclasses are regarded as unchecked exceptions for the purposes of compile-time checking of exceptions.

But still, you can catch Throwable to catch any error. That is not recommended though.

catch (Throwable e) {
  //e.printStackTrace();
}
codingenious
  • 7,785
  • 7
  • 55
  • 86
1

You can catch a Throwable (Error extends Throwable):

try {
       //...
} catch(Throwable e) {
        //...
}

Please note that it's often recommended not to do it. You can read more about it here: When to catch java.lang.Error?.

Community
  • 1
  • 1
Aneta Stępień
  • 654
  • 5
  • 17
1

If you are using the JAXP validation API then you can nominate an ErrorHandler to receive notification of errors. With this you can change what messages are displayed and where they are displayed.

Michael Kay
  • 138,236
  • 10
  • 76
  • 143