Launch a Custom Exception

0

I have a method in my Webservice that throws an exception if the code of the card is already in use, ie when trying to release the access the system checks if it is already in use and returns the Exception . >

Code in Webservice

... 
try
{
    if(ListaCartoes.Any(c => c.Codigo == cartaoParaAcesso)
       throw new Exception("CARTÃO JÁ ESTÁ EM USO");
}
catch(Exception ex)
{
  throw new ErroLiberacaoDeAcesso(ex.message);
}

Customer Code

...
try
  {
      Webservice.LiberaAcesso(informacoesAcesso);        
  }
  catch(Exception ex)
  {
       MessageBox.Show(ex.message);
  }

Code class Error AccessLibrary:

 [Serializable()]
    public class ErroLiberaAcesso: Exception
    {
        public ErroLiberaAcesso() : base()
        { }

        public ErroLiberaAcesso(string message) : base(message)
        { }

        public ErroLiberaAcesso(string message, Exception innerException) : base(message, innerException)
        { }

        protected ErroLiberaAcesso(SerializationInfo info, StreamingContext context) : base(info, context)
        { }
    }

Error that is returning:

  ---

exception = {"System.Web.Services.Protocols.SoapException: The server was unable to process the request." -> Pca_Webservice_V2.AccessRecovery: CARD NUMBER IN USE \ n

That is, it is not just the "NUMBER OF CARD IN USE" message that is returning. What can I do in this case?

    
asked by anonymous 12.11.2015 / 14:00

1 answer

1

As I understand you have a business rule and not an exceptional situation . So you should not use this mechanism. The last thing you should do in this case is to replace a simple% with% with a complex% with%.

Before using a language feature, understand what it serves .

Mainly the way you are doing where you generate a general exception, then capture , which does not the least sense in doing this (it's the worst exception abuse I've ever seen) and then throwing another one that has a misnomer (it should be if ). But the name is not even the problem, it's its existence and its use . If it is a business rule, treat it in the normal flow of the system, generating a return that informs the situation .

Then webservice would look something like this:

 //faz o que tem que fazer aqui (se é que tem algo mais)
 return ListaCartoes.Any(c => c.Codigo == cartaoParaAcesso)

On the client:

if (Webservice.LiberaAcesso(informacoesAcesso)) {
    //faz algo aqui se tiver que fazer
} else {
    MessageBox.Show("CARTÃO JÁ ESTÁ EM USO");
}

If you have other reasons for errors in the business rule, you can change the logic a bit. In this case it would probably create a class to encapsulate the error information and would send it as a return, rather than a Boolean.

This class would be very similar to an exception, but would not be one, avoiding long detours ( catch in its worst form) and unpredictability of the action, not to mention slowness and the possibility of misuse of the mechanism, as it happens (note that the current code is saying that the card is already in use even if it has a programming error or another system failure such as LiberaAcessoException ).

If the class is too exaggerated for you, return another form of code / message as I show in another question .

Doing right, perhaps, will discover other problems in your code that you could not even imagine because you were using the feature in the wrong way.

    
12.11.2015 / 15:14