A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.tutorialspoint.com/javaexamples/exception_user.htm below:

How to create user defined exception in Java

How to create user defined exception in Java Problem Description

How to create user defined exception?

Solution

This example shows how to create user defined exception by extending Exception Class.

class MyException extends Exception {
   String s1;
   MyException(String s2) {
      s1 = s2;
   } 
   @Override
   public String toString() { 
      return ("Output String = "+s1);
   }
}
public class NewClass { 
   public static void main(String args[]) { 
      try {
         throw new MyException("Custom message");
      } catch(MyException exp) {
         System.out.println(exp);
      }
   }
}
Result

The above code sample will produce the following result.

Output String = Custom message

java_exceptions.htm


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4