Creating an Exception Class - Java Object Oriented Design

Java examples for Object Oriented Design:Exception

Introduction

Create a class that extends java.lang.RuntimeException to create an exception class that can be thrown at any time.

Create a class that extends java.lang.Exception to generate a checked exception class.

Demo Code

public class Main {
  public static void main(String[] args) {
    Object chatServer = null;//from w w w.  j  ava  2  s.  c  o  m

    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) {
    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) {
    super(message);
  }
}

class Myexception2 extends RuntimeException {
  Myexception2(String message) {
    super(message);
  }
}

Related Tutorials