gnu-arch-users
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[OT] Java is fun! (was Re: [Gnu-arch-users] Nit)


From: Tom Lord
Subject: [OT] Java is fun! (was Re: [Gnu-arch-users] Nit)
Date: Wed, 22 Oct 2003 11:21:07 -0700 (PDT)



from
http://java.sun.com/docs/books/jls/second_edition/html/binaryComp.doc.html#44952
 


        13.4.19 Method and Constructor Throws

        Changes to the throws clause of methods or constructors do not
        break compatibility with existing binaries; these clauses are
        checked only at compile time.




from
http://java.sun.com/docs/books/jls/second_edition/html/statements.doc.html#79311
 


        14.20 Unreachable Statements

        It is a compile-time error if a statement cannot be executed
        because it is unreachable. Every Java compiler must carry out
        the conservative flow analysis specified here to make sure all
        statements are reachable.

        [....]

        
        * A catch block C is reachable iff both of the following are true:

            Some expression or throw statement in the try block is
            reachable and can throw an exception whose type is
            assignable to the parameter of the catch clause C. (An
            expression is considered reachable iff the innermost
            statement containing it is reachable.)  There is no
            earlier catch block A in the try statement such that the
            type of C's parameter is the same as or a subclass of the
            type of A's parameter.


from
http://java.sun.com/docs/books/vmspec/2nd-edition/html/VMSpecTOC.doc.html

        [silence]


It apparently takes a heck of a lot of `catch (Throwable e)' 
clauses to write a general purpose Java package that actually 
honors its `throws' contracts.

Earlier we saw an example of Java that went:

        public List getLinesOfFile(String f) throws IOException {
                String realFile=translatePath(f);
                InputStream is=openTheStream(realFile);
                List rv=getLinesOfStream(is);
                is.close();
                return(rv);
        }

where it was presumed that all of the called methods have `throws
IOException' or no `throws' decl at all.  Shouldn't that code really
be (usual Java syntax disclaimer): 

        public List getLinesOfFile(String f) throws IOException {
                try {
                  String realFile=translatePath(f);
                  InputStream is=openTheStream(realFile);
                  List rv=getLinesOfStream(is);
                  is.close();
                  return(rv);
                } catch (IOException e) {
                  throw e;
                } catch (Throwable e) {
                  // catch fire
                  //
                }
        }


otherwise, what about the unfortunate caller who has code:

                try {
                  List lines=Reader.getLinesOfFile(fileName);
                  Answer a=Parser.parseLines(lines);
                } catch (IOException e) {
                  ...
                } catch (ParserException e) {
                  ...
                }
                
Hey, who _really_ raised that ParserException?

-t




reply via email to

[Prev in Thread] Current Thread [Next in Thread]