MCQs on Error Handling and Exceptions | Perl

Explore advanced error handling, exceptions, and debugging techniques in Perl. Learn about eval, Try::Tiny, error trapping, and logging to write more robust and fault-tolerant Perl applications.


Advanced eval and Error Trapping (10 Questions)

  1. What does the eval function in Perl do?
    A) Evaluates expressions at runtime
    B) Handles exceptions during runtime
    C) Executes commands from a file
    D) Prints error messages
  2. Which statement is true about eval in Perl?
    A) It throws exceptions automatically
    B) It can catch runtime errors without crashing
    C) It only works with warnings enabled
    D) It can only evaluate simple expressions
  3. What happens when eval encounters an error in Perl?
    A) It halts program execution immediately
    B) It returns an error message as a string
    C) It throws a runtime error
    D) It ignores the error and continues
  4. How do you capture error information after an eval block in Perl?
    A) By checking $@
    B) By using die
    C) By checking $!
    D) By using warn
  5. Which of the following is the correct way to check for an error after using eval?
    A) if ($@) { print "Error: $@"; }
    B) if ($!) { print "Error: $!"; }
    C) if ($@ == 1) { print "Error: $@"; }
    D) if ($@ == 0) { print "Error: $@"; }
  6. What is the primary function of $@ in Perl?
    A) Holds the error message from eval
    B) Stores the status of the program
    C) Holds the last value of the evaluated expression
    D) Tracks the location of the error
  7. What is the purpose of die in Perl?
    A) It prints a message and terminates the program
    B) It logs errors without stopping the program
    C) It suppresses runtime errors
    D) It raises a warning without halting the program
  8. What happens if die is called inside an eval block?
    A) It halts the program immediately
    B) It returns an error message and continues execution
    C) It rethrows the error to the calling code
    D) It throws an exception outside the eval block
  9. How can you control the error trapping behavior in Perl?
    A) By using trap statement
    B) By setting $@
    C) By manipulating $SIG{__DIE__}
    D) By using throw
  10. Which function is used to capture the output of an eval block in Perl?
    A) eval
    B) do
    C) catch
    D) try

Use of Try::Tiny and Other Modules (10 Questions)

  1. What does the Try::Tiny module provide in Perl?
    A) A way to handle exceptions cleanly without using eval
    B) A method for defining custom errors
    C) A module for logging errors
    D) A debugging tool for errors
  2. How do you use Try::Tiny to handle exceptions in Perl?
    A) try { ... } catch { ... }
    B) try { ... } except { ... }
    C) eval { ... } catch { ... }
    D) try { ... } trap { ... }
  3. Which of the following is a key benefit of using Try::Tiny?
    A) It adds automatic logging to errors
    B) It prevents all runtime errors
    C) It simplifies error handling and cleanup
    D) It eliminates the need for eval
  4. Which module should you use to throw custom exceptions in Perl?
    A) Exception::Class
    B) Try::Tiny
    C) Error::Simple
    D) Die::Helper
  5. How can you specify a custom error class using Try::Tiny?
    A) throw MyError("message")
    B) throw "message"
    C) throw MyError->new("message")
    D) exception MyError("message")
  6. Which of these modules provides a way to catch and report errors in Perl?
    A) Error::Simple
    B) Try::Tiny
    C) Exception::Class
    D) All of the above
  7. What is the purpose of finally in Try::Tiny?
    A) To execute code after the try block regardless of success or failure
    B) To throw an exception after a try block
    C) To catch errors after the try block
    D) To handle specific error types
  8. In Perl, which of the following allows exception chaining?
    A) Throwable
    B) Error::Simple
    C) Exception::Class
    D) Try::Tiny
  9. Which module should you use to handle errors with automatic retries?
    A) Retry::Tiny
    B) Error::Simple
    C) Exception::Retry
    D) Try::Tiny
  10. Which Perl statement will stop the execution of a try block and pass control to the catch block?
    A) throw
    B) die
    C) return
    D) next

Logging and Debugging Strategies (10 Questions)

  1. Which Perl module is commonly used for logging errors?
    A) Log::Log4perl
    B) Error::Log
    C) Try::Log
    D) Perl::Logging
  2. How do you initialize Log::Log4perl for error logging?
    A) Log::Log4perl->init($config_file)
    B) Log::Log4perl->start()
    C) init_logging($config_file)
    D) start_logging($config_file)
  3. What is the default log level in Log::Log4perl?
    A) DEBUG
    B) INFO
    C) ERROR
    D) WARN
  4. Which log level in Log::Log4perl is used to track debug-level messages?
    A) ERROR
    B) DEBUG
    C) INFO
    D) TRACE
  5. How do you add a logger in Log::Log4perl?
    A) my $logger = Log::Log4perl->get_logger()
    B) my $logger = Log::Log4perl->new()
    C) my $logger = Log::Log4perl->init_logger()
    D) my $logger = Log::Log4perl->create_logger()
  6. Which method is used to log an error message in Log::Log4perl?
    A) log_error()
    B) error_log()
    C) error()
    D) warn()
  7. What does warn do in Perl?
    A) Logs an error and terminates the program
    B) Prints a warning but does not stop execution
    C) Captures errors silently
    D) Logs to a file
  8. Which Perl function is used for debugging?
    A) debug()
    B) trace()
    C) DB::
    D) debugger()
  9. How can you enable automatic debugging in Perl?
    A) Using perl -d
    B) Using use Debugger
    C) Using perl -trace
    D) Using use Debug
  10. Which debugging tool can be used for in-depth Perl debugging?
    A) Devel::ebug
    B) DB::
    C) Devel::Trace
    D) Devel::Debugger

Answers Table

QNoAnswer (Option with Text)
1A) Evaluates expressions at runtime
2B) It can catch runtime errors without crashing
3B) It returns an error message as a string
4A) By checking $@
5A) if ($@) { print "Error: $@"; }
6A) Holds the error message from eval
7A) It prints a message and terminates the program
8B) It returns an error message and continues execution
9C) By manipulating $SIG{__DIE__}
10A) eval
11A) A way to handle exceptions cleanly without using eval
12A) try { ... } catch { ... }
13C) It simplifies error handling and cleanup
14A) Exception::Class
15C) throw MyError->new("message")
16D) All of the above
17A) To execute code after the try block regardless of success or failure
18C) Exception::Class
19D) Try::Tiny
20A) throw
21A) Log::Log4perl
22A) Log::Log4perl->init($config_file)
23B) INFO
24B) DEBUG
25A) my $logger = Log::Log4perl->get_logger()
26C) error()
27B) Prints a warning but does not stop execution
28C) DB::
29A) Using perl -d
30B) DB::

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