I'm using Android SAX parser which allows to bind listeners to EndTextElementListener event, however the listeners provided by Android don't allows us to throw exceptions, so for example how do i stop the parser during the process ?

Some code:

item.getChild("child").setEndTextElementListener( new EndTextElementListener() {
        @Override
        public void end(String s) {

            if( expression )
                throw new CustomException(); //Can't do this because end method don't throw SaxExceptions

        }
    });

CustomException extends SaxException by the way. But the listeners don't throw SaxExceptions so is there any way of doing it ? Or switching to a pull parser is the only option ?

有帮助吗?

解决方案

If you don't care about exactly what type of exception is thrown, then throw a sub-class of RuntimeException. The compiler will allow this.

if( expression )
    throw new MyCustomException();
}

where:

class MyCustomException extends RuntimeException ...
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top