# Rescuing errors.. catch-all

**URL:** <https://rubytalk.org/t/rescuing-errors-catch-all/6473>\
**Category:** ruby-talk\
**Created:** [20 June 2003 00:04 UTC](https://rubytalk.org/t/rescuing-errors-catch-all/6473 "2003-06-20T00:04:19Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Daniel10](https://avatars.discourse-cdn.com/v4/letter/d/da6949/32.png) [@Daniel10](https://rubytalk.org/u/Daniel10)\
**Post date:** [20 June 2003 00:04 UTC](https://rubytalk.org/t/rescuing-errors-catch-all/6473/1 "2003-06-20T00:04:19Z")

</div>

Hi,

Im trying to print all error information to the browser so I don’t have  
to monitor the apache log every time I have a syntax error.

So far, it’s working very well with

begin  
…  
main prg  
…

rescue SystemCallError =\> rescueError  
if (rescueError)  
puts "

```
"
puts rescueError.message
puts rescueError.backtrace
puts "
```
"  
end  
end

however, I have to specify the error, is there some sort of catch-all  
exception I can use? I tried ‘Exception’ which catches SystemCallErrors  
as far as I could tell, but I still got Internal Errors (500)

db

> **···**
>
> –  
> Jun 19\* Corpus Christi (11 days after Pentecost)  
> Jun 19 Julius and Ethel Rosenberg are executed in Sing-Sing prison, 1953  
> Jun 19 Lizzie Bordon acquitted, 1893  
> Jun 19 Emancipation Day in Texas  
> Jun 19 Labor Day in Trinidad and Tobago  
> Jun 19 Revolution Day in Algeria  
> Jun 19 Artigas Birthday in Uruguay

---

<div class="post-metadata">

**Author:** ![Ara.T.Howard1](https://avatars.discourse-cdn.com/v4/letter/a/f19dbf/32.png) [@Ara.T.Howard1](https://rubytalk.org/u/Ara.T.Howard1)\
**Post date:** [20 June 2003 00:50 UTC](https://rubytalk.org/t/rescuing-errors-catch-all/6473/2 "2003-06-20T00:50:11Z")

</div>

> Hi,
> 
> Im trying to print all error information to the browser so I don’t have  
> to monitor the apache log every time I have a syntax error.
> 
> So far, it’s working very well with
> 
> begin  
> ..  
> main prg  
> ..
> 
> rescue SystemCallError =\> rescueError  
> if (rescueError)  
> puts “
> 
> ```
> ”
> puts rescueError.message
> puts rescueError.backtrace
> puts “
> ```
> ”  
> end  
> end
> 
> however, I have to specify the error, is there some sort of catch-all  
> exception I can use? I tried ‘Exception’ which catches SystemCallErrors  
> as far as I could tell, but I still got Internal Errors (500)

i think this is good as you can get - Exception is the mother of them all. if  
you are getting other (500) errors it means your script is not running  
(permissions, etc.), so it’s pretty tough for ruby to catch/report errors.  
😉

you can configure apache to do different things for this but that’s way OT…

cheers.

-a

> db
> 
> –  
> Jun 19\* Corpus Christi (11 days after Pentecost)  
> Jun 19 Julius and Ethel Rosenberg are executed in Sing-Sing prison, 1953  
> Jun 19 Lizzie Bordon acquitted, 1893  
> Jun 19 Emancipation Day in Texas  
> Jun 19 Labor Day in Trinidad and Tobago  
> Jun 19 Revolution Day in Algeria  
> Jun 19 Artigas Birthday in Uruguay

-a

> **···**
>
> ## On Fri, 20 Jun 2003, Daniel Bretoi wrote:
> 
> ====================================
> 
> > Ara Howard  
> > NOAA Forecast Systems Laboratory  
> > Information and Technology Services  
> > Data Systems Group  
> > R/FST 325 Broadway  
> > Boulder, CO 80305-3328  
> > Email: [ara.t.howard@noaa.gov](mailto:ara.t.howard@noaa.gov)  
> > Phone: 303-497-7238  
> > Fax: 303-497-7259  
> > ~ \> ruby -e ‘p(%.\x2d\x29..intern)’  
> > ====================================

---

<div class="post-metadata">

**Author:** ![Yukihiro\_Matsumoto2](https://avatars.discourse-cdn.com/v4/letter/y/b9e5f3/32.png) [@Yukihiro\_Matsumoto2](https://rubytalk.org/u/Yukihiro_Matsumoto2)\
**Post date:** [20 June 2003 00:56 UTC](https://rubytalk.org/t/rescuing-errors-catch-all/6473/3 "2003-06-20T00:56:04Z")

</div>

Hi,

> **···**
>
> In message “rescuing errors… catch-all” on 03/06/20, Daniel Bretoi [lists@debonair.net](mailto:lists@debonair.net) writes:
> 
> > however, I have to specify the error, is there some sort of catch-all  
> > exception I can use? I tried ‘Exception’ which catches SystemCallErrors  
> > as far as I could tell, but I still got Internal Errors (500)
> 
> Exception should catch all errors. But rescue does not help, when you  
> get error before entering protected region (main, this case), for  
> example, syntax errors, or even in kicking interpreter.
> 
> ```
> matz.
> 
> ```

---

<div class="post-metadata">

**Author:** ![Dmitry\_V\_Sabanin1](https://avatars.discourse-cdn.com/v4/letter/d/b77776/32.png) [@Dmitry\_V\_Sabanin1](https://rubytalk.org/u/Dmitry_V_Sabanin1)\
**Post date:** [20 June 2003 11:10 UTC](https://rubytalk.org/t/rescuing-errors-catch-all/6473/4 "2003-06-20T11:10:40Z")

</div>

One day i wrote special addition to Exception class, so it outputs quite  
pretty exception information to the browser window, it’s attached.  
When it’s included u can just do this:

begin  
#code here  
rescue Exception =\> e # not sure about this, i always had rescue =\> e here  
e.backtrace\_html if e.methods.include? ‘backtrace\_html’  
end

All backtracing code is made by ‘in-place html’ couse i wanted it to be  
totally independent from any template engines and so on.

Maybe it will be usefull to you.

[errors.rb](https://rubytalk.org/uploads/short-url/tGaTgYVYucCX5ip4Tkw3wB0hlDV.rb) (2.14 KB)

> **···**
>
> On Friday 20 June 2003 08:04, Daniel Bretoi wrote:
> 
> > Hi,
> > 
> > Im trying to print all error information to the browser so I don’t have  
> > to monitor the apache log every time I have a syntax error.
> 
> –  
> sdmitry -=- Dmitry V. Sabanin  
> MuraveyLabs.

---

<div class="post-metadata">

**Author:** ![Daniel10](https://avatars.discourse-cdn.com/v4/letter/d/da6949/32.png) [@Daniel10](https://rubytalk.org/u/Daniel10)\
**Post date:** [20 June 2003 16:27 UTC](https://rubytalk.org/t/rescuing-errors-catch-all/6473/5 "2003-06-20T16:27:39Z")

</div>

would this seem to be a reasonable feature request to make? it would be  
very nice to be able to catch everything and be able to do with it what  
you like, especially with html.

> **···**
>
> On Fri, Jun 20, 2003 at 09:56:04AM +0900, Yukihiro Matsumoto wrote:
> 
> > Hi,
> > 
> > In message “rescuing errors… catch-all” \> on 03/06/20, Daniel Bretoi [lists@debonair.net](mailto:lists@debonair.net) writes:
> > 
> > > however, I have to specify the error, is there some sort of catch-all  
> > > exception I can use? I tried ‘Exception’ which catches SystemCallErrors  
> > > as far as I could tell, but I still got Internal Errors (500)
> > 
> > Exception should catch all errors. But rescue does not help, when you  
> > get error before entering protected region (main, this case), for  
> > example, syntax errors, or even in kicking interpreter.

---

<div class="post-metadata">

**Author:** ![Daniel10](https://avatars.discourse-cdn.com/v4/letter/d/da6949/32.png) [@Daniel10](https://rubytalk.org/u/Daniel10)\
**Post date:** [20 June 2003 16:28 UTC](https://rubytalk.org/t/rescuing-errors-catch-all/6473/6 "2003-06-20T16:28:05Z")

</div>

thanks, I’ll have a look at it 🙂

db

> **···**
>
> On Fri, Jun 20, 2003 at 08:10:40PM +0900, Dmitry V. Sabanin wrote:
> 
> > On Friday 20 June 2003 08:04, Daniel Bretoi wrote:
> > 
> > > Hi,
> > > 
> > > Im trying to print all error information to the browser so I don’t have  
> > > to monitor the apache log every time I have a syntax error.
> > 
> > One day i wrote special addition to Exception class, so it outputs quite  
> > pretty exception information to the browser window, it’s attached.  
> > When it’s included u can just do this:
> > 
> > begin  
> > #code here  
> > rescue Exception =\> e # not sure about this, i always had rescue =\> e here  
> > e.backtrace\_html if e.methods.include? ‘backtrace\_html’  
> > end
> > 
> > All backtracing code is made by ‘in-place html’ couse i wanted it to be  
> > totally independent from any template engines and so on.
> > 
> > Maybe it will be usefull to you.
> > 
> > –  
> > sdmitry -=- Dmitry V. Sabanin  
> > MuraveyLabs.
> 
> > class Exception def backtrace\_html line\_color = ‘white’ lineHead\_color  
> > = ‘#FDDFA0’ error\_color = ‘red’ special\_color = ‘blue’ adminMail =  
> > ‘dmitry@iren.info’ odd\_color = ‘F2E8B3’ even\_color = ‘F5E693’ errsect  
> > = to\_s.scan (/^([\w\s]\*)[|]{3}/) if errsect.length \> 0 errtext =  
> > _to\_s.scan(/[|]{3}(._)$/) errtext = errtext.join(‘’) else errtext =  
> > to\_s errsect = ‘Unexpected’ end print "
> > 
> > ```
> > Ruby Error Report (Version #{VERSION}, platform #{RUBY_PLATFORM})
> > 
> > #{errsect} error:
> > There was an error while perfoming your request:
> > #{CGI.escapeHTML(errtext)} 
> > Exception type #{type.to_s}
> > 
> > ```
> > 
> > File Name Line Number Method Name " colorifier = 0 backtrace.each do
> > 
> > > line\> fileName, lineNum, methodName = line.split(/:/) if  
> > > line.scan(/\d\*:in/).length \> 0 methodName = \*methodName.scan(/`\w*'/) methodName.gsub!(/[`‘]/,’') if methodName end if  
> > > colorifier.divmod(2)[1] == 0 row\_color = odd\_color else row\_color =  
> > > even\_color end print "  
> > > #{fileName} #{lineNum} #{methodName if defined? methodName}  
> > > " colorifier += 1 end print "  
> > > If you wish to help us, please e-mail our [1]administrator about this  
> > > error…  
> > > This report was generated at #{Time.now}.  
> > > " end end
> > 
> > References
> > 
> > 1. mailto:/#{adminMail}
> 
> –  
> Jun 22 Carl Hubbell born, 1903  
> Jun 22 Meryl Streep born in Summit, New Jersey, 1949  
> Jun 20 Victoria crowned, 1837  
> Jun 21 Berlin airlift begins, 1948  
> Jun 21 Sun rises over Heelstone at Stonehenge  
> Jun 22 Civil rights workers disappear in Mississippi, 1964  
> Jun 20 Flag Day in Argentina  
> Jun 20 West Virginia Day in West Virginia  
> Jun 22 National Sovereignty Day in Haiti  
> Jun 22 Corrective Movement in Yemen Democratic Republic  
> Jun 22 Midsummer Eve in Finland, Sweden  
> Jun 21 Columbia records announces the first mass production of LP’s, 1948  
> Jun 22 Todd Rundgren is born in Upper Darby, Pennsylvania, 1948  
> Jun 21\* Summer Solstice  
> Jun 22\* Parashat Balak  
> Jun 22\* Parashat Hukkat

---

<div class="post-metadata">

**Author:** ![Ara.T.Howard1](https://avatars.discourse-cdn.com/v4/letter/a/f19dbf/32.png) [@Ara.T.Howard1](https://rubytalk.org/u/Ara.T.Howard1)\
**Post date:** [20 June 2003 17:16 UTC](https://rubytalk.org/t/rescuing-errors-catch-all/6473/7 "2003-06-20T17:16:18Z")

</div>

this is easy to do for yourself:

~ \> cat index.cgi # wrap your cgi’s in a baby shell script which dups 2 as 1  
#!/bin/sh  
cgi=‘foo.rb’  
$cgi 2\>&1

~ \> cat foo.rb # _any_ output from program - even from ruby failing - sent to browser  
#!/usr/local/bin/ruby  
\<\<| doesn’t even parse |\>\>

~ \> ./index.cgi \> out; cat out # note that all errors went to stdout - e.g. displayed by browser  
../foo.rb:2: parse error  
\<\<| doesn’t even parse |\>\>  
^  
../foo.rb:2: unterminated string meets end of file

when index.cgi is run all output, including errors, are displayed.

-a

> **···**
>
> On Sat, 21 Jun 2003, Daniel Bretoi wrote:
> 
> > would this seem to be a reasonable feature request to make? it would be very  
> > nice to be able to catch everything and be able to do with it what you like,  
> > especially with html.
> 
> # –
> 
> > Ara Howard  
> > NOAA Forecast Systems Laboratory  
> > Information and Technology Services  
> > Data Systems Group  
> > R/FST 325 Broadway  
> > Boulder, CO 80305-3328  
> > Email: [ara.t.howard@noaa.gov](mailto:ara.t.howard@noaa.gov)  
> > Phone: 303-497-7238  
> > Fax: 303-497-7259  
> > ~ \> ruby -e ‘p(%.\x2d\x29..intern)’  
> > ====================================

---

<div class="post-metadata">

**Author:** ![Yukihiro\_Matsumoto2](https://avatars.discourse-cdn.com/v4/letter/y/b9e5f3/32.png) [@Yukihiro\_Matsumoto2](https://rubytalk.org/u/Yukihiro_Matsumoto2)\
**Post date:** [20 June 2003 18:29 UTC](https://rubytalk.org/t/rescuing-errors-catch-all/6473/8 "2003-06-20T18:29:14Z")

</div>

Hi,

> **···**
>
> In message “Re: rescuing errors… catch-all” on 03/06/21, Daniel Bretoi [lists@debonair.net](mailto:lists@debonair.net) writes:
> 
> > > Exception should catch all errors. But rescue does not help, when you  
> > > get error before entering protected region (main, this case), for  
> > > example, syntax errors, or even in kicking interpreter.
> > 
> > would this seem to be a reasonable feature request to make? it would be  
> > very nice to be able to catch everything and be able to do with it what  
> > you like, especially with html.
> 
> I’m afraid that your request is rb\_protect() to catch _everything_.  
> Unfortunately it’s impossible.
> 
> ```
> matz.
> 
> ```

---

<div class="post-metadata">

**Author:** ![Daniel10](https://avatars.discourse-cdn.com/v4/letter/d/da6949/32.png) [@Daniel10](https://rubytalk.org/u/Daniel10)\
**Post date:** [20 June 2003 17:56 UTC](https://rubytalk.org/t/rescuing-errors-catch-all/6473/9 "2003-06-20T17:56:31Z")

</div>

> > would this seem to be a reasonable feature request to make? it would be very  
> > nice to be able to catch everything and be able to do with it what you like,  
> > especially with html.
> 
> this is easy to do for yourself:

perhaps with cgi, but I’m using embedded ruby. And I was thinking if  
it could be a benefit in other areas to be able to redirect errors like  
that…

db

> **···**
>
> On Sat, Jun 21, 2003 at 02:16:18AM +0900, ahoward wrote:
> 
> > On Sat, 21 Jun 2003, Daniel Bretoi wrote:
> 
> > ~ \> cat index.cgi # wrap your cgi’s in a baby shell script which dups 2 as 1  
> > #!/bin/sh  
> > cgi=‘foo.rb’  
> > $cgi 2\>&1
> > 
> > ~ \> cat foo.rb # _any_ output from program - even from ruby failing - sent to browser  
> > #!/usr/local/bin/ruby  
> > \<\<| doesn’t even parse |\>\>
> > 
> > ~ \> ./index.cgi \> out; cat out # note that all errors went to stdout - e.g. displayed by browser  
> > …/foo.rb:2: parse error  
> > \<\<| doesn’t even parse |\>\>  
> > ^  
> > …/foo.rb:2: unterminated string meets end of file
> > 
> > when index.cgi is run all output, including errors, are displayed.
> > 
> > ## -a
> > 
> > ====================================
> > 
> > > Ara Howard  
> > > NOAA Forecast Systems Laboratory  
> > > Information and Technology Services  
> > > Data Systems Group  
> > > R/FST 325 Broadway  
> > > Boulder, CO 80305-3328  
> > > Email: [ara.t.howard@noaa.gov](mailto:ara.t.howard@noaa.gov)  
> > > Phone: 303-497-7238  
> > > Fax: 303-497-7259  
> > > ~ \> ruby -e ‘p(%.\x2d\x29…intern)’  
> > > ====================================
> 
> –  
> Jun 22 Carl Hubbell born, 1903  
> Jun 22 Meryl Streep born in Summit, New Jersey, 1949  
> Jun 20 Victoria crowned, 1837  
> Jun 21 Berlin airlift begins, 1948  
> Jun 21 Sun rises over Heelstone at Stonehenge  
> Jun 22 Civil rights workers disappear in Mississippi, 1964  
> Jun 20 Flag Day in Argentina  
> Jun 20 West Virginia Day in West Virginia  
> Jun 22 National Sovereignty Day in Haiti  
> Jun 22 Corrective Movement in Yemen Democratic Republic  
> Jun 22 Midsummer Eve in Finland, Sweden  
> Jun 21 Columbia records announces the first mass production of LP’s, 1948  
> Jun 22 Todd Rundgren is born in Upper Darby, Pennsylvania, 1948  
> Jun 21\* Summer Solstice  
> Jun 22\* Parashat Balak  
> Jun 22\* Parashat Hukkat

---

<div class="post-metadata">

**Author:** ![Guillaume\_Marcais1](https://avatars.discourse-cdn.com/v4/letter/g/46a35a/32.png) [@Guillaume\_Marcais1](https://rubytalk.org/u/Guillaume_Marcais1)\
**Post date:** [20 June 2003 18:37 UTC](https://rubytalk.org/t/rescuing-errors-catch-all/6473/10 "2003-06-20T18:37:36Z")

</div>

Could there be another form of load that would report parse errors as an  
exception instead of crashing the entire process?

begin  
load\_with\_precaution(‘junk.pl’)  
rescue  
puts “Cannot load that piece of !@#$”  
end

Guillaume.

> **···**
>
> On Fri, 2003-06-20 at 14:29, Yukihiro Matsumoto wrote:
> 
> > Hi,
> > 
> > In message “Re: rescuing errors… catch-all” \> on 03/06/21, Daniel Bretoi [lists@debonair.net](mailto:lists@debonair.net) writes:
> > 
> > > > Exception should catch all errors. But rescue does not help, when you  
> > > > get error before entering protected region (main, this case), for  
> > > > example, syntax errors, or even in kicking interpreter.
> > > 
> > > would this seem to be a reasonable feature request to make? it would be  
> > > very nice to be able to catch everything and be able to do with it what  
> > > you like, especially with html.
> > 
> > I’m afraid that your request is rb\_protect() to catch _everything_.  
> > Unfortunately it’s impossible.
> > 
> > ```
> > matz.
> > 
> > ```

---

<div class="post-metadata">

**Author:** ![Yukihiro\_Matsumoto2](https://avatars.discourse-cdn.com/v4/letter/y/b9e5f3/32.png) [@Yukihiro\_Matsumoto2](https://rubytalk.org/u/Yukihiro_Matsumoto2)\
**Post date:** [21 June 2003 04:33 UTC](https://rubytalk.org/t/rescuing-errors-catch-all/6473/11 "2003-06-21T04:33:01Z")

</div>

Hi,

> Could there be another form of load that would report parse errors as an  
> exception instead of crashing the entire process?
> 
> begin  
> load\_with\_precaution(‘junk.pl’)  
> rescue  
> puts “Cannot load that piece of !@#$”  
> end

begin  
load(‘junk.pl’)  
rescue SyntaxError  
puts “Cannot load that piece of !@#$”  
end

should work.

```
						matz.

```

> **···**
>
> In message “Re: rescuing errors… catch-all” on 03/06/21, Guillaume Marcais [guslist@free.fr](mailto:guslist@free.fr) writes:

---

<div class="post-metadata">

**Author:** ![Guillaume\_Marcais1](https://avatars.discourse-cdn.com/v4/letter/g/46a35a/32.png) [@Guillaume\_Marcais1](https://rubytalk.org/u/Guillaume_Marcais1)\
**Post date:** [25 June 2003 03:15 UTC](https://rubytalk.org/t/rescuing-errors-catch-all/6473/12 "2003-06-25T03:15:29Z")

</div>

Oops, my mistake. It does work. I though, after a quick test, that it  
didn’t. Cool!

Guillaume.

> **···**
>
> Le samedi, 21 jun 2003, à 00:33 US/Eastern, Yukihiro Matsumoto a écrit :
> 
> > Hi,
> > 
> > In message “Re: rescuing errors… catch-all” \> on 03/06/21, Guillaume Marcais [guslist@free.fr](mailto:guslist@free.fr) writes:
> > 
> > > Could there be another form of load that would report parse errors as  
> > > an  
> > > exception instead of crashing the entire process?
> > > 
> > > begin  
> > > load\_with\_precaution(‘junk.pl’)  
> > > rescue  
> > > puts “Cannot load that piece of !@#$”  
> > > end
> > 
> > begin  
> > load(‘junk.pl’)  
> > rescue SyntaxError  
> > puts “Cannot load that piece of !@#$”  
> > end
> > 
> > should work.
> > 
> > ```
> > matz.
> > 
> > ```
