Types of Exception Classes in Apex

Exception Classes in Apex

Question

What are the types of exception classes developers can choose between in Apex?

Answers

Explanations

Click on the arrows to vote for the correct answer

A. B. C. D.

A.

The correct answer is A. System-defined, user-defined.

In Apex, an exception is an unexpected event that occurs during the execution of a program. When an exception occurs, the program terminates abnormally and control is transferred to the exception handler. To handle exceptions in Apex, developers can use exception classes.

There are two types of exception classes in Apex:

  1. System-defined Exception Classes: These classes are provided by Salesforce and are used to handle various types of exceptions that can occur during the execution of an Apex program. Examples of system-defined exception classes in Apex include:
  • DmlException: This exception is thrown when a DML operation fails.
  • QueryException: This exception is thrown when a query fails.
  • NullPointerException: This exception is thrown when a null value is encountered where it is not allowed.
  1. User-defined Exception Classes: These classes are created by developers to handle specific exceptions that may occur during the execution of their Apex program. Developers can create custom exception classes by extending the built-in Apex Exception class.

Example:

java
public class CustomException extends Exception { public CustomException(String message) { super(message); } }

In this example, a custom exception class named CustomException is created by extending the built-in Apex Exception class. The constructor for the custom exception class takes a message parameter and passes it to the super constructor.

In summary, developers can choose between two types of exception classes in Apex: system-defined and user-defined. System-defined exception classes are provided by Salesforce and can be used to handle various types of exceptions that can occur during the execution of an Apex program. User-defined exception classes are created by developers to handle specific exceptions that may occur during the execution of their Apex program.