/** * The ast nodes will use this callback to notify us that they throw an exception of type t. This * method will throw a SemanticException if the type t is not allowed to be thrown at this point; * the exception t will be added to the throwsSet of all exception checkers in the stack, up to * (and not including) the exception checker that catches the exception. * * @param t The type of exception that the node throws. * @throws SemanticException */ public void throwsException(Type t, Position pos) throws SemanticException { if (!t.isUncheckedException()) { // go through the stack of catches and see if the exception // is caught. boolean exceptionCaught = false; ExceptionChecker ec = this; while (!exceptionCaught && ec != null) { if (ec.catchable != null) { for (Iterator<Type> iter = ec.catchable.iterator(); iter.hasNext(); ) { Type catchType = (Type) iter.next(); if (ts.isSubtype(t, catchType, ts.emptyContext())) { exceptionCaught = true; break; } } } if (!exceptionCaught && ec.throwsSet != null) { // add t to ec's throwsSet. ec.throwsSet.add(t); } if (ec.catchAllThrowable) { // stop the propagation exceptionCaught = true; } ec = ec.pop(); } if (!exceptionCaught) { reportUncaughtException(t, pos); } } }
void uncaughtType(Type t, Position pos) throws SemanticException { SemanticException e = new SemanticException( codeType + " cannot throw a \"" + t + "\"; the exception must either be caught or declared to be thrown.", pos); Map<String, Object> map = CollectionFactory.newHashMap(); map.put(CodedErrorInfo.ERROR_CODE_KEY, CodedErrorInfo.ERROR_CODE_SURROUND_THROW); map.put("TYPE", t.toString()); e.setAttributes(map); throw e; }
/** Type check the statement. */ public Node typeCheck(TypeChecker tc) throws SemanticException { TypeSystem ts = tc.typeSystem(); // Check that all initializers have the same type. // This should be enforced by the parser, but check again here, // just to be sure. Type t = null; for (Iterator i = inits.iterator(); i.hasNext(); ) { ForInit s = (ForInit) i.next(); if (s instanceof LocalDecl) { LocalDecl d = (LocalDecl) s; Type dt = d.type().type(); if (t == null) { t = dt; } else if (!t.equals(dt)) { throw new InternalCompilerError( "Local variable " + "declarations in a for loop initializer must all " + "be the same type, in this case " + t + ", not " + dt + ".", d.position()); } } } if (cond != null && !ts.isImplicitCastValid(cond.type(), ts.Boolean())) { throw new SemanticException( "The condition of a for statement must have boolean type.", cond.position()); } return this; }