Wednesday, 5 April 2023

Exception Handling in Apex Part 4 | How to create User Defined Exception Class in Apex

                      In this post, we will see Exception Handling in Apex Part 4 | How to create User Defined Exception Class in Apex


Program Code: (class NewException)

public class NewException extends Exception {

    override public String getMessage()

    {

        return 'divide by zero exception';

    }

}


Program Code: (Anonymous Window)

System.debug('Before Exception');

Integer a = 10, b = 0;


try {

    if (b == 0) {

        throw new NewException();

    } else {

        Double result = a / b;

    }

}

catch (NewException e) {

    System.debug('Cause:' + e.getCause());

    System.debug('Message:' + e.getMessage());

    System.debug('Line Number:' + e.getLineNumber());

    System.debug('Stack Trace:' + e.getStackTraceString());

    System.debug('Type Name:' + e.getTypeName());

finally {

    System.debug('In Finally Block');

}

System.debug('After Exception');


No comments:

Post a Comment