return error message with "Throw Exception" or "Return"?

2

Hello, the question arose to me, which is the best way to return a message in the event of an error? Throw Exception:

try
{
    .......
 return  cargarPersonas;
}
catch(Exception e)
{
    throw  new Exception("Mensaje.", e);
}

Return:

try
{
    .......
 return  cargarPersonas;
}
catch(Exception e)
{
    return "Mensaje: " + e.message;
}
    
asked by Lalo Alexander 28.09.2016 в 22:45
source

4 answers

1

The 2 options have a different purpose, the first using throw serves to throw an exception in our application.

try
{
    .......
 return  cargarPersonas;
}
catch(Exception e)
{
    throw  new Exception("Mensaje.", e);
}

and the second option returns a String containing the error message:

try
{
    .......
 return  cargarPersonas;
}
catch(Exception e)
{
    return "Mensaje: " + e.message;
}

If you just want to return a message:

  

What is the best way to return a message in the case that arises   an error?

the ideal option would be the second option.

    
answered by 29.09.2016 в 00:06
1

According to Microsoft's guide , the following are given recommendations:

  • Avoid returning error codes
  • Report errors throwing exceptions.
  • If an error occurs in which it is dangerous to continue with the execution of the program, the execution must be terminated.
  • Do not use exceptions for normal flow control.
  • Document exceptions.
  • Do not return method exceptions
  • Consider using builders for exceptions.

Among others that can be seen in the official guide.

    
answered by 29.09.2016 в 00:36
0

Clearly throw the exception. That is, the first option.

The method that is responsible for collecting the exception will have a complete Exception object with all its properties including the descriptive message.

The method that collects the exception may control it or re-launch it to a higher level if it can not handle it.

    
answered by 08.11.2016 в 19:50
0

The first option is more accurate in case you need to propagate the error to higher layers, however I do not recommend that you create a new Exception object.

The drawback of the second option is that you can only return a message when your method returns a string.

What you should do is:

If you know in advance that under certain conditions, an operation can fail, it is better that you throw the exception:

if(empleado == null || empleado.nombre == null) throw new InvalidOperationException("Mensaje relacionado a objetos de empleados no inicializados");

if(empleado.id < 0) throw new InvalidOperationException(string.Format("Mensake de Id no válido: {0}", empleado.nombre));

which will allow you to have a try / catch that will only propagate the errors:

try{ }catch{ throw; }

Which is basically the same as:

try{ }catch(Exception exc) { throw exc; }

What you should not do is:

try{ }catch(Exception exc) { throw new Exception("Mensaje"); }

Since you would be overwriting valuable information about the cause of the exception and sending only a message that will surely be something like: "failed to load users" without really having information about the type of exception or a clear message of the cause of the error .

    
answered by 08.11.2016 в 20:35