Salesforce Platform Developer I (legacy) — Question 242
How should a developer create a new custom exception class?
Answer options
- A. public class CustomException extends Exception{}
- B. CustomException ex = new (CustomException)Exception();
- C. public class CustomException implements Exception{}
- D. (Exception)CustomException ex = new Exception();
Correct answer: A
Explanation
The correct answer is A because it correctly extends the Exception class, allowing CustomException to inherit its properties and behaviors. Option B is incorrect as it has a syntax error and does not properly instantiate a custom exception. Option C is wrong because exceptions in Java do not implement interfaces but rather extend from the Exception class. Option D is incorrect due to improper casting and instantiation of an Exception instance instead of a CustomException.