Ruby - Ensure exception handler

Introduction

The following code uses ensure clause to do default action in case of exception.

Demo

startdir = Dir.getwd

begin# from  w w w . jav  a  2  s. c  o m
   Dir.chdir( "X:\\" )
   puts( `dir` )
rescue Exception => e
   puts e.class
   puts e
ensure
   Dir.chdir( startdir )
end

Result

Related Topic