divz
divz

Reputation: 7967

Exception handling issue in ruby

In the following code i want to handle the exception.if msg[0] not found i have to catch that exception message msg[2] in rescue and if it is found put the success message msg[1]

puts "Verifying Home Page"
 def verifyHomepage(*args)
 begin
  args.each do |msg|    
    page.find(msg[0])
    puts msg[1]
    rescue
    puts msg[2]      
  end
end
end
verifyHomepage(['#logoAnchorr', 'logo anchor found', 'Logo anchor not Found'], ['.navbar-inner', 'Header Bar found', 'Header Bar  not Found'])

In the above code iam getting error sysntax error unexpected keyword rescue expecting keyword end

Upvotes: 1

Views: 163

Answers (2)

Arup Rakshit
Arup Rakshit

Reputation: 118299

Salil has pointed you where to fix,that's correct. Now The below approach also you could adapt:

puts "Verifying Home Page"

def verifyHomepage(*args)
  args.each do |msg|   
    next puts(msg[1]) if page.find(msg[0]) rescue nil  
    puts msg[2]     
  end
end
a = [['#logoAnchorr', 'logo anchor found', 'Logo anchor not Found'], ['.navbar-inner', 'Header Bar found', 'Header Bar  not Found']]
verifyHomepage(*a)

Output:

Verifying Home Page
Logo anchor not Found
Header Bar  not Found

Upvotes: 2

Salil
Salil

Reputation: 47532

You have to write begin inside the block

puts "Verifying Home Page"
def verifyHomepage(*args)  
    args.each do |msg|
      begin
        page.find(msg[0])
        puts msg[1]
      rescue
        puts msg[2]
    end
  end
end
verifyHomepage(['#logoAnchorr', 'logo anchor found', 'Logo anchor not Found'], ['.navbar-inner', 'Header Bar found', 'Header Bar  not Found'])

Upvotes: 1

Related Questions