none
C# Using try catch method

    Question

  • so im trying to learn/build a stack Linked list. I stumble upon a problem. what if i use Pop method in a empty stack but i must continue to do the other methods.

    static void Main() { List M = new List(); M.Push(5); M.Pop(); M.Pop();

      M.Push(10); M.Print();

    Output:

    The Error 

    10

    The 2nd Pop() should tell an error(Pop cant be use 2x if stack is empty) but still continue.

    I might know where the error should go but im a bit confuse about the Try and Catch method with a Throw New Argument

    Here is my Pop code;

    public void Pop()
            {
                 
                    Node temp = Head;
    
                    if (CountPosition > 0)
                    {
                        CountPosition--;
                        temp.Data = null;
                        Head = temp.Next;
                    }
    
                 
                if (CountPosition == 0)
                {
                    if (temp == null)
                    {
                        
            // here could be the code inserted                   
                             
                    }
                }        
                    
            }

    And i must make my own Exception method.

    {
        public class IOE : Exception
        {       
            public IOE()
            {            
               
            }
    
            public string ErrorMessage()
            {
                string Letter = "IllegalOperationException";
                return Letter;
            }
            
             
             
    
        }
    }

    so What is a easy and good code practice for a Throw with Catch and Try Method for my Problem?



    • Edited by k9nneo1191 Wednesday, March 29, 2017 11:13 AM
    Wednesday, March 29, 2017 11:09 AM

Answers

  • I'm guessing that you know that .Net already has a Stack class and that creating your own is an exercise on your part?

    You don't really need to make your own exception and I would say its more usual practice to use suitable built-in exceptions (in this case, an InvalidOperationException) where-ever possible.

    If you do create your own exception then its best to follow the appropriate naming convention of <something>Exception.

    To throw an exception you just do:

     throw new MyException("error message");

    To catch it, wrap your code in a try/catch block

    try
    {
       // code that may throw an exception
    }
    catch (MyException me)
    {
      // handle me exception
    }


    You can have multiple catch blocks to handle different kinds of exceptions plus a default catch block for any oher exceptions, all demonstrated here

    Now, you are saying that you want to call Pop() and then continue on even if an exception?

    It is not good practice to use exceptions to control 'program flow'. So the best thing to do here would be to actually check the count before trying to pop if you think there is a chance that it won't work (and that this possibility is expected within your program flow).

    int value = 0; // default value
    
    if (stack.Count()>0)
    {
       value = stack.Pop();
    }
    
    // and so on.

    If you are writing your own stack class and want to handle this situation as a matter of course you could always create a a 'TryPop()' method that returns true or false indicating whether it worked or not and the actual value in an 'out' parameter (see the int.Parse and int.TryParse methods to get an idea of this pattern).

    For example:

    int value;
    
    if (stack.TryPop(out value))
    {
       // use value
       // etc
    }

    • Marked as answer by k9nneo1191 Wednesday, March 29, 2017 1:31 PM
    Wednesday, March 29, 2017 12:18 PM

All replies

  • I'm guessing that you know that .Net already has a Stack class and that creating your own is an exercise on your part?

    You don't really need to make your own exception and I would say its more usual practice to use suitable built-in exceptions (in this case, an InvalidOperationException) where-ever possible.

    If you do create your own exception then its best to follow the appropriate naming convention of <something>Exception.

    To throw an exception you just do:

     throw new MyException("error message");

    To catch it, wrap your code in a try/catch block

    try
    {
       // code that may throw an exception
    }
    catch (MyException me)
    {
      // handle me exception
    }


    You can have multiple catch blocks to handle different kinds of exceptions plus a default catch block for any oher exceptions, all demonstrated here

    Now, you are saying that you want to call Pop() and then continue on even if an exception?

    It is not good practice to use exceptions to control 'program flow'. So the best thing to do here would be to actually check the count before trying to pop if you think there is a chance that it won't work (and that this possibility is expected within your program flow).

    int value = 0; // default value
    
    if (stack.Count()>0)
    {
       value = stack.Pop();
    }
    
    // and so on.

    If you are writing your own stack class and want to handle this situation as a matter of course you could always create a a 'TryPop()' method that returns true or false indicating whether it worked or not and the actual value in an 'out' parameter (see the int.Parse and int.TryParse methods to get an idea of this pattern).

    For example:

    int value;
    
    if (stack.TryPop(out value))
    {
       // use value
       // etc
    }

    • Marked as answer by k9nneo1191 Wednesday, March 29, 2017 1:31 PM
    Wednesday, March 29, 2017 12:18 PM
  • thank RJP1973. It was really Helpful.
    Wednesday, March 29, 2017 12:58 PM
  • Hi k9nneo1191,

    If your issue is solved please Mark as answer or Vote as helpful post to the appropriate answer so that it will help other members to find solution if they faces similar issue.

    Your understanding and cooperation will be grateful.

    Thanks,
    Sabah Shariq

    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered "Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]

    • Marked as answer by k9nneo1191 Wednesday, March 29, 2017 1:31 PM
    • Unmarked as answer by k9nneo1191 Wednesday, March 29, 2017 1:31 PM
    Wednesday, March 29, 2017 1:25 PM
    Moderator