java - Regarding Final Rethrow -


i read in documentation of precise rethrow that, http://www.theserverside.com/tutorial/ocpjp-use-more-precise-rethrow-in-exceptions-objective-java-7

basically, can list specific exceptions in throws clause of method, if not explicitly handled catch block if:

the try block throws specific exception @ point in time.

the specific exception isn't handled @ point preceding catch block

the exception named in throws clause of method signature must on class hierarchy of @ least 1 exception being handled , rethrown catch block (subtype or supertype)

have @ code (attention on throws clause of main)

class openexception extends exception {}  class closeexception extends exception {}  public class prethrow {     public static void main(string args[]) throws openexception, closeexception, java.io.ioexception     {         boolean flag = true;         try         {             if (flag)             {                 throw new openexception();             }             else             {                 throw new closeexception();             }         }         catch (exception e)         {             system.out.println(e.getmessage());             throw e;         }     }  } 

it mentioned in first condition that, you can list specific exception in throws clause if try block throws specific exception @ point in time.

in code try block never throws java.io.ioexception, still including in throws clause produce no error.

why?

the quote mention misleading:

  • if checked exception may thrown in method body must declare in throws clause of method.
  • but may add any exception throws clause of method, whether can thrown or not.

for example compiles:

public void m() throws ioexception {} 

Comments

Popular posts from this blog

PHP DOM loadHTML() method unusual warning -

python - How to create jsonb index using GIN on SQLAlchemy? -

c# - TransactionScope not rolling back although no complete() is called -