similar concepts, different syntax

Slides:



Advertisements
Similar presentations
Chapter 17 Failures and exceptions. This chapter discusses n Failure. n The meaning of system failure. n Causes of failure. n Handling failure. n Exception.
Advertisements

More about Ruby Maciej Mensfeld Presented by: Maciej Mensfeld More about Ruby dev.mensfeld.pl github.com/mensfeld.
Chapter 2.2 – More about Ruby Maciej Mensfeld Presented by: Maciej Mensfeld More about Ruby dev.mensfeld.pl github.com/mensfeld senior.
Exceptions Don’t Frustrate Your User – Handle Errors KR – CS 1401 Spring 2005 Picture – sysprog.net.
Exception Handling Chapter 15 2 What You Will Learn Use try, throw, catch to watch for indicate exceptions handle How to process exceptions and failures.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 15: Exception Handling.
COMP 121 Week 5: Exceptions and Exception Handling.
Chapter 16: Exception Handling C++ Programming: From Problem Analysis to Program Design, Fifth Edition.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 16: Exception Handling.
1 Lecture 11 Interfaces and Exception Handling from Chapters 9 and 10.
Exception Handling.  What are errors?  What does exception handling allow us to do?  Where are exceptions handled?  What does exception handling facilitate?
Exceptions in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Exception Error handling. Exception 4 n An unusual occurrence during program execution that requires immediate handling n Errors are the most common type.
Exceptions. Many problems in code are handled when the code is compiled, but not all Some are impossible to catch before the program is run  Must run.
CS203 Java Object Oriented Programming Errors and Exception Handling.
And other languages…. must remember to check return value OR, must pass label/exception handler to every function Caller Function return status Caller.
WEEK EXCEPTION HANDLING. Syntax Errors Syntax errors, also known as parsing errors, are perhaps the most common kind of complaint you get while.
17. Python Exceptions Handling Python provides two very important features to handle any unexpected error in your Python programs and to add debugging.
Exceptions Handling Exceptionally Sticky Problems.
Introduction to Exception Handling and Defensive Programming.
Chapter 24 Exception CSC1310 Fall Exceptions Exceptions Exceptions are events that can modify the flow or control through a program. They are automatically.
Chapter 14: Exception Handling. Objectives In this chapter, you will: – Learn what an exception is – Learn how to handle exceptions within a program –
Exceptions and Assertions Chapter 15 – CSCI 1302.
Chapter 15: Exception Handling C++ Programming: Program Design Including Data Structures, Fifth Edition.
(c) University of Washington10-1 CSC 143 Java Errors and Exceptions Reading: Ch. 15.
Exceptions Lecture 11 COMP 401, Fall /25/2014.
Lecture10 Exception Handling Jaeki Song. Introduction Categories of errors –Compilation error The rules of language have not been followed –Runtime error.
And other languages…. must remember to check return value OR, must pass label/exception handler to every function Caller Function return status Caller.
1 Handling Errors and Exceptions Chapter 6. 2 Objectives You will be able to: 1. Use the try, catch, and finally statements to handle exceptions. 2. Raise.
Ruby Inheritance and other languages….
Eighth Lecture Exception Handling in Java
C++ Exceptions.
Exceptions in Python Error Handling.
Exceptions.
Debugging and Handling Exceptions
Handling Exceptionally Sticky Problems
Tirgul 13 Exceptions 1.
Java Programming Language
Chapter 13 Exception Handling
Introduction to Exceptions in Java
Computer Programming I
Introduction to Exceptions in Java
Chapter 12 Exception Handling and Text IO
Exception Handling.
Exceptions and files Taken from notes by Dr. Neil Moore
Chapter 14: Exception Handling
CNS 3260 C# .NET Software Development
Partnership.
Exceptions & exception handling
Topics Introduction to File Input and Output
Exceptions & exception handling
Java Programming Language
Python’s Errors and Exceptions
Chapter 12 Exception Handling
Exceptions and files Taken from notes by Dr. Neil Moore
Exception Handling.
Part B – Structured Exception Handling
Exceptions.
CIT 383: Administrative Scripting
Exception Handling.
Exception Handling.
Problems Debugging is fine and dandy, but remember we divided problems into compile-time problems and runtime problems? Debugging only copes with the former.
Python Syntax Errors and Exceptions
CSC 143 Java More on Exceptions.
By Ryan Christen Errors and Exceptions.
CSC 143 Java Errors and Exceptions.
Handling Exceptionally Sticky Problems
Topics Introduction to File Input and Output
CMSC 202 Exceptions.
Exception Handling.
Presentation transcript:

similar concepts, different syntax Ruby Exceptions similar concepts, different syntax

So what about Ruby?* Exceptions are raised using the raise method of Kernel The rescue clause is used to handle exceptions Exceptions are instances of the Exception class (or a subclass) Subclasses do not add methods or behavior, but allow exceptions to be categorized Most exceptions extend StandardError Other exceptions are low-level, typically not handled by programs * Exception info directly from The Ruby Programming Language

Getting info message method returns a string – more suitable for programmers than end users backtrace returns the call stack array of strings filename : linenumber in methodname

raising an exception fail is synonym (use if expect program to end) Several ways to invoke raise: with no arguments. If inside rescue, re-raises same exception, otherwise raises RuntimeError with single Exception object. Not common. with single String argument. Creates RuntimeError with that string as message. Very common. with exception object, string (for message) and array of strings (for backtrace).

Example def factorial(n) raise "bad argument" if n < 1 # raise ArgumentError if n < 1 # raise ArgumentError, "Expected argument >= 1, got #{n}" if n < 1 return 1 if n == 1 n * factorial(n-1) end Can provide a custom stack trace def factorial4(n) if n < 1 raise ArgumentError, "Expected argument >= 1, got #{n}", caller

capturing an exception rescue is part of Ruby language (not a Kernel method) clause that can be attached to other Ruby statements typically attached to begin begin # statements, possible exceptions rescue # code to deal with exceptions end # if no exception, code continues here, code in rescue is not executed

handling the exception rescue handles any StandardError global variable $! refers to raised exception better to specify variable in rescue begin x = factorial(0) rescue => ex puts "#{ex.class}: #{ex.message}" end

handling multiple types def factorial5(n) raise TypeError, “Need integer" if not n.is_a? Integer raise ArgumentError, “Need argument >= 1, got #{n}" if n < 1 return 1 if n == 1 n * factorial(n-1) end begin x = factorial5("a") rescue ArgumentError => ex puts "Try again with argument > 1" rescue TypeError => ex puts "Try again with an integer" Does the order matter?

propagating exceptions def explode raise "bam!" if rand(10) == 0 end def risky begin 10.times do explode #raises error ~10% of time rescue TypeError # won't catch RuntimeError puts $! end # RuntimeError not handled, will propagate "hello" # if no exception def defuse puts risky rescue RuntimeError => e # handle propagated error puts e.message defuse QUICK EX: Trace code

Other options retry – with rescue, reruns block of code that rescue is attached to. Useful for transient failures such as overloaded server. Be sure to limit number of retries. ensure – code that always runs, for housekeeping like disconnecting from database, closing files, etc. not covered: subtle details for ensure rescue can be used as a statement modifier (y = factorial(x) rescue 0 #returns 0 if error)

throw/catch similar to a labeled break, but can break out of multiple levels not used for exception handling used infrequently, details not covered