why can not I release my exception

1

Good evening I'm starting on the issue of raise exeptions and I'm trying to perform a simple operation however it does not work this is what I tried:

class errores(Exception):
    def __int__(self,numero1,numero2):
        self.num1 = numero1
        self.num2 = numero2

    def error(self):
        return print("los numero {} {} no efectuaron la operacion".format(self.num1,self.num2))

try:
    def operaciones(operacion):
        def suma(x,y):
            return print(x+y)

        def resta(x,y):
            return print(x-y)

        lista = {'+':suma,'-':resta}

        return lista[operacion]
        raise errores
except errores as e:
    e.error()

pt = operaciones('*')
pt(10, 3)

I am working with higher order functions and also starting with functional programming.

But I do not understand what fails to launch the exception

    
asked by Mystic_Force 31.12.2018 в 06:05
source

1 answer

1

Your code has several problems:

  • Class names must start with a capital letter, that is, instead of errores your exception should be called Errores . Better yet, give it a more meaningful name that better describes what kind of error occurs. For example ErrorOperacion or similar.

  • The constructor must be __init__() instead of __int__()

  • Block try/catch is incorrectly placed. You have it around def operaciones() , but that block can not produce any exception (not even if you throw it with raise ) since all it does is define a function. The exception would not be thrown until that function is executed .

    That is, block try/catch should be around lines pt=operaciones('*') and pt(10,3) .

  • The constructor of your error expects two numbers, so when you instantiate it in raise you would have to pass it two numbers, such as raise ErrorOperacion(x,y) . But the problem is that x e y are not defined at the point where you try to throw the exception, since they are parameters of suma() or resta() , but not of operaciones() .

  • Your raise never executes, since it goes after a return instruction after which no more code of that function is executed anymore. Anyway, even if it was not after return , exceptions are thrown when a problem is detected, so it should be within some if . Otherwise, that exception will always be thrown when it reaches that line.

  • The attempt to access lista[operacion] will generate another exception if the operacion is not in the list (as is the case with the * ). This exception is of type KeyError that your code does not handle.

  • The method error of the exception should not print anything, but return a string to be printed by whoever calls it.

  • The same for operations sumar() and restar() . The functions in general should not print their results, but return them to be printed by the person who called them. And in any case it does not make sense to do a return print() , since print itself returns None and therefore that would be the value that your function would be returning.

I'm not sure how to fix all this, since I'm not sure what you had in mind when passing two numbers to the error constructor. I think it would be best to have two different exceptions, one to detect if the operation is not correct (which would be thrown if operacion is not in lista ) and another to detect if the operands are not correct (which would be launched according to the value of x e y , and therefore within the functions that already operate). Both exceptions could derive from another common one, to make it easier to capture them in a single except .

Like this:

class ErroresOperacion(Exception):
  pass

class ErrorOperacionInvalida(ErroresOperacion):
  def __init__(self, operando):
    self.op = operando

  def error(self):
    return "La operacion '{}' no está soportada".format(self.op)


class ErrorOperacionOperandos(ErroresOperacion):
  def __init__(self, num1, num2):
    self.num1 = num1
    self.num2 = num2

  def error(self):
    return "Los números {} y {} no son válidos para la operación".format(
        self.num1,self.num2)

def operaciones(operacion):
    def suma(x,y):
        return x+y

    def resta(x,y):
        return x-y

    def division(x, y):
      # Ejemplo de cómo lanzar la excepcion ErrorOperacionOperandos
      if y==0:
        raise ErrorOperacionOperandos(x,y)
      return x/y

    lista = {'+': suma, '-': resta, '/': division}

    if operacion not in lista:
      raise ErrorOperacionInvalida(operacion)

    return lista[operacion]

try: 
  pt = operaciones('+')
  r = pt(10, 3)
  print(r)
  pt = operaciones('*')
  r = pt(10, 3)
  print(r)
except ErroresOperacion as e:
  print(e.error())

try:
  pt = operaciones('-')
  r = pt(10, 0)
  print(r)
  pt = operaciones('/')
  r = pt(10, 0)
  print(r)
except ErroresOperacion as e:
  print(e.error())

That when executed will show:

13
La operacion '*' no está soportada
10
Los números 10 y 0 no son válidos para la operación
    
answered by 31.12.2018 / 12:04
source