Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Exception handling
Message
De
26/05/2017 03:14:28
 
 
Information générale
Forum:
C#
Catégorie:
Code, syntaxe and commandes
Divers
Thread ID:
01651425
Message ID:
01651520
Vues:
41
>>>Hi everybody,
>>>
>>>I checked this https://stackoverflow.com/questions/1456563/best-way-to-check-for-inner-exception
>>>.
>>>We have the following code:
>>>
>>>
>>>var sqlException = GetSqlExceptionFromBaseException(ex);
>>>            if (sqlException != null)
>>>            {
>>>                //2627 is the exception number for a unique constraint violation
>>>                if (sqlException.Number == 2627)
>>>                {
>>>                    var errorMessage = Messages.alreadyExists;
>>>                    var sentences = sqlException.Message.Split('.');
>>>                    var duplicateValue = Regex.Matches(sqlException.Message, @"\(([^)]*)\)");
>>>                    if (sentences.Count() > 3  && duplicateValue.Count > 0)
>>>                    {                        
>>>                        errorMessage =
>>>                            String.Format(Messages.alreadyExistsWithValue, duplicateValue[0].Value.Replace("(", "").Replace(")", ""));
>>>                    }
>>>                    throw new DuplicatePropertyException(errorMessage);
>>>                }
>>>                else
>>>                {
>>>                    throw ex;
>>>                }
>>>            }
>>>            else
>>>            {
>>>                throw ex;
>>>            }
>>>        }
>>>
>>>The exception actually says check inner exception for details (and I can see the actual exception when I do so). I'm wondering what is the best way to throw this error but at the same time present that inner exception text as well?
>>>
>>>We do log it properly with Elmah but I also want to display it instead of 'see inner exception' message.
>>>
>>>Thanks in advance.
>>
>>Not sure what you are asking. 'InnerException' is a property of Exception so 'throw ex' will have the inner exception available (and you can drill down as far as required).
>>If you want the exception nested in the DuplicatePropertyException then just add it to the constructor:
throw new DuplicatePropertyException(errorMessage,ex);
>
>Hi Viv,
>
>My message perhaps was not very clear. I am asking about the line throw ex - is it possible to enhance this line to include inner exception directly?

As above ex DOES include the inner exception (if there is one). If you want to discard the top level exception and simply pass the inner exception then use:
throw ex.InnerException
>
>That error is later shown on the screen and it says exactly 'error such and such see inner exceptions for details'. This message is not very helpful to the user. I can try to see where in the interface code we're showing that exception and may be add some code there but I've been wondering if it somehow possible when I throw exception enhance its message to include the text of the inner exception directly into the message.
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform