Duplicate :
|
|
Relates :
|
|
Relates :
|
|
Relates :
|
Below are three files: TreeInfo.java, TreeScanner.java and Tree.java. If they are compiled with javac p/Tree.java p/TreeScanner.java p/TreeInfo.java the compilation succeeds. If they are compiled with javac p/TreeInfo.java p/TreeScanner.java p/Tree.java you get the following error: p/TreeScanner.java:9: unreported exception java.lang.Throwable; must be caught or declared to be thrown if(tree!=null) tree.accept(this); ^ 1 error ------Tree.java-------------- package p; public abstract class Tree { /** Visit this tree with a given visitor. */ public abstract <E extends Throwable> void accept(Visitor<E> v) throws E; /** A generic visitor class for trees. */ public static abstract class Visitor<E extends Throwable> { public void visitTree(Tree that) throws E { assert false; } } } ------TreeInfo.java---------- package p; import p.Tree.*; public class TreeInfo { public static void declarationFor(final Tree tree) { class DeclScanner extends TreeScanner<Error> { public void scan(Tree tree) { } } DeclScanner s = new DeclScanner(); tree.accept(s); } } ------TreeScanner.java------- package p; import p.Tree.*; public class TreeScanner<E extends Throwable> extends Visitor<E> { /** Visitor method: Scan a single node. */ public void scan(Tree tree) throws E { if(tree!=null) tree.accept(this); } }
|