# “TRY-CATCH-FINALLY” EQUIVALENT IN RUBY

> URL: https://crodrigues.com/try-catch-finally-equivalent-in-ruby/  
> Author: Carlos Rodrigues  
> Published: 2011-05-10  
> Updated: 2026-10-21  
> Categories: Backend  
> Tags: ror, ruby  

_For those like me who are entering the ruby world but have programming experience with other languages, it can be hard to get things done,…
Continue reading“TRY-CATCH-FINALLY” EQUIVALENT IN RUBY_
For those like me who are entering the ruby world but have programming experience with other languages, it can be hard to get things done, but that’s a part of ruby, doing things differently

![:)](https://web.archive.org/web/20200926061359im_/http://crodrigues.com/wp-includes/images/smilies/icon_smile.gif)

On a language like Java or C# the syntax is:

```csharp
try
{
   some_code();
}
catch(Exeption)
{
   handle_expeption();
}
finally
{
   this_code_is_always_executed();
}
```

In Ruby the ideia is the same, but the syntax is a little different:

begin – rescue – ensure

Lets see:

```ruby
 begin
     some_code
 rescue
      handle_error
 ensure
     this_code_is_always_executed
 end
```

If you need to log the error, you can have access to the error message inside **rescue** block:

```ruby
@error_message="#{$!}"
```

Hope you find it as useful as I did 🙂
