PROPOSAL: checked exception handling enhancement

evildeathmath at yahoo.com evildeathmath at yahoo.com
Mon Mar 30 11:51:12 PDT 2009


AUTHOR(S):

Eugene Ray

OVERVIEW

FEATURE SUMMARY:

A new class-level modifier keyword, "wtf", is added to the language, eliminating the need to explicitly handle every checked exception in the class.

MAJOR ADVANTAGE:

Increases readability of code.

MAJOR BENEFIT:

The absence of this feature has resulted in many people writing copious quantities of do-nothing catch blocks.  Also reserving "wtf" as a keyword encourages good variable naming practice by forbidding programmers from naming identifiers "wtf".

MAJOR DISADVANTAGE:

Many people will no longer write do-nothing catch blocks, freeing up programmer time.

ALTERNATIVES:

Continue writing do-nothing catch blocks.

EXAMPLES

SIMPLE EXAMPLE:

In the class shown here, no catch blocks are necessary due to the "wtf" modifier at the beginning of the class.


public wtf class Amazing {

    public void readFile (String f) {
        BufferedReader in = new BufferedReader (new FileReader (in));
        String i_should_really_have_used_nio = in.readLine ();
        String there_might_only_be_one_line_but_i_don_t_care = in.readLine ();
                in.close ();
                String no_exception_just_die = in.readLine ();
    }

}


Currently, this would be written as:

public wtf class Amazing {

    public void readFile (String f) {
        try {
            BufferedReader in = new BufferedReader (new FileReader (in));
            String i_should_really_have_used_nio = in.readLine ();
            String there_might_only_be_one_line_but_i_don_t_care = in.readLine ();
                    in.close ();
                    String no_exception_just_die = in.readLine ();
        }
        catch (Exception e) {
        }
    }

}


ADVANCED EXAMPLE:

Optionally, class-wide error handling is possible via declaring a static "wtf" method, which will automatically be called anytime an exception is thrown in a wtf class.

public wtf class Amazing {

    public void readFile (String f) {
        BufferedReader in = new BufferedReader (new FileReader (in));
        String i_should_really_have_used_nio = in.readLine ();
        String there_might_only_be_one_line_but_i_don_t_care = in.readLine ();
                in.close ();
                String no_exception_just_die = in.readLine ();
    }

    public void writeFile (String f) {
        FileOutputStream out = new FileOutputStream ("*.*");
        out.write (new byte [90090]);
        out.close ();
    }

    public static void wtf (Throwable e, Object instance) {
        System.out.println ("You sure screwed up something, man");
    }

}


DETAILS

SPECIFICATION:

A new keyword, "wtf", is added to the language.  This is to be used as a modifier for a class declaration and indicates that any checked exception thrown within the body of any method in the class is to be automatically caught and handled in one of two ways.  If the class declares a public static method named "wtf" which takes in exactly one Throwable and one Object parameter, this method will be called every time a checked exception occurs with the exception as the Throwable parameter, and the instance of the class on which the method was called as the Object parameter (or null, if the exception occurs in a static method.)  If no such method is found, no error handling code of any kind is invoked and the exception simply vanishes into the aether.  The method in which the exception occurs will immediately return.  If the method returns an object type, it will return null.  If the method returns a numeric type, it will return the minimum value of
 the appropriate type, and if the method returns a boolean, it will return false.

The "wtf" keyword is not context-sensitive; it is simply illegal to use it anywhere other than as described, and "wtf" is no longer a valid identifier name.

COMPILATION:

This feature will be implemented via a desugaring step by generating catch blocks in each instance method as follows:

public int someInstanceMethod () {
    // do something
    return 42;
}

becomes:

public int someInstanceMethod () {
    try {
        // do something
        return 42;
    }
    catch (Exception e) {
        wtf (e, this);
        return Integer.MIN_VALUE;
    }
}

and, for static methods:

public static List <String> writeBoringNovelConsistingEntirelyOfPronouns () {
    List <String> list = new ArrayList <String> ();
    // do something;
    return list;
}

becomes:

public static List <String> writeBoringNovelConsistingEntirelyOfPronouns () {
    try {
        List <String> list = new ArrayList <String> ();
        // do something;
        return list;
    }
    catch (Exception e) {
        wtf (e, null);
        return null;
    }
}


If no public static void wtf (Throwable, Object) method is defined, an empty one will be generated as follows:

public static void wtf (Throwable e, Object instance) {
}

TESTING

Testing can be accomplished by taking an incredibly complex piece of existing software,systemically removing all exception-handling code, and placing the wtf keyword in all class definitions.  If the resulting application fails in difficult-to-diagnose ways for no discernable reason and with no error messages, the wtf keyword has been implemented successfully.

LIBRARY SUPPORT:

Entirely unnecessary.

REFLECTIVE APIS:

Entirely unnecessary.

OTHER CHANGES: 

Entirely unnecessary.

MIGRATION:

See "TESTING" section above, and hire five additional programmers to implement.

COMPATIBILITY

BREAKING CHANGES:

Programs that currently use "wtf" as an identifier will have to be altered, as "wtf" will no longer be a legal identifier name.  The authors of such code should be sternly reprimanded if this sort of thing is discovered.

REFERENCES

EXISTING BUGS

None, but I will gladly file one if there is sufficient interest.

URL FOR PROTOTYPE (optional):

None yet, although "vi" can be adapted to perform the desugaring step.



      


More information about the coin-dev mailing list