How can I make an exception and add to my message containing the key and value?

I have methods that look like this:

public IDictionary<string, string> Delete(Account account) { try { _accountRepository.Delete(account); } catch { _errors.Add("", "Error when deleting account"); } return _errors; } public IDictionary<string, string> ValidateNoDuplicate(Account ac) { var accounts = GetAccounts(ac.PartitionKey); if (accounts.Any(b => b.Title.Equals(ac.Title) && !b.RowKey.Equals(ac.RowKey))) _errors.Add("Account.Title", "Duplicate"); return _errors; } 

I would like to change this method to return bool, and therefore it throws an exception if there is an error, and not:

 _errors.Add("", "Error when deleting account"); 

Can someone explain to me how I can throw an exception and pass a message containing the key and value. In this case, the key will be "" , and the value will be "Error when deleting account" .

Also in the method that calls this. How do I catch the exception?

Do I need to create my own class and somehow throw an exception based on this class?

+4
source share
4 answers

Create your own exception class that can store the data you need:

 public class AccountException : ApplicationException { public Dictionary<string, string> Errors { get; set; }; public AccountException(Exception ex) : base(ex) { Errors = new Dictionary<string, string>(); } public AccountException() : this(null) {} } 

In your methods, you can throw an exception. Also do not return the error status that is handled by the exception.

Do not throw the exception that you get in the method, include it as an InnerException so that it can be used for debugging.

 public void Delete(Account account) { try { _accountRepository.Delete(account); } catch(Exception ex) { AccountException a = new AccountException(ex); a.Errors.Add("", "Error when deleting account"); throw a; } } public void ValidateNoDuplicate(Account ac) { var accounts = GetAccounts(ac.PartitionKey); if (accounts.Any(b => b.Title.Equals(ac.Title) && !b.RowKey.Equals(ac.RowKey))) { AccountException a = new AccountException(); a.Errors.Add("Account.Title", "Duplicate"); throw a; } } 

When calling methods, you will catch your type of exception:

 try { Delete(account); } catch(AccountException ex) { // Handle the exception here. // The ex.Errors property contains the string pairs. // The ex.InnerException contains the actual exception } 
+5
source

The Exception class has a Data property which is a dictionary of key / value pairs.

 IDictionary<string, string> errors; ... if (errors.Count > 0) { Exception ex = ... construct exception of the appropriate type foreach(string key in _errors.Keys) { ex.Data.Add(key, _errors[key]); } throw ex; } 

Note that it is generally considered good practice to use Exceptions that are Serializable, so objects that you put in the data dictionary should also be serializable. In your example, you just insert rows so that everything will be fine.

Do I need to create my own class and somehow create an exception based on this class?

Of course, there is no need to create your own Exception class, and this may not be desirable. MSDN design guidelines for exceptions provide recommendations for choosing the type of exception .

In general, you should prefer to use one of the existing exception types, unless you have an error condition that can be programmed differently from the existing exception types.

+6
source

Create your own Exception and then throw it away.

 public class RepositoryException : Exception { public RepositoryException() : base() { } public RepositoryException(string key, string value) : base() { base.Data.Add(key, value); } public RepositoryException(string message) : base(message) { } public RepositoryException(string message, Exception innerException) : base(message, innerException) { } } public Boolean Delete(Account account) { try { _accountRepository.Delete(account); return true; } catch (Exception ex) { throw new RepositoryException("", "Error when deleting account"); // throw new RepositoryException("Error when deleting account", ex); // OR just // throw new RepositoryException("Error when deleting account"); } } 
+1
source

You can use your own exceptions instead

_errors.Add("", "Error when deleting account");

So, each _errors.Add(..) will be replaced with something like

throw new MyAppException(key, value);

How to create your own exception class has been described above. This way you provide your exception object with a key and value .

You need to know what type of exception you will catch.

 try { Delete(account); } catch(NullPointerException ex) { throw new MyAppException(key, value); } 

And now in your caller methods (external methods) you can catch only your exceptions.

 try { _accountRepository.Delete(account); } catch(MyAppException ex) { //exception handle logic } 
+1
source

Source: https://habr.com/ru/post/1386811/


All Articles