集册 Java实例教程 创建一个异常类

创建一个异常类

欢马劈雪     最近更新时间:2020-01-02 10:19:05

586
提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
创建一个扩展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) {
展开阅读全文