SlideShare une entreprise Scribd logo
1  sur  17
Deepak H B
Full-Stack Developer
Gmail: deepakhb2@gmail.com
Linkedin: http://in.linkedin.com/in/deepakhb
GitHub: https://github.com/deepakhb2
Exception Handling
Introduction
• An exception is an object that represents some kind of exceptional
conditions, it indicates hat sometime has gone wrong.
– This could be a programming error attempting to divide by zero.
– Attempting to invoke a method on an object that define an method or
passing an invalid argument to a method.
– It could be result of an some king of external condition, making
network request when the network down or trying to create an object
when the system is out of memory.
• When one of these errors or conditions occurs an exception is
raised.
• By default, Ruby programs terminate when an exception occurs.
• But it is possible to declare exception handlers. An exception
handler is a block of code that is executed if an exception occurs
during the execution of some other block of code.
• Raising an exception transfers the flow of control to exception
handling code.
• Ruby uses the Kernel method raise to raise exceptions and uses
rescue clause to handle exceptions.
• Exceptions raised by raise are instances of the Exception class or
one of its many subclasses.
Exception Classes and Exception Objects
• Exception objects are instance of the Exception class or one of
its subclass.
Object
+--Exception
+--NoMemoryError
+--ScriptError
| +--LoadError
| +--NotImplementedError
| +--SyntaxError
+--SecurityError # Was a StandardError in 1.8
+--SignalException
| +--Interrupt
+--SystemExit
+--SystemStackError # Was a StandardError in 1.8
+--StandardError
+--ArgumentError
Contd..
+--FiberError # New in 1.9
+--IOError
| +--EOFError
+--IndexError
| +--KeyError # New in 1.9
| +--StopIteration # New in 1.9
+--LocalJumpError
+--NameError
| +--NoMethodError
+--RangeError
| +--FloatDomainError
+--RegexpError
+--RuntimeError
+--SystemCallError
+--ThreadError
+--TypeError
+--ZeroDivisionError
Contd..
• You don’t need to be familiar with each of these
exception subclass.
• The most of these subclass extend a class known as
StandardError.
• These are the “normal” exceptions that typical Ruby
programs try to handle.
• Most of them are undocumented because, most of them
add no new methods to those defined by the base
Exception class.
Methods of exception objects
• The exception class defines two methods that return details
about the exception.
• The message method returns a string that may provide
human readable details about what went wrong.
• The backtrace method returns an array of strings that
represents the call stack at the point the exception was raised.
• Exception objects are typically created by the raise method.
When this is done, the raise method sets the stack trace of
the exception appropriately.
• The set_backtrace method is used to set the backtrace for the
custom exceptions.
Defining new exceptions
• If you are defining a module a Ruby code, it is often
appropriate to define your own subclass of StandardError for
exception that are specific to your module.
• This may be a trivial, one-line subclass
Class MyError < StandardError; end
Raising Exceptions with raise
• The Kernel method raise raises an exception.
• There are several ways to invoke raise
– If raise is called with no arguments, it creates a new RuntimeError object and
raises it.
– If raise is called with a single Exception object as its argument, it raises that
exception.
– If raise is called with a single string argument, it creates a new RuntimeError
exception object, with the specified string as its message, and raises that
exception.
– If the first argument to raise is an object that has an exception method, then
raise invokes that method and raises the Exception object that it returns.
def factorial(n)
raise “Bad argument” if n<1
return 1 if n ==1
n*factorial(n-1)
end
– The other ways to raise exception
raise RuntimeError, “Bad argument” if n<1
raise RuntimeError.new(“Bad argument”) if n<1
raise RuntimeError.exception(“Bad argument”) if n<1
Contd..
• ArgumentError is more appropriate than RuntimeError and
more detailed error message would be helpful.
raise ArgumentError, “Exception argument >=1. Got #{n}} if n<1
• The intent of the exception we’re raising here is to point
out a problem with the invocation of the factorial method,
not with the code inside the method.
• The caller method of kernel is used as third argument to
provide a custom stack trace.
raise ArgumentError, “Exception argument >= 1. Got #{n}”, caller
Handling Exceptions with rescue
• A rescue clause, by contrast, is a fundamental part of the
Ruby language.
• rescue is not a statement in its own right, but rather a
clause that can be attached to other Ruby statements.
• Most commonly, a rescue clause is attached to a begin
statement.
• A begin statement with a rescue clause looks like this:
begin
#code
rescue
#Exception handling code goes here
End
Naming the exception
• In rescue clause, the global variable $! Refers to the Exception
object that is being handled.
• If your program includes the line:
require ‘English’, then you can use the global variable $ERROR_INFO
instead.
• A better alternative to $! or $ERROR_INFO is to specify a variable
name for the exception object in the rescue clause itself:
Rescue => ex
• The statements of this rescue clause can now use the ex to refer to
Exception object that describes the exception.
• The rescue clause does not define a new variable scope and a
variable named in the rescue clause is visible even after the end of
the rescue clause.
Handling exceptions by type
• The rescue clauses shown here handled any exception that is a
StandardError and ignore any exception object that is not a
StandardError.
• To handle non standard exceptions outside the StandardError
hierarchy or to handle only specific types of exceptions, you must
include one or more exception classes in the rescue clause.
rescue Exception
• To handle an ArgumentError and assign the exception object to the
variable e:
rescue ArgumentError => e
rescue ArgumentError, TypeError => error
• The rescue keyword is followed by zero or more comma-separated
expressions, each of which must evaluate to a class object that
represents the Expression class or a subclass.
Contd..
• We might use a case statement to run different code based on
the class of the exception object.
Begin
x=factorial(n)
rescue ArgumentError => ex
puts “Try again with value >= 1”
rescue TypeErrror => ex
puts “Try again with an integer”
end
• Ruby interpreter attempts to match exceptions to rescue
clauses in the order they are written.
Retry in a rescue clause
• When the retry statement is used within a rescue clause, it returns the
block of code to which the rescue is attached.
• When an exception is caused by a transient failure, such as an overloaded
server, it might make sense to handle the exception by simply trying again.
• retry is not suitable handling technique for these exceptions.
require ‘open-uri’
tries = 0
begin
tries += 1
open(http://www.example.com)
rescue OpenURI::HTTPError => e
puts e.message
if(tries<4)
sleep(2**tries)
retry
end
end
The else Clause
• A begin statement may include an else clause after its
rescue clauses.
• You might guess that the else clause is a catch-all rescue
that it handles any exception that does not match a
previous rescue claues.
• The code in the else clause is executed if the code in the
body of the begin statement runs to completion without
exceptions.
• The exception raised by the else clause are not handled by
the rescue statements.
The ensure Clause
• The optional ensure clause, if it appears, must come after all rescue and
else clasues.
• It may also be used by itself without any rescue or else clause.
• The ensure clause contains code that always runs, no matter what
happens with the code following begin.
– If the code runs to completion, then control jumps to the else clause, if there
is one and then to the ensure clause.
– If the code executes a return statement, then the execution skips the else
clause and jumps directly to the ensure clause before returning.
– If the following code begin raises an exception, then control jumps to the
appropriate rescue clause, and then to the ensure clause.
– If there is no rescue clause, or if no rescue clause can handle the exception,
then control jumps directly to the ensure clause.
• The purpose of ensure clause is to ensure that housekeeping details such
as closing files, disconnecting database connections, and committing or
aborting transactions get taken care.

Contenu connexe

Tendances

Exception Handling
Exception HandlingException Handling
Exception Handling
Alpesh Oza
 
Latest C Interview Questions and Answers
Latest C Interview Questions and AnswersLatest C Interview Questions and Answers
Latest C Interview Questions and Answers
DaisyWatson5
 
Method Shelters : Another Way to Resolve Class Extension Conflicts
Method Shelters : Another Way to Resolve Class Extension Conflicts Method Shelters : Another Way to Resolve Class Extension Conflicts
Method Shelters : Another Way to Resolve Class Extension Conflicts
S Akai
 
Exception handling chapter15
Exception handling chapter15Exception handling chapter15
Exception handling chapter15
Kumar
 
Exception Handling Mechanism in .NET CLR
Exception Handling Mechanism in .NET CLRException Handling Mechanism in .NET CLR
Exception Handling Mechanism in .NET CLR
Kiran Munir
 
Python Programming - X. Exception Handling and Assertions
Python Programming - X. Exception Handling and AssertionsPython Programming - X. Exception Handling and Assertions
Python Programming - X. Exception Handling and Assertions
Ranel Padon
 

Tendances (20)

130410107010 exception handling
130410107010 exception handling130410107010 exception handling
130410107010 exception handling
 
What is Exception Handling?
What is Exception Handling?What is Exception Handling?
What is Exception Handling?
 
Exception Handling
Exception HandlingException Handling
Exception Handling
 
Exception handling in c programming
Exception handling in c programmingException handling in c programming
Exception handling in c programming
 
Exceptions in c++
Exceptions in c++Exceptions in c++
Exceptions in c++
 
Java exception handling
Java exception handlingJava exception handling
Java exception handling
 
Exception handling c++
Exception handling c++Exception handling c++
Exception handling c++
 
Latest C Interview Questions and Answers
Latest C Interview Questions and AnswersLatest C Interview Questions and Answers
Latest C Interview Questions and Answers
 
Method Shelters : Another Way to Resolve Class Extension Conflicts
Method Shelters : Another Way to Resolve Class Extension Conflicts Method Shelters : Another Way to Resolve Class Extension Conflicts
Method Shelters : Another Way to Resolve Class Extension Conflicts
 
Exception handling chapter15
Exception handling chapter15Exception handling chapter15
Exception handling chapter15
 
Exception Handling in the C++ Constructor
Exception Handling in the C++ ConstructorException Handling in the C++ Constructor
Exception Handling in the C++ Constructor
 
Exception Handling Mechanism in .NET CLR
Exception Handling Mechanism in .NET CLRException Handling Mechanism in .NET CLR
Exception Handling Mechanism in .NET CLR
 
F6dc1 session6 c++
F6dc1 session6 c++F6dc1 session6 c++
F6dc1 session6 c++
 
Exception handlingpdf
Exception handlingpdfException handlingpdf
Exception handlingpdf
 
Exception Handling in object oriented programming using C++
Exception Handling in object oriented programming using C++Exception Handling in object oriented programming using C++
Exception Handling in object oriented programming using C++
 
Python Programming - X. Exception Handling and Assertions
Python Programming - X. Exception Handling and AssertionsPython Programming - X. Exception Handling and Assertions
Python Programming - X. Exception Handling and Assertions
 
Exception handling in c++
Exception handling in c++Exception handling in c++
Exception handling in c++
 
Exceptions overview
Exceptions overviewExceptions overview
Exceptions overview
 
OpenMP and static code analysis
OpenMP and static code analysisOpenMP and static code analysis
OpenMP and static code analysis
 
Exception Handling
Exception HandlingException Handling
Exception Handling
 

Similaire à 8 Exception Handling

Exception Handling Exception Handling Exception Handling
Exception Handling Exception Handling Exception HandlingException Handling Exception Handling Exception Handling
Exception Handling Exception Handling Exception Handling
AboMohammad10
 
Ch-1_5.pdf this is java tutorials for all
Ch-1_5.pdf this is java tutorials for allCh-1_5.pdf this is java tutorials for all
Ch-1_5.pdf this is java tutorials for all
HayomeTakele
 
L12.2 Exception handling.pdf
L12.2  Exception handling.pdfL12.2  Exception handling.pdf
L12.2 Exception handling.pdf
MaddalaSeshu
 

Similaire à 8 Exception Handling (20)

Exception handling in java
Exception handling in javaException handling in java
Exception handling in java
 
Exceptions in java
Exceptions in javaExceptions in java
Exceptions in java
 
Exception handling in java.pptx
Exception handling in java.pptxException handling in java.pptx
Exception handling in java.pptx
 
Exceptionhandling
ExceptionhandlingExceptionhandling
Exceptionhandling
 
ACP - Week - 9.pptx
ACP - Week - 9.pptxACP - Week - 9.pptx
ACP - Week - 9.pptx
 
Chap2 exception handling
Chap2 exception handlingChap2 exception handling
Chap2 exception handling
 
Java-Unit 3- Chap2 exception handling
Java-Unit 3- Chap2 exception handlingJava-Unit 3- Chap2 exception handling
Java-Unit 3- Chap2 exception handling
 
Exception Handling Exception Handling Exception Handling
Exception Handling Exception Handling Exception HandlingException Handling Exception Handling Exception Handling
Exception Handling Exception Handling Exception Handling
 
UNIT III 2021R.pptx
UNIT III 2021R.pptxUNIT III 2021R.pptx
UNIT III 2021R.pptx
 
UNIT III 2021R.pptx
UNIT III 2021R.pptxUNIT III 2021R.pptx
UNIT III 2021R.pptx
 
Java Exception.ppt
Java Exception.pptJava Exception.ppt
Java Exception.ppt
 
Exception handling
Exception handlingException handling
Exception handling
 
Exception hierarchy
Exception hierarchyException hierarchy
Exception hierarchy
 
Ch-1_5.pdf this is java tutorials for all
Ch-1_5.pdf this is java tutorials for allCh-1_5.pdf this is java tutorials for all
Ch-1_5.pdf this is java tutorials for all
 
L12.2 Exception handling.pdf
L12.2  Exception handling.pdfL12.2  Exception handling.pdf
L12.2 Exception handling.pdf
 
Pi j4.2 software-reliability
Pi j4.2 software-reliabilityPi j4.2 software-reliability
Pi j4.2 software-reliability
 
16 exception handling - i
16 exception handling - i16 exception handling - i
16 exception handling - i
 
Exception Handling in Java
Exception Handling in JavaException Handling in Java
Exception Handling in Java
 
06 exceptions
06 exceptions06 exceptions
06 exceptions
 
Exceptions
ExceptionsExceptions
Exceptions
 

Plus de Deepak Hagadur Bheemaraju (10)

12 Introduction to Rails
12 Introduction to Rails12 Introduction to Rails
12 Introduction to Rails
 
11 Ruby Gems
11 Ruby Gems11 Ruby Gems
11 Ruby Gems
 
10 Networking
10 Networking10 Networking
10 Networking
 
9 Inputs & Outputs
9 Inputs & Outputs9 Inputs & Outputs
9 Inputs & Outputs
 
7 Methods and Functional Programming
7  Methods and Functional Programming7  Methods and Functional Programming
7 Methods and Functional Programming
 
6 Object Oriented Programming
6 Object Oriented Programming6 Object Oriented Programming
6 Object Oriented Programming
 
5 Statements and Control Structures
5 Statements and Control Structures5 Statements and Control Structures
5 Statements and Control Structures
 
4 Expressions and Operators
4  Expressions and Operators4  Expressions and Operators
4 Expressions and Operators
 
3 Datatypes
3 Datatypes3 Datatypes
3 Datatypes
 
2 Basics
2 Basics2 Basics
2 Basics
 

Dernier

The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 

Dernier (20)

Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 

8 Exception Handling

  • 1. Deepak H B Full-Stack Developer Gmail: deepakhb2@gmail.com Linkedin: http://in.linkedin.com/in/deepakhb GitHub: https://github.com/deepakhb2
  • 3. Introduction • An exception is an object that represents some kind of exceptional conditions, it indicates hat sometime has gone wrong. – This could be a programming error attempting to divide by zero. – Attempting to invoke a method on an object that define an method or passing an invalid argument to a method. – It could be result of an some king of external condition, making network request when the network down or trying to create an object when the system is out of memory. • When one of these errors or conditions occurs an exception is raised. • By default, Ruby programs terminate when an exception occurs. • But it is possible to declare exception handlers. An exception handler is a block of code that is executed if an exception occurs during the execution of some other block of code. • Raising an exception transfers the flow of control to exception handling code. • Ruby uses the Kernel method raise to raise exceptions and uses rescue clause to handle exceptions. • Exceptions raised by raise are instances of the Exception class or one of its many subclasses.
  • 4. Exception Classes and Exception Objects • Exception objects are instance of the Exception class or one of its subclass. Object +--Exception +--NoMemoryError +--ScriptError | +--LoadError | +--NotImplementedError | +--SyntaxError +--SecurityError # Was a StandardError in 1.8 +--SignalException | +--Interrupt +--SystemExit +--SystemStackError # Was a StandardError in 1.8 +--StandardError +--ArgumentError
  • 5. Contd.. +--FiberError # New in 1.9 +--IOError | +--EOFError +--IndexError | +--KeyError # New in 1.9 | +--StopIteration # New in 1.9 +--LocalJumpError +--NameError | +--NoMethodError +--RangeError | +--FloatDomainError +--RegexpError +--RuntimeError +--SystemCallError +--ThreadError +--TypeError +--ZeroDivisionError
  • 6. Contd.. • You don’t need to be familiar with each of these exception subclass. • The most of these subclass extend a class known as StandardError. • These are the “normal” exceptions that typical Ruby programs try to handle. • Most of them are undocumented because, most of them add no new methods to those defined by the base Exception class.
  • 7. Methods of exception objects • The exception class defines two methods that return details about the exception. • The message method returns a string that may provide human readable details about what went wrong. • The backtrace method returns an array of strings that represents the call stack at the point the exception was raised. • Exception objects are typically created by the raise method. When this is done, the raise method sets the stack trace of the exception appropriately. • The set_backtrace method is used to set the backtrace for the custom exceptions.
  • 8. Defining new exceptions • If you are defining a module a Ruby code, it is often appropriate to define your own subclass of StandardError for exception that are specific to your module. • This may be a trivial, one-line subclass Class MyError < StandardError; end
  • 9. Raising Exceptions with raise • The Kernel method raise raises an exception. • There are several ways to invoke raise – If raise is called with no arguments, it creates a new RuntimeError object and raises it. – If raise is called with a single Exception object as its argument, it raises that exception. – If raise is called with a single string argument, it creates a new RuntimeError exception object, with the specified string as its message, and raises that exception. – If the first argument to raise is an object that has an exception method, then raise invokes that method and raises the Exception object that it returns. def factorial(n) raise “Bad argument” if n<1 return 1 if n ==1 n*factorial(n-1) end – The other ways to raise exception raise RuntimeError, “Bad argument” if n<1 raise RuntimeError.new(“Bad argument”) if n<1 raise RuntimeError.exception(“Bad argument”) if n<1
  • 10. Contd.. • ArgumentError is more appropriate than RuntimeError and more detailed error message would be helpful. raise ArgumentError, “Exception argument >=1. Got #{n}} if n<1 • The intent of the exception we’re raising here is to point out a problem with the invocation of the factorial method, not with the code inside the method. • The caller method of kernel is used as third argument to provide a custom stack trace. raise ArgumentError, “Exception argument >= 1. Got #{n}”, caller
  • 11. Handling Exceptions with rescue • A rescue clause, by contrast, is a fundamental part of the Ruby language. • rescue is not a statement in its own right, but rather a clause that can be attached to other Ruby statements. • Most commonly, a rescue clause is attached to a begin statement. • A begin statement with a rescue clause looks like this: begin #code rescue #Exception handling code goes here End
  • 12. Naming the exception • In rescue clause, the global variable $! Refers to the Exception object that is being handled. • If your program includes the line: require ‘English’, then you can use the global variable $ERROR_INFO instead. • A better alternative to $! or $ERROR_INFO is to specify a variable name for the exception object in the rescue clause itself: Rescue => ex • The statements of this rescue clause can now use the ex to refer to Exception object that describes the exception. • The rescue clause does not define a new variable scope and a variable named in the rescue clause is visible even after the end of the rescue clause.
  • 13. Handling exceptions by type • The rescue clauses shown here handled any exception that is a StandardError and ignore any exception object that is not a StandardError. • To handle non standard exceptions outside the StandardError hierarchy or to handle only specific types of exceptions, you must include one or more exception classes in the rescue clause. rescue Exception • To handle an ArgumentError and assign the exception object to the variable e: rescue ArgumentError => e rescue ArgumentError, TypeError => error • The rescue keyword is followed by zero or more comma-separated expressions, each of which must evaluate to a class object that represents the Expression class or a subclass.
  • 14. Contd.. • We might use a case statement to run different code based on the class of the exception object. Begin x=factorial(n) rescue ArgumentError => ex puts “Try again with value >= 1” rescue TypeErrror => ex puts “Try again with an integer” end • Ruby interpreter attempts to match exceptions to rescue clauses in the order they are written.
  • 15. Retry in a rescue clause • When the retry statement is used within a rescue clause, it returns the block of code to which the rescue is attached. • When an exception is caused by a transient failure, such as an overloaded server, it might make sense to handle the exception by simply trying again. • retry is not suitable handling technique for these exceptions. require ‘open-uri’ tries = 0 begin tries += 1 open(http://www.example.com) rescue OpenURI::HTTPError => e puts e.message if(tries<4) sleep(2**tries) retry end end
  • 16. The else Clause • A begin statement may include an else clause after its rescue clauses. • You might guess that the else clause is a catch-all rescue that it handles any exception that does not match a previous rescue claues. • The code in the else clause is executed if the code in the body of the begin statement runs to completion without exceptions. • The exception raised by the else clause are not handled by the rescue statements.
  • 17. The ensure Clause • The optional ensure clause, if it appears, must come after all rescue and else clasues. • It may also be used by itself without any rescue or else clause. • The ensure clause contains code that always runs, no matter what happens with the code following begin. – If the code runs to completion, then control jumps to the else clause, if there is one and then to the ensure clause. – If the code executes a return statement, then the execution skips the else clause and jumps directly to the ensure clause before returning. – If the following code begin raises an exception, then control jumps to the appropriate rescue clause, and then to the ensure clause. – If there is no rescue clause, or if no rescue clause can handle the exception, then control jumps directly to the ensure clause. • The purpose of ensure clause is to ensure that housekeeping details such as closing files, disconnecting database connections, and committing or aborting transactions get taken care.