Apex Exceptions Flashcards

1
Q

List some apex exceptions

A
  • DmlException
  • QueryException
  • ListException
  • SObjectException
  • LimitException
    • raised when our code exceeds a governor limit
    • the LimitException is the only standard exception that cannot be caught and handled
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How do you create a Custom Exception?

A
  • we can create custom exceptions by making Apex classes with names that end in Exception and they extend the Exception class

public class FieldSecurityException extends Exception {}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How do you throw a custom exception?

A
  • we can throw custom exceptions by using the throw keyword and instantiating our Apex class that represents our custom exception

throw new FieldSecurityException(‘bleep.’);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Explain Try-Catch-Finally Blocks

A

they handle our exceptions

  • try
    • will surround the code that may throw an exception
  • catch
    • follows the try block
    • we can have one or more catch blocks
    • handles the exception thrown by the code in the try block
  • finally
    • follows the try block (and the catch block, if one is included)
    • will execute whether the code in the try block raised exception or not
  • try blocks cannot be alone - they must have at least one catch or finally block following them
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Why do all exceptions (both standard and custom) have access to the Exception class methods?

A
  • because all exceptions (both standard and custom) extend the Exception class, they inherit that class’s methods
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What are some exception class methods?

A
  • getLineNumber() - returns code line exception was thrown at
  • getMessage() - returns exception message
  • getStackTraceString() - returns stack trace
  • getTypeName() - returns type of exception
How well did you know this?
1
Not at all
2
3
4
5
Perfectly