hi,i don't know how to capture the following exception if the file directory or name is not correct.
You are re-raising the exception in rescue block. Try to modify the rescue block like this:
rescue Exception => e
flash.now[:error]=“error: #{e.message}”
end
You can do something like
raise "File not found" unless File.exists?(filename)
or better
begin
raise FileNotFoundException.new("File not Found") unless File.exists?(filename) .... rescue FileNotFoundException .... end
by writing your own FileNotFoundException class and catching it in the rescue class
Szymon Nowak wrote in post #988178:
You are re-raising the exception in rescue block. Try to modify the rescue block like this:
rescue Exception => e flash.now[:error]="error: #{e.message}" end
thank you,szimek,chaitanyv.it seems we just need to rescue the exception,and no need to re-raise exception.but i do not know why nearly all the examples in the book <programming ruby> re-raise the exceptions.i think i have misunderstood the exception in ruby.if an exception raises in the end used method,we generally do a final capture,and no need to reraise,am i right?
[...]but i do not know why nearly all the examples in the book <programming ruby> re-raise the exceptions.i think i have misunderstood the exception in ruby.if an exception raises in the end used method,we generally do a final capture,and no need to reraise,am i right?
You are right. Catching and re-raising the exception is used if you want to do something when the exception happens but you don't want to fully handle the exception. For example, if you would like to log it but you don't want to be responsible for handling the problem. If in your case catching the exception to update the flash is enough then that's all you need and you don't have to re-raise.
pepe wrote in post #988286:
[...]but i do not know why nearly all the examples in the book <programming ruby> re-raise the exceptions.i think i have misunderstood the exception in ruby.if an exception raises in the end used method,we generally do a final capture,and no need to reraise,am i right?
You are right. Catching and re-raising the exception is used if you want to do something when the exception happens but you don't want to fully handle the exception. For example, if you would like to log it but you don't want to be responsible for handling the problem. If in your case catching the exception to update the flash is enough then that's all you need and you don't have to re-raise.
thank you,i have further understood exception under your help,which makes me happy.
>> [...]but i do not know why nearly >> all the examples in the book <programming ruby> re-raise the >> exceptions.i think i have misunderstood the exception in ruby.if an >> exception raises in the end used method,we generally do a final >> capture,and no need to reraise,am i right?
> You are right. Catching and re-raising the exception is used if you > want to do something when the exception happens but you don't want to > fully handle the exception. For example, if you would like to log it > but you don't want to be responsible for handling the problem. If in > your case catching the exception to update the flash is enough then > that's all you need and you don't have to re-raise.
thank you,i have further understood exception under your help,which makes me happy.
You're very welcome. Glad I could help.