Exercises, examples and other material relating to training module R111. This topic is presented on public courses Learning to program in Ruby
, Ruby Programming
You can check for some, but not all, errors. It's
sometimes necessary (and often preferable even if not
necessary) to write clean code that assumes correct
operation, and provide rescue blocks telling Ruby what
to do in the event of a failure.
Related technical and longer articleserrors v exceptions
|Articles and tips on this subject||updated|
|4008||Reading and checking user inputs - first lessons - Ruby|
Early programming exercises on a "learning to program in Xxxx" course involve asking the user to enter some pieces of data (usually numbers), doing a calculation using those numbers, and printing out a result. For that's a quite common requirement for a simple program - and this is the first realistic ...
|3435||Sorta sorting a hash, and what if an exception is NOT thrown - Ruby|
Some gems from last week ...
1. Ruby's begin and rescue clauses allow you to catch anything exceptional that could derail your program. But did you know that you can also add an else clause to be run only in the event of there being no exception? There's an example from last week's Ruby course which ...
|3433||Exceptions - a fail-safe way of trapping things that may go wrong|
As part of a previous post, I was looking at the "Internal Server Errors" logged on our web server over the past 3 months ... and I found one coming from a Python / CGI demonstration which I wrote and uploaded for a delegate a couple of months back.
Internal server error 500 (by default on Apache httpd) ...
|3260||Ruby - a training example that puts many language elements together to demonstrate the whole|
Towards the end of our programming language training courses, we pull together all the various strands into a worked example that shows how they go together. I've just posted such an example from last week's Ruby Programming Course ... [here].
Let's have a look at some of the things in the example ...
|3177||Insurance against any errors - Volcanoes and Python|
Have you heard of people who have taken out insurance, only do discover that they're not covered for some eventuality - "but the list of circumstances doesn't include your flight being canceled because the plane couldn't fly through volcanic ash" is a story that many people heard, with an irony that ...
|2622||Handling unusual and error conditions - exceptions|
"I can't answer that question in the way you expect" ... that's something that may be said to you occasionally - you ask someone what suit a playing card is that they're holding and they cannot tell you because it is a joker, or you ask what number is written on a piece of paper when the paper is blank.
|2621||Ruby collections and strings - some new examples|
From yesterday's Ruby course - a whole set of new examples on Ruby collections - arrays (which are ordered lists) and hashes (which are unordered). In other words, you look somethng up in an array by its position number, whereas you look something up in a hash based on its unique key, and the way it's ...
|2620||Direct access to object variable (attributes) in Ruby|
Rather than writing getters and setters, in many Object Oriented languages you can access the variables within an object directly. That can be dangerous for code flexibility for the future, as it means that you're removing the possibility of interspersing code. However, it can be very convenient.
|2615||String to number conversion with error trapping in Ruby|
You've read in a string of text in Ruby - perhaps the user's input, or perhaps from a file. You know that string shoould contain a number - so you use the to_i method to do the conversion. But there's a problem - if the string doesn't start with a number, it won't convert correctly - it will just ...
|1875||What are exceptions - Python based answer|
How do you check for run time errors in your program? You'll include tests with if statements in almost every program you write, to ensure that user entered data is reasonable / as you would expect ... but you will also need to do more that that. You'll need to check whether a system function has run ...
Examples from our training material
|bmi.rb|| BMI (Body Mass Index) calculator|
|catcher|| Converting String to Integer and handling errors|
|chq|| catching exceptions - file handling|
|fsta.rb|| File Reporter - assumes all is OK|
|fstb.rb|| File Reporter - handle problems with open|
|fstc.rb|| File Reporter - handle multiple problems and re prompt|
|mc|| Catching user errors - a short demonstration|
|mighty.rb|| Passing exceptions back to calling code|
Some modules are available for download
as a sample of our material or under an Open Training Notes License
for free download from [here]
Topics covered in this module
begin and end (and a mention of BEGIN and END).
Raise and rescue.
Throw and catch.
If you are looking for a complete course and not just a information on a single subject, visit our Listing and schedule
Well House Consultants specialise in training courses in
. We run
throughout the UK (and beyond for longer courses), and
at our training centre in Melksham, Wiltshire, England.
It's surprisingly cost effective to come on our public courses -
even if you live in a different
country or continent to us
We have a technical library of over 700 books on the subjects on which we teach.
These books are available for reference at our training centre.