Skip to content

Instantly share code, notes, and snippets.

View edipofederle's full-sized avatar
🏠
Working from home

Édipo Féderle edipofederle

🏠
Working from home
View GitHub Profile
Exception
NoMemoryError
ScriptError
LoadError
NotImplementedError
SyntaxError
SignalException
Interrupt
StandardError
ArgumentError
def fatorial(n)
raise "Argumento invalido" if n < 1
return 1 if n == 1
n * fatorial(n-1)
end
puts fatorial(5)
puts fatorial(4)
puts fatorial(10)
puts fatorial(-4)
def fatorial(n)
raise ArgumentError if n < 1
return 1 if n == 1
n * fatorial(n-1)
end
puts fatorial(5)
puts fatorial(4)
puts fatorial(10)
puts fatorial(-4)
def fatorial(n)
raise ArgumentError, "Esperava um argumento >= 1. Mas retornou #{n}", caller if n < 1
return 1 if n == 1
n * fatorial(n-1)
end
puts fatorial(-4)
def fatorial(n)
raise TypeError, "Valor invalido, somente inteiros positivos" unless n.is_a? Integer
raise ArgumentError, "Esperava um argumento >= 1. Mas retornou #{n}", caller if n < 1
return 1 if n == 1
n * fatorial(n-1)
end
require "test/unit"
require "fatorial.rb"
class TestLibraryFileName < Test::Unit::TestCase
def test_simples_cases
assert_equal(120, fatorial(5))
assert_equal(6, fatorial(3))
end
def test_negative_numbers
begin
#algum código
rescue Exception => e
#código da manipualação da exceção vem aqui, qualquer exceção levantada pelo código acima, cai aqui.
end
def simple(n)
raise ArgumentError, "Argumento Invalido" if n < 0
end
begin
simple(-4)
rescue Exception => e
puts "#{e.class}: #{e.message}"
end
def simple(n)
raise ArgumentError, "Argumento Invalido" if n < 0
end
begin
simple(-4)
rescue Exception => e
puts "Dentro do Rescue"
puts "#{e.class}: #{e.message}"
puts $!
begin
x = fatorial("f")
rescue ArgumentError => e
puts "Numero >= que 1 eh preciso"
rescue TypeError => e
puts "Tente com intiero"
end