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"
– Rod
If you only want the text, why not just return a text?
– Jéf Bueno
@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.
– JcSaint
It would be interesting if you only launch your Exception and work with the same no?
– Shura16
@Shura16 forgive me but I did not understand your suggestion.
– JcSaint
No need to launch a new
Exception
to capture her and launch herErroLiberacaoDeAcesso
. We agree to use the suffix Exception in the exceptions.try/catch
and return only the exception message.– MFedatto
@Mfedatto as would be this encapsulation to Try/catch, I recognize that this is the first time I see talk about it.
– JcSaint
Like Mfedatto said, you don’t have to use
try/catch
on your Webservice. Use only theif ...
and inside it throws its Exceptionthrow new ErroLiberacaoDeAcesso("Mensagem de erro");
– Shura16
@Jcsaint Webservice can release other exceptions of the same genre? This "exception" is a business rule and not an exceptional situation, right?
– Maniero
@Shura16 understood, I did it but the mistake was the same. :(
– JcSaint
@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.
– JcSaint
If you only want the message return only the message and not Exception.
– Shura16
@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 aspublic class BusinessException : Exception
and in yourtry/catch
you check the two types of exception:try { Foo(); } catch (BusinessException ex) { Console.WriteLine(ex.Message); } catch (Exception) { throw; }
.– MFedatto
@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 aErroLiberacaoDeAcesso
.– MFedatto
@Mfedatto ok, thanks for the explanation, mainly on how the exception reaches the customer. :)
– JcSaint
@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?
– Maniero