Path: blob/master/test/jdk/java/lang/Class/getMethod/Exceptions.java
41153 views
/*1* Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @bug 431991026* @summary Verify that exceptions are thrown as expected.27*/2829public class Exceptions {30void m0() {}31public void m1() {}32private void m2() {}33protected void m4() {}3435private static final String [] npe = {null};36private static final String [] nsme = {"m0", "m2", "m4", "m6"};37private static final String [] pass = {"m1"};3839private void test(String s, Class ex) {40Throwable t = null;41try {42getClass().getMethod(s, new Class[] {});43} catch (Throwable x) {44if (ex.isAssignableFrom(x.getClass()))45t = x;46}47if ((t == null) && (ex != null))48throw new RuntimeException("expected " + ex.getName()49+ " for " + s);50else51System.out.println(s + " OK");52}5354public static void main(String [] args) {55Exceptions e = new Exceptions();56for (int i = 0; i < npe.length; i++)57e.test(npe[i], NullPointerException.class);58for (int i = 0; i < nsme.length; i++)59e.test(nsme[i], NoSuchMethodException.class);60for (int i = 0; i < pass.length; i++)61e.test(pass[i], null);62}63}646566