# How to check if operation returns an error

**URL:** <https://www.ruby-forum.com/t/how-to-check-if-operation-returns-an-error/179911>\
**Category:** Ruby\
**Created:** [December 7, 2009, 10:46pm UTC](https://www.ruby-forum.com/t/how-to-check-if-operation-returns-an-error/179911 "2009-12-07T22:46:44Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![lordcrap](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/ba8739/32.png) [@lordcrap](https://www.ruby-forum.com/u/lordcrap)\
**Post date:** [December 7, 2009, 10:46pm UTC](https://www.ruby-forum.com/t/how-to-check-if-operation-returns-an-error/179911/1 "2009-12-07T22:46:44Z")

</div>

Hi all, I started learning ruby two weeks ago and I’m loving it. I’m  
coding some date/time program to practice at the moment and I hit a  
wall. I need a way to ask ruby whether or not a certain operation will  
return an error. (I’ve tried google, I’ve searched this forum, the  
manuals and IRC but to no avail, so I’m turning to you guys)

I don’t think the code is necessary since it’s probably a really easy  
thing such as “.isitanerror” or whatever but here it is anyway.

To put you in context, before this, the program checks that all the  
input was integers and well formated and then inputs the whole thing  
into an array called “thedate”. Now I want to check if the array of  
integers is a date or not (i.e. if someone enters ‘99’ in the ‘hour’  
slot I want the program to know it’s not correct.) When I enter a real  
date it works, when I enter malformated gibberish the code before  
catches it but if I enter well formated “not date” numbers, the last  
line before the “end” returns an error - so again, what I want to do is  
to test wether or not that last line will return an error without  
running it and breaking the program.

if problems != true  
dateformated = thedate[0].to\_s + ‘-’ + thedate[1].to\_s + ‘-’ +  
thedate[2].to\_s + ‘T’ + thedate[3].to\_s + ‘:’ + thedate[4].to\_s + ‘:’ +  
thedate[5].to\_s

birthindate = DateTime.parse(dateformated)  
end

---

<div class="post-metadata">

**Author:** ![lordcrap](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/ba8739/32.png) [@lordcrap](https://www.ruby-forum.com/u/lordcrap)\
**Post date:** [December 7, 2009, 10:54pm UTC](https://www.ruby-forum.com/t/how-to-check-if-operation-returns-an-error/179911/2 "2009-12-07T22:54:08Z")

</div>

> thing such as “.isitanerror” or whatever but here it is anyway.  
> running it and breaking the program.  
> You can’t. You can, however, catch the error when it occurs:

irb(main):006:0\> begin  
irb(main):007:1\* DateTime.parse ‘blah’  
irb(main):008:1\> rescue  
irb(main):009:1\> puts ‘learn to write a date fool!’  
irb(main):010:1\> end  
learn to write a date fool!  
=\> nil  
irb(main):011:0\>

Have a look at the exceptions section here:

> **[Ruby Basic Tutorial](http://www.troubleshooters.com/codecorn/ruby/basictutorial.htm#_Exceptions)**
>
> A tutorial on Ruby basics

---

<div class="post-metadata">

**Author:** ![lordcrap](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/ba8739/32.png) [@lordcrap](https://www.ruby-forum.com/u/lordcrap)\
**Post date:** [December 7, 2009, 11:10pm UTC](https://www.ruby-forum.com/t/how-to-check-if-operation-returns-an-error/179911/3 "2009-12-07T23:10:09Z")

</div>

Thank you, it works perfectly!

Although not in the format I was expecting it, it’s exactly what I was  
looking for.

---

<div class="post-metadata">

**Author:** ![lordcrap](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/ba8739/32.png) [@lordcrap](https://www.ruby-forum.com/u/lordcrap)\
**Post date:** [December 11, 2009, 2:37pm UTC](https://www.ruby-forum.com/t/how-to-check-if-operation-returns-an-error/179911/4 "2009-12-11T14:37:00Z")

</div>

begin … rescue way is similar to Java’s try…catch, and it is  
comfirtable.
