MCQs on Basic Error Handling | Elixir

Error handling is a crucial aspect of writing robust Elixir applications. By understanding how to manage errors using try, catch, and rescue, raising custom errors with raise, and using after for cleanup, developers can create more reliable and maintainable code. This guide covers basic error handling techniques in Elixir to improve application resilience.

1. Using try, catch, rescue

  1. In Elixir, which block is used to handle exceptions in the code?
    • a) catch
    • b) rescue
    • c) try
    • d) finally
  2. What does the try block do in Elixir error handling?
    • a) Catches exceptions
    • b) Executes code that might raise an exception
    • c) Defines cleanup actions
    • d) Raises errors manually
  3. How do you handle errors that occur within the try block in Elixir?
    • a) Using throw
    • b) Using rescue
    • c) Using catch
    • d) Using raise
  4. What is the correct syntax to rescue an error from the try block in Elixir?
    • a) try ... rescue Exception -> ... end
    • b) catch ... exception -> ... end
    • c) try ... catch Error -> ... end
    • d) throw ... Exception -> ... end
  5. Which of the following will NOT be caught by the rescue block in Elixir?
    • a) Runtime exceptions
    • b) Compile-time errors
    • c) Arithmetic errors
    • d) Custom exceptions
  6. Which statement is true about the catch block in Elixir?
    • a) It handles both exceptions and errors.
    • b) It handles only system-level errors.
    • c) It is not used for exception handling.
    • d) It is used to catch non-local returns and errors.
  7. What is the role of the try block in Elixir?
    • a) It defines a section of code where errors can be raised.
    • b) It executes code in the background while handling errors.
    • c) It catches errors that occur within the block.
    • d) It ensures that exceptions are not thrown in the code.
  8. Which of the following is a valid way to handle errors in Elixir?
    • a) try ... catch
    • b) try ... rescue
    • c) try ... throw
    • d) try ... finally

2. Raising Errors with raise

  1. Which function is used to raise errors in Elixir?
    • a) throw
    • b) raise
    • c) catch
    • d) exit
  2. What is the default exception type raised by the raise function in Elixir if no argument is provided?
  • a) RuntimeError
  • b) Error
  • c) Exception
  • d) RuntimeException
  1. What argument is needed to raise a custom exception in Elixir using raise?
  • a) A message string
  • b) A list of parameters
  • c) An exception module
  • d) None; raise automatically uses default exceptions
  1. How do you raise a specific exception with a custom message in Elixir?
  • a) raise Exception, message: "Error occurred"
  • b) raise "Error occurred"
  • c) raise Error("Error occurred")
  • d) raise :error, "Error occurred"
  1. Which of the following is the correct way to raise an exception with Elixir’s raise?
  • a) raise RuntimeError, "Something went wrong"
  • b) raise :error, "Something went wrong"
  • c) raise "Something went wrong"
  • d) raise RuntimeError("Something went wrong")
  1. What happens when an error is raised with the raise function in Elixir?
  • a) The program continues executing normally.
  • b) It causes an immediate termination of the program.
  • c) It is caught by the try block.
  • d) It is logged for debugging purposes.
  1. Can the raise function be used to raise errors inside the try block in Elixir?
  • a) Yes, and it can be caught by the rescue block.
  • b) No, it will always terminate the program.
  • c) Yes, but it will be ignored by the catch block.
  • d) No, raise cannot be used inside try.
  1. Which of the following will raise an error with the message “Error raised” in Elixir?
  • a) raise RuntimeError, "Error raised"
  • b) throw "Error raised"
  • c) exit "Error raised"
  • d) catch "Error raised"

