Launch a custom Exception

Asked

Viewed 1,407 times

0

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

Code in the 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);
}

Code on the Client

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

Code Class Erroraccess:

 [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 could not process the request. ---> Pca_webservice_v2.Errorsaccess: CARD NUMBER IN USE n

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

  • Post the code of your "Errormanceaccess"

  • If you only want the text, why not just return a text?

  • @jbueno if I return a text, I have to keep comparing string to know what was the return of the error. Since more than one type of error may occur.

  • 1

    It would be interesting if you only launch your Exception and work with the same no?

  • @Shura16 forgive me but I did not understand your suggestion.

  • 1

    No need to launch a new Exception to capture her and launch her ErroLiberacaoDeAcesso. We agree to use the suffix Exception in the exceptions. try/catch and return only the exception message.

  • @Mfedatto as would be this encapsulation to Try/catch, I recognize that this is the first time I see talk about it.

  • 1

    Like Mfedatto said, you don’t have to use try/catch on your Webservice. Use only the if ... and inside it throws its Exception throw new ErroLiberacaoDeAcesso("Mensagem de erro");

  • @Jcsaint Webservice can release other exceptions of the same genre? This "exception" is a business rule and not an exceptional situation, right?

  • @Shura16 understood, I did it but the mistake was the same. :(

  • @bigown yes, for example, I check whether the person has access or not, whether the card is within the expiration date or not, whether the number is already in use, since the attendant may enter a wrong number. That’s why I’m using exceptions, so I don’t need to use if and compare which error type was returned.

  • 1

    If you only want the message return only the message and not Exception.

  • 1

    @Jcsaint as Exceptions are exceptions to the rule. There is nothing wrong with using exceptions to treat business validation, but it would be interesting for you to use a specific type for business exceptions, such as public class BusinessException : Exception and in your try/catch you check the two types of exception: try { Foo(); } catch (BusinessException ex) { Console.WriteLine(ex.Message); } catch (Exception) { throw; }.

  • 1

    @Jcsaint it is important to keep in mind that the exceptions released in your service do not reach the customer as normal exceptions. The communication between the customer and the service is entirely based on request and response, while an exception is an interruption in the process Runtime, fully within the service. What the customer receives is a missing exception in communicating with the service. In the customer you need to treat a SoapException, not a ErroLiberacaoDeAcesso.

  • @Mfedatto ok, thanks for the explanation, mainly on how the exception reaches the customer. :)

  • @Jcsaint Take a look at [tour]. You can accept an answer if it solved your problem. You can vote on all the posts on the site as well. Did any help you more? You need something to be improved?

Show 11 more comments

1 answer

2

From what I understand you have a business rule and not an exceptional situation. Then you should not use this mechanism. The last thing you should do in this case is to replace a if simple-for-a catch complex.

Before using a language resource, understand what it’s for.

Especially the way you’re doing where you generate a general exception, then the catch, which makes no sense at all in doing this (it’s the worst exception abuse I’ve ever seen) and then releasing another one that has a misnamed (it should be LiberaAcessoException). But the name isn’t even the problem, it’s its existence and its use. If it is business rule, treat in the normal flow of the system, generating a return that informs the situation.

Then the 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");
}

I put in the Github for future reference.

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

That class would be too similar to an exception, but would not be a, avoiding the long detours (goto in its worst form) and unpredictability of the action, not to mention the slowness and the possibility of misuse of the mechanism, as occurs (note that the current code is saying that the card is already in use even if you have a programming error or other system failure, as the System.Web.Services.Protocols.SoapException).

If class is something exaggerated for you, return another form of code/message as I do demonstrate in another question.

Doing the right thing might uncover other issues in your code that you didn’t even know about because you were using the feature the wrong way.

  • In this case I will have to create a method for each business rule in the webservice, ie a method to check if card is in one, a method to check the validity, a method to check if the card number is correct ? And on the customer side use an if for each check also ?!

  • Probably, but this is about organization. Of course you can use only one. It depends on what you need. Your code doesn’t make any of this clear, so I can’t talk specifically. Then the verification can use one or several ifs, it depends on how you will do. It actually follows the same exception scheme. If you need several catchs, will need several ifs, if I would only use one catch, then you can only use one if. The only difference between one and the other is that in the ifyou know when he will be executed, with the catch, you do not know. And causes the problem reported in the question.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.