How Can I Force Execution to the Catch Block?

后端 未结 12 1220
野趣味
野趣味 2021-01-17 07:46

I am wondering can try..catch force execution to go into the catch and run code in there?

here example code:

try {
    if (         


        
相关标签:
12条回答
  • 2021-01-17 07:49

    An effective way to throw an Exception and also jump to Catch as so:

    try
    {
       throw new Exception("Exception Message");
    }
    catch (Exception e)
    {
       // after the throw, you will land here
    }
    
    0 讨论(0)
  • 2021-01-17 07:53

    Yes, you have to throw exception :

      try
      {
        throw new Exception("hello");
      }
      catch (Exception)
      {
    
         //run some code here...
      }
    
    0 讨论(0)
  • 2021-01-17 07:56
       try{
          if (AnyConditionTrue){
                  //run some code
                   }
          else{
                  throw new Exception();
              }
       }
       catch(){
    
          //run some code here...
    
       }
    

    But like Yuck has stated, I wouldn't recommend this. You should take a step back at your design and what you're looking to accomplish. There's a better way to do it (i.e. with normal conditional flow, instead of exception handling).

    0 讨论(0)
  • 2021-01-17 07:58

    Slight resurrection, but I wanted to add both a sample (primarily like others) and a use case.

    public int GetValueNum(string name)
        {
            int _ret = 0;
    
            try
            {
                Control c = (extendedControls.Single(s => s.ValueName == name) as Control);
    
                if (c.GetType() == typeof(ExtendedNumericUpDown))
                    _ret = (int)((ExtendedNumericUpDown)c).Value;
    
                else
                    throw new Exception();
            }
    
            catch
            {
                throw new InvalidCastException(String.Format("Invalid cast fetching .Value value for {0}.\nExtendedControllerListener.GetValueNum()", name));
            }
    
            return _ret;
        }
    

    In my case, I have custom controls - a handful of controls that use a base Windows.Forms control, but add two bools and a string for tracking, and also automatically get registered to a Singleton List<T> so they can be properly fetched without drilling down through control containers (it's a tabbed form).

    In this case, I'm creating some methods to easily get values (.Value, .Text, .Checked, .Enabled) by a name string. In the case of .Value, not all Control objects have it. If the extended control is not of type ExtendedNumericUpDown, it IS an InvalidCastException as the method should not be called against that type of control. This isn't flow, but the prescribed usage of invalid cast. Since Control doesn't naturally have a .Value property, Visual Studio won't let me just force an attempt and fail after.

    0 讨论(0)
  • 2021-01-17 08:00

    why are you catching an exception? Why not just run the code in your "else" block? If you MUST do it that way, just throw a new exception

    throw new Exception();
    
    0 讨论(0)
  • 2021-01-17 08:03

    Rather than throwing an Exception in the else, I would recommend extracting the code from your catch into a method and call that from your else

    try
    {
        if (AnyConditionTrue)
        {
            MethodWhenTrue();
        }
        else
        {
            HandleError();
        }
    }
    catch(Exception ex)
    {
        HandleError();
    }
    
    0 讨论(0)
提交回复
热议问题