Domanda

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 ?

È stato utile?

Soluzione

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 ...
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top