how to implement it using Error.pm(转)

本文详细探讨了Perl中的异常处理机制,特别是如何利用Error.pm模块来实现面向对象的异常处理。通过对比传统的错误处理方式,突出了异常处理的优势,并介绍了Error.pm的基本用法及与其他模块如Fatal.pm的结合使用。

The main goal of this article is to discuss in detail about exception handling in Perl and how to implement it using Error.pm. On our way, we'll be touching upon the advantages of using exception-handling over traditional error-handling mechanisms, exception handling with eval {}, problems with eval {} and the functionalities available in Fatal.pm. But by and large, our focus we'll be on using Error.pm for exception handling.

What Is an Exception ?

An exception can be defined as an event that occurs during the execution of a program that deviates it from the normal execution path. Different types of errors can cause exceptions. They can range from serious errors such as running out of virtual memory to simple programming errors such as trying to read from an empty stack or opening an invalid file for reading.

 

Related Reading

Computer Science & Perl Programming

Best of The Perl Journal

Table of Contents

Sample Chapter

 

 

An exception usually carries with it three important pieces of information:

  1. The type of exception - determined by the class of the exception object
  2. Where the exception occurred - the stack trace
  3. Context information - error message and other state information

 

An exception handler is a piece of code used to gracefully deal with the exception. In the rest of article, the terms exception handler and catch block will be used interchangeably.

By choosing exceptions to manage errors, applications benefit a lot over traditional error-handling mechanisms. All the advantages of using exception handling are discussed in detail in the next section.

Advantages of Using Exception Handling

Object-oriented exception handling allows you to separate error-handling code from the normal code. As a result, the code is less complex, more readable and, at times, more efficient. The code is more efficient because the normal execution path doesn't have to check for errors. As a result, valuable CPU cycles are saved.

Another important advantage of OO exception handling is the ability to propagate errors up the call stack. This happens automatically without you, the programmer, explicitly checking for return values and returning them to the caller. Moreover, passing return values up the call stack is error prone, and with every hop there is a tendency to lose vital bits of information.

Most of the time, the point at which an error occurs is rarely the best place to handle it. So, the error needs to be propagated up the call stack. But by the time the error reaches the place where it can be handled suitably, a lot of the error context is lost. This is a common problem with traditional error-handling mechanisms (i.e. checking for return values and propagating them to the caller). Exceptions come to the rescue by allowing contextual information to be captured at the point where the error occurs and propagate it to a point where it can be effectively used/handled.

For instance, if you have a function processFile() that is the fourth method in a series of method calls made by your application. And also func1() is the only method interested in the errors that occur within processFile(). With a traditional error-handling mechanism, you would do the following to propagate the error code up the call stack until the error finally reaches func1().

