I have this method which i want to throw a new exception from
public void AddProduct(Product product1, Category category)
{
var addPToCat = new Program();
var productExistInCategory = ProductDictionary.Any(x => x.Key == product1.ArticleNumber);
if (!productExistInCategory)
{
ProductDictionary.Add(product1.ArticleNumber, category.Name );
}
}
The AddProduct()
method is called from this method which i want to catch the exception:
public void AddProductToCategory()
{
if (productExist)
{
if (categoryExist != null)
{
categoryExist.AddProduct(product, categoryExist);
try
{
if (productExistInCategoryInCategory)
categoryExist.ProductDictionary.Add(product.ArticleNumber, categoryName);
}
catch
{
Console.WriteLine("Produkten finns redan");
Thread.Sleep(2000);
throw new ArgumentException("Produkten finns redan");
}
}
}
Am i doing it right or is there something wrong?