用C#很长时间了,刚刚开始注意的问题。private static MyClass TestA()
        {
            MyClass myClass = "TestA";
            try
            {                
                throw new Exception();                
            }
            catch
            {
            }
            return MyClass;            
        }
// TestA没有问题  
private static string TestB()
        {
            
            try
            {
                string str = "TestB";
                //throw new Exception(); 
                return str;                
            }
            catch
            {
            }                       
        }
// TestB编译不通过  错误信息 并非所有的代码
private static string TestC()
        {
            
            try
            {
                string str = "TestC";
                //throw new Exception(); 
                return str;                
            }
            catch( Exception ex )
            {
                throw ex;
            }                       
        }
//TestC编译通过

解决方案 »

  1.   

    catch
    {
    return null;
    }
      

  2.   

    private static string TestB()
            {            
                try
                {
                    string str = "TestB";
                    //throw new Exception();                                
                }
                catch
                {
     return Err_str;
                }    
     return str;                   
            }
      

  3.   

    private static string TestB()
            {
                
                try
                {
                    string str = "TestB";
                    //throw new Exception(); 
                    return str;                
                }
                catch
                {
                    return null;
                }                       
            }
    //IntelliSense(Sense)  也就是说当执行到catch的时候就没有return 所以编译器才报错??
      

  4.   

    看你的要求了,如果出现异常,抛出,而不返回的话,
    private static string TestB()
            {
                
                try
                {
                    string str = "TestB";
                    ...            
                }
                catch(Exception ex)
                {
                    throw ex;
                }   
                return str;                    
            }
    如果你想出现异常则返回null的话:
    private static string TestB()
            {
                
                try
                {
                    string str = "TestB";
                    //throw new Exception(); 
                   ....
                }
                catch
                {
                    return null;
                } 
                return str;                       
            }
      

  5.   

    应该是这样的吧:private static string TestB()
            {
                
                try
                {
                    string str = "TestB";
                    throw new Exception(); //因为这里抛出异常,被下面的catch捕获,下面的代码跳过
                    return str;//这里应该有个警告,检测到无法访问的代码
                }
                catch//这里捕获了异常后,没有返回值,也没有再抛出异常,所以报错。
                {
                }                       
            }
      

  6.   

    private static string TestB()
            {
                
                try
                {
                    string str = "TestB";
                    //throw new Exception(); 
                    return str;                
                }
                catch{
                    //错误处理
                }
                finally
                {
                    return null;
                }              
            }
      

  7.   

    返回和抛出异常都是方法的出口点,区别只是前者是正常出口点,后者是异常出口点。
    Not all code paths return a value意味着方法中至少有一条路径不包括这样的出口点,而try{} catch{}就是一种分支路径