1. Perl
  2. reading material

I thought about why Perl doesn't really need exception handling finally

Perl uses die to raise an exception. This corresponds to throw in the Java language. Use eval to catch the exception. This is equivalent to catch in the Java language. Whether or not an exception has occurred can be determined by whether a message is set in $@.

# Perl exception handling

eval {
  # Exception-raising process
}

if ($@) {
  
}

As you know, Perl doesn't have finally statement. But I don't hear stories that are particularly troublesome. Why. In most cases, the processing performed by finally statement is the release of external resources. It is the process of closing a file, deleting a temporary file, and releasing the connection to the DB. There is a desire to execute such processing with finally statement.

Why is finally statement required in Java? This is because in Java, GC uses a mechanism called "generational GC", so the programmer cannot know when to release an object. In Java, the finalizer should not release resources. So finaly statement is mandatory.

Perl's GC uses a simple reference counting method to ensure that the destructor is executed when the object is released. I feel that finally statement is not a required feature because I can write the logic for releasing resources in this position.

Also, ruby has a GC mark-and-sweep, so it should be designed so that destructors are not used. Therefore, finally statement (eusure statement in Ruby) is a necessary function.

I realize that just because Perl doesn't have finally statement doesn't mean there's a big inconvenience. So Perl's exception handling is useless, and the idea that exception handling that implements finally is great doesn't seem to be a valid evaluation.

Related Informatrion