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)
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
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
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
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
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: $@"; }
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
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
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
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
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)
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
How do you use Try::Tiny to handle exceptions in Perl? A) try { ... } catch { ... } B) try { ... } except { ... } C) eval { ... } catch { ... } D) try { ... } trap { ... }
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
Which module should you use to throw custom exceptions in Perl? A) Exception::Class B) Try::Tiny C) Error::Simple D) Die::Helper
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")
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
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
In Perl, which of the following allows exception chaining? A) Throwable B) Error::Simple C) Exception::Class D) Try::Tiny
Which module should you use to handle errors with automatic retries? A) Retry::Tiny B) Error::Simple C) Exception::Retry D) Try::Tiny
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)
Which Perl module is commonly used for logging errors? A) Log::Log4perl B) Error::Log C) Try::Log D) Perl::Logging
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)
What is the default log level in Log::Log4perl? A) DEBUG B) INFO C) ERROR D) WARN
Which log level in Log::Log4perl is used to track debug-level messages? A) ERROR B) DEBUG C) INFO D) TRACE
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()
Which method is used to log an error message in Log::Log4perl? A) log_error() B) error_log() C) error() D) warn()
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
Which Perl function is used for debugging? A) debug() B) trace() C) DB:: D) debugger()
How can you enable automatic debugging in Perl? A) Using perl -d B) Using use Debugger C) Using perl -trace D) Using use Debug
Which debugging tool can be used for in-depth Perl debugging? A) Devel::ebug B) DB:: C) Devel::Trace D) Devel::Debugger
Answers Table
QNo
Answer (Option with Text)
1
A) Evaluates expressions at runtime
2
B) It can catch runtime errors without crashing
3
B) It returns an error message as a string
4
A) By checking $@
5
A) if ($@) { print "Error: $@"; }
6
A) Holds the error message from eval
7
A) It prints a message and terminates the program
8
B) It returns an error message and continues execution
9
C) By manipulating $SIG{__DIE__}
10
A) eval
11
A) A way to handle exceptions cleanly without using eval
12
A) try { ... } catch { ... }
13
C) It simplifies error handling and cleanup
14
A) Exception::Class
15
C) throw MyError->new("message")
16
D) All of the above
17
A) To execute code after the try block regardless of success or failure