Study for the Salesforce Platform Developer Exam. Study with flashcards and multiple choice questions, each question has hints and explanations. Get ready for your exam!

Practice this question and more.


Which statement can a developer use to throw a custom exception?

  1. throw new RecordNotFoundException("problem occurred")

  2. throw new RecordNotFoundException()

  3. throw RecordNotFoundException("problem occurred")

  4. throw RecordNotFoundException()

The correct answer is: throw new RecordNotFoundException("problem occurred")

A developer can throw a custom exception in Salesforce by using the syntax that properly instantiates the exception object. The correct way to create and throw a custom exception is by using the `new` keyword followed by the name of the exception class and parentheses to indicate that you are creating a new instance of that exception. Additionally, a custom exception can accept a message that describes the specific problem, which can be useful for debugging and logging purposes. In the correct answer, the instantiation includes a message "problem occurred," which is passed to the constructor of the `RecordNotFoundException` class. This provides context for the exception being thrown, allowing downstream code or the logged error to better understand what went wrong. Using the other formats presented either lacks the necessary instantiation of the exception object or tries to call the constructor without the `new` keyword, which wouldn’t correctly create an instance of the exception. This is why the correct answer is valid, ensuring that the developer adheres to the proper exception handling standards in the Salesforce platform.