From: Raveendran .P on
Hi,


lib_error.rb:

class Library
def self.error
puts "Oops. something went wrong"
end
end


original_code.rb:

# require the file first line of code instead of calling in mid of
programs

require 'lib_error.rb'

begin
puts fg
rescue
Library.error
end


begin
puts fdg
rescue
Library.error
end

Run the code:

Oops. something went wrong
Oops. something went wrong

I hope it clears ur doubt. If need more details then update here asap.

Thanks,
P.Raveendran
http://raveendran.wordpress.com
--
Posted via http://www.ruby-forum.com/.

From: Raveendran .P on

Hi,

You can use the first line as,


require 'lib_error'

OR

load 'lib_error.rb'

OR

require 'lib_error.rb'




Thanks,
P.Raveendran
http://raveendran.wordpress.com

--
Posted via http://www.ruby-forum.com/.

From: Jan Roslind on
Thanks P.Raveendran
Will give it a try :=)
--
Posted via http://www.ruby-forum.com/.

From: Jan Roslind on
Raveendran .P wrote:
> ....
> begin
> puts fdg
> rescue
> Library.error
> end
> .....

Sorry, but I ends up adding

rescue
Library.error binding, self

to every methods. Not wery DRY. Problem if I want to change rescue
clause later (add a argument)

Regards
Jan
--
Posted via http://www.ruby-forum.com/.

From: Martin Boese on
On Thu, 11 Feb 2010 16:27:02 +0900
Jan Roslind <jan(a)jan-roslind.dk> wrote:

> Raveendran .P wrote:
> > ....
> > begin
> > puts fdg
> > rescue
> > Library.error
> > end
> > .....
>
> Sorry, but I ends up adding
>
> rescue
> Library.error binding, self
>
> to every methods. Not wery DRY. Problem if I want to change rescue
> clause later (add a argument)
>
> Regards
> Jan


Hmm.. what about:


class Code
def self.try(&block)
begin
block.call
rescue
puts "Errors: #{$!}"
end
end
end


Code.try do
a = 1123
bad_code
end



Cheers,
Martin