alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Java example source code file (TargetType63.java)

This example Java source code file (TargetType63.java) is included in the alvinalexander.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Learn more about this Java project at its project page.

Java - Java tags/keywords

classnotfoundexception, exception, runtimeexception, targettype63, throwable

The TargetType63.java Java example source code

/*
 * @test /nodynamiccopyright/
 * @summary smoke test for inference of throws type variables
 * @compile/fail/ref=TargetType63.out -XDrawDiagnostics TargetType63.java
 */
class TargetType63 {

    interface F<T extends Throwable> {
        void m() throws T;
    }

    void g1() { }
    void g2() throws ClassNotFoundException { }
    void g3() throws Exception { }

    <Z extends Throwable> void m1(F fz) throws Z { }
    <Z extends ClassNotFoundException> void m2(F fz) throws Z { }

    void test1() {
        m1(()->{ }); //ok (Z = RuntimeException)
        m1(this::g1); //ok (Z = RuntimeException)
    }

    void test2() {
        m2(()->{ }); //fail (Z = ClassNotFoundException)
        m2(this::g1); //fail (Z = ClassNotFoundException)
    }

    void test3() {
        m1(()->{ throw new ClassNotFoundException(); }); //fail (Z = ClassNotFoundException)
        m1(this::g2); //fail (Z = ClassNotFoundException)
        m2(()->{ throw new ClassNotFoundException(); }); //fail (Z = ClassNotFoundException)
        m2(this::g2); //fail (Z = ClassNotFoundException)
    }

    void test4() {
        m1(()->{ throw new Exception(); }); //fail (Z = Exception)
        m1(this::g3); //fail (Z = Exception)
    }
}

Other Java examples (source code examples)

Here is a short list of links related to this Java TargetType63.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.