sub func3 { my $retval = processFile($FILE); if (!$retval) { return $retval; } else { .... } } sub func2 { my $retval = func3(); if (!$retval) { return $retval; } else { .... } } sub func1 { my $retval = func2(); if (!$retval) { return $retval; } else { .... } } sub processFile { my $file = shift; if (!open(FH, $file)) { return -1; } else { # Process the file return 1; } }

With OO exception handling, all you need to do is wrap the function call to func2() within the try block and handle the exceptions thrown from that block with an appropriate exception handler (catch block). The equivalent code with exception handling is shown below.

sub func1 { try { func2(); } catch IOException with { # Exception handling code here }; } sub func2 { func3(); ... } sub func3 { processFile($FILE); ... } sub processFile { my $file = shift; if (!open(FH, $file)) { throw IOException("Error opening file <$file> - $!"); } else { # Process the file return 1; } }

Since func1() is the only function to possess a catch block, the exception that was thrown in the processFile() function is propagated all the way up to func1(), where it would be appropriately dealt with, by the catch block. The difference in the bloat factor and code obfuscation level between these two error handling techniques is obvious.

Finally, exceptions can be used to group related errors. By doing this, you will be able to handle related exceptions using a single exception handler. The inheritance hierarchy of the exception classes can be used to logically group exceptions. Thus, an exception handler can catch exceptions of the class specified by its parameter, or can catch exceptions of any of its subclass.

Let's Do It in Perl

Perl's Built-In Exception Handling Mechanism

Perl has a built-in exception handling mechanism, a.k.a the eval {} block. It is implemented by wrapping the code that needs to be executed around an eval block and the $@ variable is checked to see if an exception occurred. The typical syntax is:

eval { ... }; if ($@) { errorHandler($@); }

Within the eval block, if there is a syntax error or runtime error, or a die statement is executed, then an undefined value is returned by eval, and $@ is set to the error message. If there was no error, then $@ is guaranteed to be a null string.

What's wrong with this? Since the error message store in $@ is a simple scalar, checking the type of error that has occurred is error prone. Also, $@ doesn't tell us where the exception occurred. To overcome these issues, exception objects were incorporated in Perl 5.005.

From Perl 5.005 onward, you can do this:

eval { open(FILE, $file) || die MyFileException->new("Unable to open file - $file"); }; if ($@) { # now $@ contains the exception object of type MyFileException print $@->getErrorMessage(); # where getErrorMessage() is a method in MyFileException class }

The exception class (MyFileException) can be built with as much functionality as desired. For example, you can get the calling context by using caller() in constructer of the exception class (typically MyFileException::new()).

It is also possible to test specific exception types as shown below:

eval { .... }; if ($@) { if ($@->isa('MyFileException')) { # Specific exception handler .... } else { # Generic exception handler .... } }

If the exception object implements stringification, by overloading the string operations, then the stringified version of the object would be available whenever $@ is used in string context. By constructing the overloading method appropriately, the value of $@ in string context can be tailored as desired.

package MyException; use overload ('""' => 'stringify'); ... ... sub stringify { my ($self) = @_; my $class = ref($self) || $self; return "$class Exception: " . $self->errMsg() . " at " . $self->lineNo() . " in " . $self->file(); # Assuming that errMsg(), lineNo() & file() are methods # in the exception class # to store & return error message, line number and source # file respectively. }

When overloading the string operator '"', the overloading method (stringify() in our case) is expected to return a string representing the stringified form of the object. The stringify() method can return various context/state information about the exception object, as part of the string.

Problems with eval

The following are some of the issues in using the eval {} construct:

  • Similar looking syntactic constructs can mean different things, based on the context.
  • eval blocks can be used for both, building dynamic code snippets as well as for exception handling
  • No builtin provision for cleanup handler a.k.a finally block
  • Stack trace if required, needs to maintained by writing custom code
  • Aesthetically unappealing (Although this is very subjective)

 

Error.pm to the Rescue

The Error.pm module implements OO exception handling. It mimics the try/catch/throw syntax available in other OO languages like Java and C++ (to name a few). It is also devoid of all the problems that are inherent when using eval. Since it's a pure perl module, it runs on almost all platforms where Perl runs.

Use'ing Error.pm

The module provides two interfaces:

  1. Procedural interface for exception handling (exception handling constructs)
  2. Base class for other exception classes

 

The module exports various functions to perform exception handling. They will be exported if the :try tag is used in the use statement.

A typical invocation would look like this:

use Error qw(:try); try { some code; code that might thrown an exception; more code; return; } catch Error with { my $ex = shift; # Get hold of the exception object handle the exception; } finally { cleanup code; }; # <-- Remember the semicolon

Don't forget to include the trailing semicolon (;) after the closing brace. For those of you who want to know why: All these functions accept a code reference as their first parameter. For example, in the try block the code that follows try is passed in as a code reference (anonymous function) to the function try().

try Block

An exception handler is constructed by enclosing the statements that are likely to throw an exception within a try block. If an exception occurs within the try block, then it is handled by the appropriate exception handler (catch block), associated with the try block. If no exceptions are thrown, then try will return the result of block.

The syntax is: try BLOCK EXCEPTION_HANDLERS

A try block should have at least one (or more) catch block(s) or one finally block.

 

Pages: 1, 2

 

Catch Block

The try block associates the scope of its associated exception handlers. You associate exception handlers with a try block by providing one or more catch blocks directly after the try block:

 

 

 

try { .... } catch IOException with { .... } catch MathException with { .... };

The syntax is: catch CLASS with BLOCK

This enables all errors that satisfy the condition $ex->isa(CLASS) to be handled by evaluating BLOCK.

The BLOCK receives two parameters. The first is the exception being thrown and the second is a scalar reference. If this scalar reference is set on return from the catch block, then the try block continues as if there was no exception.

If the scalar referenced by the second parameter is not set, and no exceptions are thrown (within the catch block), then the current try block will return with the result from the catch block.

In order to propagate an exception, the catch block can choose to rethrow the exception by calling $ex->throw()

Order of Catch Blocks Matter

The order of exception handlers is important. It's all the more critical if you have handlers at different levels in the inheritance hierarchy. Exception handlers that are built to handle exception types that are furthermost from the root of the hierarchy (Error) should be placed first in the list of catch blocks.

An exception handler designed to handle a specific type of object may be pre-empted by another handler whose exception type is a superclass of that type. This happens if the exception handler for that exception type appears earlier in the list of exception handlers.

For example:

try { my $result = $self->divide($value, 0); # divide() throws DivideByZeroException return $result; } catch MathException with { my $ex = shift; print "Error: Caught MathException occurred/n"; return; } catch DivideByZeroException with { my $ex = shift; print "Error: Caught DivideByZeroException/n"; return 0; };

Assuming the Inheritance hierarchy:

MathException is-a Error [ @MathException::ISA = qw(Error) ] DivideByZeroException is-a MathException [ @DivideByZeroException::ISA = qw(MathException) ]

In the above code listing, the DivideByZeroException is caught by the first catch block instead of the second. That is because DivideByZeroException is a subclass of MathException. In other words, $ex->isa('MathException') returns true. Hence, the exception is handled by the code within the first catch block. Reversing the order of the catch blocks would ensure that the exception is caught by the correct exception handler.

Finally Block

The final step in setting up an exception handler is providing a mechanism for cleaning up before control is passed to different part of the program. This can be achieved by enclosing the cleanup logic within the finally block. Code in the finally block is executed irrespective of what happens within the try block. Typical use of the finally block is to close files or in general to release any system resource.

If no exceptions are thrown, then none of the code in the catch block(s) gets executed. But the code in the finally block is always executed.

If an exception is thrown, then code in the appropriate catch block is executed. Once the execution of that code is complete, the finally block is executed.

try { my $file = join('.', '/tmp/tempfile', $$); my $fh = new FileHandle($file, 'w'); throw IOException("Unable to open file - $!") if (!$fh); # some code that might throw an exception return; } catch Error with { my $ex = shift; # Exception handling code } finally { close($fh) if ($fh); # Close the temporary file unlink($file); # Delete the temporary file };

In the above code listing, a temporary file is created in the try block and the block also has some code that can potentially throw an exception. Irrespective of whether the try block succeeds, the temporary file has to be closed and deleted from the file system. This is accomplished by closing and deleting the file in the finally block.

Remember, only one finally block is allowed per try block.

Throw Statement

throw() creates a new "Error" object and throws an exception. This exception would be caught by a surrounding try block, if there is one. Otherwise the program will exit.

throw() can also be called on an existing exception to rethrow it. The code listing below illustrates how to rethrow an exception:

try { $self->openFile(); $self->processFile(); $self->closeFile(); } catch IOException with { my $ex = shift; if (!$self->raiseException()) { warn("IOException occurred - " . $ex->getMessage()); return; } else { $ex->throw(); # Re-throwing exception } };

Building Your Own Exception Class

Setting the value of the $Error::Debug package variable to true, enables the capturing of stack trace for later retrieval using the stacktrace() method. (In case you are not familiar, stacktrace is a list of all the methods executed in sequence that lead to the exception).

The code snippet below creates the exception classes MathException, DivideByZero and OverFlowException. Where the latter two are subclasses of MathException and MathException by itself is derived from Error.pm

package MathException; use base qw(Error); use overload ('""' => 'stringify'); sub new { my $self = shift; my $text = "" . shift; my @args = (); local $Error::Depth = $Error::Depth + 1; local $Error::Debug = 1; # Enables storing of stacktrace $self->SUPER::new(-text => $text, @args); } 1; package DivideByZeroException; use base qw(MathException); 1; package OverFlowException; use base qw(MathException); 1;

And More ...

The error modules has other special exception handling blocks, such as except and otherwise. They are deliberately not covered here because they are specific to Error.pm, and you won't find them in other OO languages. For those who are interested, please refer the POD documentation that is embedded within Error.pm.

Fatal.pm

If you have some functions that return false on error and a true value on success, then you can use Fatal.pm to convert them into functions that throw exceptions on failure. It is possible to do this to both user defined functions as well as built-in functions (with some exceptions).

use Fatal qw(open close); eval { open(FH, "invalidfile") }; if ($@) { warn("Error opening file: $@/n"); } .... .... eval { close(FH); }; warn($@) if ($@);

By default, Fatal.pm catches every use of the fatalized functions i.e.

use Fatal qw(chdir); if (chdir("/tmp/tmp/")) { .... } else { # Execution flow never reaches here }

If you are fortunate enough to have Perl 5.6 or later, then you can circumvent this by adding :void in the import list. All functions named after this in that import list will raise an exception only when they are called in void context i.e. when their return values are being ignored.

By changing the use statement, as shown below we can be sure that the code in the else block is executed when chdir() fails

use Fatal qw(:void chdir);

The code listing below illustrates the use of Fatal.pm in conjunction with Error.pm.

use Error qw(:try); use Fatal qw(:void open); try { open(FH, $file); .... openDBConnection($dsn); return; } catch DBConnectionException with { my $ex = shift; # Database connection failed } catch Error with { my $ex = shift; # If the open() fails, then we'll be here };

The Perl6 Connection

Since the exception handling syntax in Perl 6 is expected to be modeled closely against Error.pm as detailed in the Perl 6 RFC 63 Exception handling syntax - http://dev.perl.org/rfc/63.pod, also in RFC 80 and RFC 88. It would make sense for developers to make use of OO-ish exception handling capabilities in their code and later on migrate to the exception handling syntax in Perl 6, as and when it is available.

Conclusion

The following are some of the key reasons to choose exception-handling mechanism over the traditional error-handling mechanisms:

  • Error handling code can be separated from normal code
  • Less complex, more readable and more efficient code
  • Ability to propagating errors up the call stack
  • Ability to persist contextual information for the exception handler
  • Logical grouping of error types

 

So, stop returning error codes and start throwing exceptions.

Have an exceptional time !!

rtw89 A repo for the newest Realtek rtw89 codes. This repo now contains the code for the Realtek RTW8922AE, which is a Wifi 7 device. It has been tested using a Wifi 6 AP as I do not have access to a Wifi 7 model. The driver works very well. This repo is current with rtw-next up to April 3, 2024. This branch was created from the version merged into the wireless-next repo, which is in the 5.16 kernel. IF YOU USE DRIVERS FROM THIS REPO FOR KERNELS 5.16+, YOU MUST BLACKLIST THE KERNEL VERSIONS!!!! FAILING TO DO THIS WILL RESULT IN ALL MANNER OF STRANGE ERRORS. This code will build on any kernel 6.10 and newer as long as the distro has not modified any of the kernel APIs. IF YOU RUN UBUNTU, YOU CAN BE ASSURED THAT THE APIs HAVE CHANGED. NO, I WILL NOT MODIFY THE SOURCE FOR YOU. YOU ARE ON YOUR OWN!!!!! Note that if you use this driver on kernels older than 5.15, the enhanced features of wifi 5 and wifi 6 are greatly crippled as the kernel does hot have the capability to support the new packet widths and speeds. If you use such a kernel, you might as well have an 802.11n (wifi 4) device. This repository includes drivers for the following cards: Realtek 8851BE, 8852AE, 8852BE, 8852CE, and 8922AE. If you are looking for a driver for chips such as RTL8188EE, RTL8192CE, RTL8192CU, RTL8192DE, RTL8192EE, RTL8192SE, RTL8723AE, or RTL8723BE, these should be provided by your kernel. If not, then you should go to the Backports Project (https://backports.wiki.kernel.org/index.php/Main_Page) to obtain the necessary code. If you have an RTW8822B{E,U,S}, RTW8822C{E,U,S}, RTW8723D{E,U,S}, or RTW8821C{E,U,S}, then you should use the drivers at https://github.com/lwfinger/rtw88.git. Installation instruction Requirements You will need to install "make", "gcc", "kernel headers", "kernel build essentials", and "git". For Ubuntu: You can install them with the following command sudo apt-get update sudo apt-get install make gcc linux-headers-$(uname -r) build-essential git Users of Debian, Ubuntu, and similar (Mint etc) may want to scroll down and follow the DKMS instructions at the end of this document instead. For Fedora: You can install them with the following command sudo dnf install kernel-headers kernel-devel sudo dnf group install "C Development Tools and Libraries" For openSUSE: Install necessary headers with sudo zypper install make gcc kernel-devel kernel-default-devel git libopenssl-devel For Arch: After installing the necessary kernel headers and base-devel, git clone https://aur.archlinux.org/rtw89-dkms-git.git cd rtw89-dkms-git makepkg -sri If any of the packages above are not found check if your distro installs them like that. Installation For all distros: git clone https://github.com/lwfinger/rtw89.git cd rtw89 make sudo make install Installation with module signing for SecureBoot For all distros: git clone https://github.com/lwfinger/rtw89.git cd rtw89 make sudo make sign-install You will be prompted with a password, please keep it in mind and use it in the next steps. Reboot to activate the new installed module. In the MOK management screen: Select "Enroll key" and enroll the key created by above sign-install step When prompted, enter the password you entered when create sign key. If you enter wrong password, your computer won't be bootable. In this case, use the BOOT menu from your BIOS, to boot into your OS then do below steps: sudo mokutil --reset Restart your computer Use BOOT menu from BIOS to boot into your OS In the MOK management screen, select reset MOK list Reboot then retry from the step to make sign-install How to unload/reload a Kernel module sudo modprobe -rv rtw_8852ae sudo modprobe -rv rtw89core #These two statements unload the module Due to the behavior of the modprobe utility, it takes both to unload. sudo modprobe -v rtw_8852ae #This loads the module A single modprobe call will reload the module. Uninstall drivers For all distros: sudo make uninstall Problem with recovery after sleep or hibernation Some BIOSs have trouble changing the power state from D3hot to D0. If you have this problem, then sudo cp suspend_rtw89 /usr/lib/systemd/system-sleep/. That script will unload the driver before sleep or hibernation, and reload it following resumption. Option configuration IMPORTANT: If you have an HP or Lenovo laptop, Their BIOS does not handle the PCIe interface correctly. To compensate, run the following command: sudo cp 70-rtw89.conf /etc/modprobe.d/. Then unload the drivers and reload. You should see the options appended to the end of the rtw89_pci or rtw89pci load line. If it turns out that your system needs one of the other configuration options, then do the following: sudo nano /etc/modprobe.d/<dev_name>.conf There, enter the line below: options <driver_name> <<driver_option_name>>=<value> The available options for rtw89pci are disable_clkreq, disable_aspm_l1, and disable_aspm_l1ss. The available options for rtw89core are debug_mask, and disable_ps_mode. If after rebooting the wifi still doesn't work, it might mean that it was not loaded. To fix that, you will have to manually rebuild initramfs. To do that, execute one of the two commands, depending on how old/new your system is. mkinitrd # If you're running an older system dracut -f --regenerate-all # If you're running a newer system After rebuilding initramfs, reboot your computer and check if the wifi works properly now. Normally, none of these will be needed; however, if you are getting firmware errors, one or both of the disable_aspm_* options may help. They are needed when a buggy BIOS fails to implement the PCI specs correctly. When your kernel changes, then you need to do the following: cd ~/rtw89 git pull make clean make sudo make install ;or sudo make sign-install Remember, this MUST be done whenever you get a new kernel - no exceptions. These drivers will not build for kernels older than 5.8. If you must use an older kernel, submit a GitHub issue with a listing of the build errors, but be aware that doing so will cripple your device. Without the errors, the issue will be ignored. I am not a mind reader. When you have problems where the driver builds and loads correctly, but fails to work, a GitHub issue is NOT the best place to report it. I have no idea of the internal workings of any of the chips, and the Realtek engineers who do will not read these issues. To reach them, send E-mail to linux-wireless@vger.kernel.org. Include a detailed description of any messages in the kernel logs and any steps that you have taken to analyze or fix the problem. If your description is not complete, you are unlikely to get any satisfaction. One other thing - your mail MUST be plain test. HTML mail is rejected. DKMS packaging for debian and derivatives DKMS is commonly used on debian and derivatives, like ubuntu, to streamline building extra kernel modules. By following the instructions below and installing the resulting package, the rtw89 driver will automatically rebuild on kernel updates. Secure boot signing will happen automatically as well, as long as the dkms signing key (usually located at /var/lib/dkms/mok.key) is enrolled. See your distro's secure boot documentation for more details. Prerequisites: sudo apt install dh-sequence-dkms debhelper build-essential devscripts git-build-recipe This workflow uses devscripts, which has quite a few perl dependencies. You may wish to build inside a chroot to avoid unnecessary clutter on your system. The debian wiki page for chroot has simple instructions for debian, which you can adapt to other distros as needed by changing the release codename and mirror url. If you do, make sure to install the package on your host system, as it will fail if you try to install inside the chroot. Build and installation # If you've already built as above clean up your workspace or check one out specially (otherwise some temp files can end up in your package) git clean -xfd git deborig HEAD dpkg-buildpackage -us -uc sudo apt install ../rtw89-dkms_1.0.2-3_all.deb This will install the package, and build the module for your currently active kernel. You should then be able to modprobe as above. It will also load automatically on boot. A note regarding firmware Firmware from userspace is required to use this driver. This package will attempt to pull the firmware in automatically as a Recommends. However, if your distro does not provide one of firmware-realtek >= 20230117-1 or linux-firmware >= 20220329.git681281e4-0ubuntu3.10, the driver will fail to load, and dmesg will show an error about a specific missing firmware file. In this case, you can download the firmware files directly from https://git.kernel.org/pub/scm/linux/kernel/git/firmware/linux-firmware.git/tree/rtw89. 将上述内容翻译成中文
09-25
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值