try { // some code that may throw an exception } catch (Exception e) { System.out.println("Exception type: " + e.getClass()); }
package com.example.myapp.exceptions; public class MyCustomException extends Exception { public MyCustomException(String message) { super(message); } }In this example, we are creating a custom exception class called MyCustomException, which extends the Exception class. We're also providing a constructor that allows us to specify a message for the exception. The java.util.Exception class is included in the Java Standard Library, which is part of the java.util package.