java - Which Exceptions require a throws statement for the method? -
this question has answer here:
in java, there kinds of exceptions require throws statement:
public void mymethod() throws ioexception { throw new ioexception("error!"); } while others don't:
public void myothermethod() { throw new illegalargumentexception("error!"); } public void mythirdmethod() { throw new error("error!"); } the first method wont compile wthout throws statement.
what criteria determining if exception/error requires throws statement?
exception checked , complained compiler called checked exceptions in java.
at compile time, java compiler checks program contains handlers checked exceptions. java compiler analyzes checked exceptions can result execution of method or constructor.for each checked exception possible result, throws clause method or constructor must mention class or superclasses of exception.
read more jls: http://docs.oracle.com/javase/specs/jls/se5.0/html/exceptions.html
ioexception checked exception , hence java compiler asks either catch or throw it. while illegalargumentexception run time exception , not checked or complained compiler.
Comments
Post a Comment