1. Description: The getClass method is a method in Java Exception class that is used to obtain the runtime class of an exception. The method returns an object of Class type which represents the class that was thrown as an exception.
2. Code Examples:
Example 1: In this example, we are catching an exception and retrieving the class name using getClass() method.
try { // code that throws an exception } catch (Exception e) { System.out.println("Exception class name: " + e.getClass().getName()); }
Output: Exception class name: java.lang.Exception
Example 2: In this example, we are throwing a user-defined exception and retrieving the class name using getClass() method.
try { throw new MyException("This is a user defined exception"); } catch (Exception e) { System.out.println("Exception class name: " + e.getClass().getName()); }
Output: Exception class name: MyException
3. Package Library: The getClass() method is a part of the java.lang package, which is a part of the Java Standard Library.
Java Exception.getClass - 30 examples found. These are the top rated real world Java examples of Exception.getClass extracted from open source projects. You can rate examples to help us improve the quality of examples.