How to deal with not knowing what exceptions can be raised by a library method in Ruby? How to deal with not knowing what exceptions can be raised by a library method in Ruby? ruby ruby

How to deal with not knowing what exceptions can be raised by a library method in Ruby?


(And yes I did look at the code of the library method, and can get some idea of what exceptions are raised but I cannot be 100% sure and if it is not documented I feel uncomfortable relying on it.)

I suggest having a look at the tests, as they will show some of the "likely" scenarios and what might be raised. Don't forget that good tests are documentation, too.


Should you want to discard non valid JSON data:

begin  res = JSON.parse(string)rescue JSON::ParserError => e  # string was not validend


I guess that if no documentation is provided, you have to rely on something like this :

begin   # code goes hererescue   # fail reason is in $!end