try { // some code that may throw an exception } catch (Exception e) { e.printStackTrace(); }
class CustomException extends Exception { public CustomException(String message) { super(message); } } public class Main { public static void main(String[] args) { try { throw new CustomException("My custom exception message"); } catch (CustomException e) { e.printStackTrace(); } } }In this example, a custom exception class is created by extending the Exception class. In the main method, an instance of this custom exception is thrown and caught, and its stack trace is printed using the printStackTrace method. Overall, the java.util package library is quite vast and contains many classes and methods for different purposes. The Exception class and its printStackTrace method are just a small part of this library, but are crucial for handling errors and debugging programs.