提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
创建一个扩展java.lang.RuntimeException的类,以创建可以随时抛出的异常类。
public class Main { public static void main(String[] args) { /* 时代Java - nowjava.com */ Object chatServer = null; try { call("Hello, how are you?"); } catch (MyException e) { System.out.println("Caught a connection unavailable Exception!"); } disconnectChatServer(chatServer); } private static void disconnectChatServer(Object chatServer) { /* N o w J a v a . c o m - 时代Java 提 供 */ throw new Myexception2("Chat server is empty"); } private static void call(String chatMessage) throws MyException { throw new MyException("Can't find the chat server"); } } class MyException extends Exception { MyException(String message) {