Over a million developers have joined DZone.

Just Rescue If The Errors Cause Doesn't Matter

·
Sometimes we have to check for every possible error and react adequate, but only sometimes. 
There are also cases, where it does not matter what's wrong,
the result we want in any error case is clear.
Then we  can just rescue.
 
Here the occurrence array may not be set because nothing is counted yet,
or one element is not set, because the according letter didn't occur yet,
it does not matter. We always answer "The requested letter occurred 0 times till now." 

  def get_char_count(letterbyte)
    @occurrences[letterbyte].ccount
  rescue
    0
  end  
Topics:

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}