-2

I made three custom exceptions. For the product name, the quantity and the price but when I try to include it in try, it wont show up. I just need the exception to work then make it show up in the Console or Message Box.

This is what it was originally:

 public string Product_Name(string name) {
if (!Regex.IsMatch(name, @"^[a-zA-Z]+$"))
   //Exception here
return name;
}
public int Quantity(string qty) {
if (!Regex.IsMatch(qty, @"^[0-9]"))
  //Exception here
return Convert.ToInt32(qty);
}

public double SellingPrice(string price)
{
if (!Regex.IsMatch(price.ToString(), @"^(\d*\.)?\d+$"))
   //Exception here
return Convert.ToDouble(price);
}

And this is what I have tried:

  public string Product_Name (string name)
    {
        if (!Regex.IsMatch(name, @"^[a-zA-Z]+$"))

            try
            {
                name = txtProductName.Text;
            }
            catch (Exception StringFormattException)
            {
                Console.WriteLine("Error info:" + StringFormattException.InnerException);
            }
            finally
            {
                Console.WriteLine("Executed.");
            }


        return name;
    }

And the same for the other two.

This is the class where I put my custom exceptions:

 class ProductClass
{
    private int _Quantity;
    private double _SellingPrice;
    private string _ProductName, _Category, _ManufacturingDate, _ExpirationDate, _Description;

    public ProductClass(string ProductName, string Category, string MfgDate, string ExpDate, double Price, int Quantity, string Description)
    {
        this._Quantity = Quantity;
        this._SellingPrice = Price;
        this._ProductName = ProductName;
        this._Category = Category;
        this._ManufacturingDate = MfgDate;
        this._ExpirationDate = ExpDate;
        this._Description = Description;
    }

    public string productName
    {
        get
        {
            return this._ProductName;
        }
        set
        {
            this._ProductName = value;
        }
    }

    public string Category
    {
        get
        {
            return this._Category;
        }
        set
        {
            this._Category = value;
        }
    }

    public string manufacturingDate
    {
        get
        {
            return this._ManufacturingDate;
        }
        set
        {
            this._ManufacturingDate = value;
        }
    }

    public string expirationDate
    {
        get
        {
            return this._ExpirationDate;
        }
        set
        {
            this._ExpirationDate = value;
        }
    }

    public string description
    {
        get
        {
            return this._Description;
        }
        set
        {
            this._Description = value;
        }
    }

    public int quantity
    {
        get
        {
            return this._Quantity;
        }
        set
        {
            this._Quantity = value;
        }
    }

    public double sellingPrice
    {
        get
        {
            return this._SellingPrice;
        }
        set
        {
            this._SellingPrice = value;
        }


    }



    class NumberFormattException : Exception 
    { 
        public NumberFormattException(string Quantity) : base(Quantity) { } 
    }

    class StringFormattException : Exception
    {
        public StringFormattException(string Product_Name) : base(Product_Name) { }
    }
    class CurrencyFormatException : Exception
    {
        public CurrencyFormatException( string SellingPrice) : base(SellingPrice) { }
    }



}
Ciel
  • 25
  • 6
  • 1
    Read up on exceptions. A single `try` can have multiple associated `catch` blocks, each with a different exception type. For example, if you have 3 exception types that may be thrown (`Ex1`, `Ex2` and `Ex3`), you could write `try { SomeCode(); } catch (Ex1 e1) { CatchCode1(e1); } catch (Ex2 e2) { CatchCode2(e2); } catch (Ex3 e3) { CatchCode(e3); } finally { FinCode(); }`. Note the format of the catch statement, the exception specification is like a method parameter specification: `catch(ExceptionType exVariable)`. If you have multiple catch blocks, the first one that matches wins – Flydog57 Dec 13 '20 at 05:39

1 Answers1

3

You need to write:

catch (StringFormattException ex)
{
    string msg = ex.Message;
    if ( ex.InnerException != null )
      msg += $" ({ex.InnerException.Message})";
    // Also you can do a loop to get all inners
    Console.WriteLine("String format error: " + msg);
}

Because if you write:

catch (Exception StringFormattException)
{
  ...
}

Here Exception is a type and StringFormattException the variable's name of the contextual instance of the exception like in:

string StringFormattException = "test";

try-catch (C# Reference)