3. Understanding Exceptions

  1. What is an exception in Elixir?
  • a) A special type of error that can be recovered from.
  • b) An event that stops the program execution.
  • c) A type of warning in Elixir.
  • d) A system-level error that cannot be handled.
  1. How can exceptions be caught and handled in Elixir?
  • a) Using try, catch, rescue
  • b) By ignoring errors
  • c) Using throw and catch only
  • d) Using the after block for cleanup
  1. Which of the following statements is false about exceptions in Elixir?
  • a) Exceptions can be propagated to the caller.
  • b) Exceptions in Elixir are always fatal and cannot be handled.
  • c) Exception handling allows for safer code execution.
  • d) Exception handling prevents program crashes.
  1. What is the purpose of rescue in Elixir?
  • a) To catch system errors automatically
  • b) To define a block for error handling when exceptions are raised
  • c) To log all errors in a system
  • d) To throw errors explicitly
  1. What does the catch block do in Elixir’s error handling?
  • a) It catches exceptions raised by the raise function.
  • b) It handles non-local returns, like throw.
  • c) It handles both errors and exceptions.
  • d) It is used for cleanup actions after errors.
  1. What type of error will the catch block handle in Elixir?
  • a) Any exception type
  • b) Only system-level errors
  • c) Non-local returns such as throw
  • d) Compile-time errors
  1. Which of the following can be used to define custom exceptions in Elixir?
  • a) defmodule Exception
  • b) defmodule Error
  • c) defmodule CustomException
  • d) defmodule RuntimeError
  1. What type of errors should be caught using the catch block instead of rescue in Elixir?
  • a) System errors
  • b) Arithmetic errors
  • c) Non-local returns (e.g., throw)
  • d) Compile-time errors

4. Using after for Cleanup

  1. What does the after block in Elixir’s error handling do?
  • a) It defines code to be executed after an exception is raised.
  • b) It is used to catch all exceptions.
  • c) It runs cleanup code regardless of success or failure.
  • d) It catches only fatal errors.
  1. How can you use the after block in Elixir?
  • a) To ensure that some code runs regardless of whether an error occurs
  • b) To catch specific exceptions
  • c) To exit the program gracefully
  • d) To log errors after they are raised
  1. Which statement is true about the after block in Elixir?
  • a) It is always executed, regardless of whether an exception occurred or not.
  • b) It is executed only when a specific error is raised.
  • c) It will terminate the program after execution.
  • d) It is used for debugging purposes only.
  1. Which block is typically used for performing cleanup operations, such as closing files or database connections, in Elixir?
  • a) try
  • b) catch
  • c) finally
  • d) after
  1. How does the after block differ from the rescue block in Elixir?
  • a) The after block is used to handle specific exceptions, while rescue is used for cleanup.
  • b) The after block always executes, whereas rescue only executes if an exception occurs.
  • c) The after block cannot be used for error handling.
  • d) There is no difference; they are used interchangeably.
  1. What would be an ideal use case for the after block in Elixir error handling?
  • a) To handle specific exceptions
  • b) To perform cleanup tasks like closing resources
  • c) To terminate the application gracefully
  • d) To ignore the errors silently

Answer Table

QnoAnswer (Option with Text)
1b) rescue
2b) Executes code that might raise an exception
3b) Using rescue
4a) try ... rescue Exception -> ... end
5b) Compile-time errors
6d) It is used to catch non-local returns and errors
7a) It defines a section of code where errors can be raised
8b) try ... rescue
9b) raise
10a) RuntimeError
11a) A message string
12a) raise Exception, message: "Error occurred"
13a) raise RuntimeError, "Something went wrong"
14b) It causes an immediate termination of the program
15a) Yes, and it can be caught by the rescue block
16a) raise RuntimeError, "Something went wrong"
17a) A special type of error that can be recovered from
18a) Using try, catch, rescue
19b) Exceptions in Elixir are always fatal and cannot be handled
20b) To define a block for error handling when exceptions are raised
21b) It handles non-local returns, like throw
22c) Non-local returns such as throw
23a) defmodule Exception
24c) Non-local returns (e.g., throw)
25a) It defines code to be executed after an exception is raised
26a) To ensure that some code runs regardless of whether an error occurs
27a) It is always executed, regardless of whether an exception occurred or not
28d) after
29b) The after block always executes, whereas rescue only executes if an exception occurs
30b) To perform cleanup tasks like closing resources

4o mini

Use a Blank Sheet, Note your Answers and Finally tally with our answer at last. Give Yourself Score.

X
error: Content is protected !!
Scroll to Top