Adding exception in loop