throw - finally

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Henning Makholm

    throw - finally

    Having started in a job where I am to write Java code, I am working my
    way through the Java Language Specification. But the following
    situation gives me problems:

    public class Foo {
    public static void foo(boolean b) {
    try {
    System.out.prin tln(b);
    if( b ) throw new Exception();
    } finally { return ; }
    }
    public static void main(String[] args) {
    foo(true); foo(false);
    }
    }

    This code is safe at runtime (because the finally block silently
    discards the exception when it terminates abruptly), but as far as I
    can read the JLS, it should lead to a compile-time error. However, the
    code is happily accepted by all of Sun JDK 1.5, Eclipse 3.2 JDK and
    Jikes 1.22.

    Specifically, in the third edition of JLS, section 14.18 about the
    throw statement says:

    | .. at least one of the following three conditions must be true, or a
    | compile-time error occurs:
    | * The exception is not a checked exception [...]
    | * The throw statement i contained in the try block of a try
    | statement and the type of the Expression is assignable to the
    | type of at least one catch clause [...]
    | * The throw statement is contained in a method or constructor
    | and the type of the Expression is assignable to at least one
    | type listed in the throws clause of the declaration.

    Neither of those three conditions are true for my code, so why am I
    not getting the compile-time error the text promises me? Am I
    overlooking some relevant general clause somewhere, and if so which?

    One compiler that does produce a compile-time error is gcj, so if I'm
    wrong at least I'm not alone...

    --
    Henning Makholm "Jeg har tydeligt gjort opmærksom på, at man ved at
    følge den vej kun bliver gennemsnitligt ca. 48 år gammel,
    og at man sætter sin sociale situation ganske overstyr og, så
    vidt jeg kan overskue, dør i dybeste ulykkelighed og elendighed."
  • Joshua

    #2
    Re: throw - finally

    On Fri, 04 Aug 2006 14:24:48 +0200, Henning Makholm wrote:
    >
    Specifically, in the third edition of JLS, section 14.18 about the
    throw statement says:
    >
    | .. at least one of the following three conditions must be true, or a
    | compile-time error occurs:
    | * The exception is not a checked exception [...]
    | * The throw statement i contained in the try block of a try
    | statement and the type of the Expression is assignable to the
    | type of at least one catch clause [...]
    | * The throw statement is contained in a method or constructor
    | and the type of the Expression is assignable to at least one
    | type listed in the throws clause of the declaration.
    >
    Neither of those three conditions are true for my code, so why am I
    not getting the compile-time error the text promises me? Am I
    overlooking some relevant general clause somewhere, and if so which?
    >
    One compiler that does produce a compile-time error is gcj, so if I'm
    wrong at least I'm not alone...
    At first, that would seem to violate 14.18, but you also have to look at
    how it is really treated in the bytecode (actually, it's a little more
    complex, but that's beyond the scope of this post):

    public class Foo {
    public static void foo(boolean b) {
    try {
    System.out.prin tln(b);
    if( b ) throw new Exception();
    } catch ( any ) { return ; }
    }
    public static void main(String[] args) {
    foo(true); foo(false);
    }
    }

    So this would leave bullet two satisfied for the runtime. I don't think
    GCJ compiles to bytecode like the JDK does, so GCJ's implementation may
    decompile to something that would defy that statement.

    P.S. A better way to learn Java is through Sun's tutorials, although
    you'll need to use the 1.5 "New Features" to learn to use Generics,
    auto-boxing, etc.

    Comment

    • Henning Makholm

      #3
      Re: throw - finally

      Scripsit Joshua <Pidgeot18@gmai l.com>
      >Neither of those three conditions are true for my code, so why am I
      >not getting the compile-time error the text promises me?
      At first, that would seem to violate 14.18, but you also have to look at
      how it is really treated in the bytecode (actually, it's a little more
      complex, but that's beyond the scope of this post):
      Where in the spec does it say that one has to look at how it is really
      treated in the bytecode?
      P.S. A better way to learn Java is through Sun's tutorials,
      But I want the raw truth, not some predigested
      he-probably-doesnt-want-to-know-the-details tutorial.
      although you'll need to use the 1.5 "New Features" to learn to use
      Generics, auto-boxing, etc.
      We're not supposed to go beyond 1.1 for compatibility reasons anyway.

      --
      Henning Makholm "Han råber og skriger, vakler ud på kørebanen og
      ind på fortorvet igen, hæver knytnæven mod en bil,
      hilser overmådigt venligt på en mor med barn, bryder ud
      i sang og stiller sig til sidst op og pisser i en port."

      Comment

      • Edmond Dantes

        #4
        Re: throw - finally

        Henning Makholm wrote:
        >although you'll need to use the 1.5 "New Features" to learn to use
        >Generics, auto-boxing, etc.
        >
        We're not supposed to go beyond 1.1 for compatibility reasons anyway.
        One of the many reasons I left Java behind is the continual frustration with
        compatability. All the great new features are in the latest releases, and
        many of them makes writing the code that much easier. But some Joe Blow
        still has 1.1, and can't be expected to install the latest JRE. Arrrgh!

        I now resort to using AJAX for all of my client browser applications. Works
        great. Less filling.

        --
        -- Edmond Dantes, CMC
        And Now for something Completely Different:









        Posted Via Usenet.com Premium Usenet Newsgroup Services
        ----------------------------------------------------------
        ** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
        ----------------------------------------------------------
        Best Usenet Service Providers 2025 ranked by Newsgroup Access Newsservers, Usenet Search, Features & Free Trial. Add VPN for privacy.

        Comment

        • Sigmund Hansen

          #5
          Re: throw - finally

          Edmond Dantes wrote:
          Henning Makholm wrote:
          >
          >>although you'll need to use the 1.5 "New Features" to learn to use
          >>Generics, auto-boxing, etc.
          >We're not supposed to go beyond 1.1 for compatibility reasons anyway.
          >
          One of the many reasons I left Java behind is the continual frustration with
          compatability. All the great new features are in the latest releases, and
          many of them makes writing the code that much easier. But some Joe Blow
          still has 1.1, and can't be expected to install the latest JRE. Arrrgh!
          >
          I now resort to using AJAX for all of my client browser applications. Works
          great. Less filling.
          >
          Why can't the user be expected to install a newer JRE?
          Of course, the user should be given a link to:


          It's kind of like expecting a user to have the latest drivers for his
          graphics card,
          or that they have .NET Whatsitcalled 1.1 installed.
          Or for that matter, if you don't bother buying Visual Studio, but use
          the express version,
          expecting the user to download the msvc80 redistributable s, which of
          course can't be included in your software, and since you can't build
          installers, you don't get them put into installers either.

          So, if the user can't use your software, just because they refuse to
          install something that is required for the software to work, then too
          bad for them.

          Sometimes, you have to expect a user to be slightly competent, and those
          who aren't probably shouldn't be playing with your software anyway.
          Unless of course you are making software for people who don't know how
          to use computers.

          But of course that's just my unholy opinion.

          Comment

          Working...