问题描述:
运行程序出现此错误   ReturnProblem()并非所有代码路径都返回值源程序:
using System;namespace ConsoleApplication5
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
Console.WriteLine(ReturnProblem());            
} private static int ReturnProblem()
{
try
{
int i=8;
return i;
}
catch(System.Exception e)
{
Console.WriteLine(e.Message);
}
}
}
}
解决方法:
我完全可以在try块中写return,但这并不是我想要的。
能不能就在try块里面写retrun,而且程序也不报告错?

解决方案 »

  1.   

    try
    {
    int i=8;

    }
    catch(System.Exception e)
    {
    Console.WriteLine(e.Message);
    }
    finally
    {
    return i;
    }
      

  2.   

    hyj_828(水梦) 同志  请不要这么不负责任 好不好?!
     
    你应该运行一下你的代码试试,不要想当然。
      

  3.   

    try
    {
           int i=8;
           return i;
    }
    catch(System.Exception e)
    {
           Console.WriteLine(e.Message);
           return 0;
    }
      

  4.   

    ry
    {
           int i=8;
           return i;
    }
    catch(System.Exception e)
    {
           Console.WriteLine(e.Message);
           return 0;
    }      
    支持楼上的...这个才正确啊..
      

  5.   

    int i=0;
    try

       i=8
    }
    catch(System.Exception e)
    {
           Console.WriteLine(e.Message);}
    return i;
      

  6.   

    private static int ReturnProblem()
    {
             int i = 0;
    try
    {
    i = 8;
    }
    catch(System.Exception e)
    {
    Console.WriteLine(e.Message);
    }
             return i;
    }
      

  7.   

    private static int ReturnProblem()
    {
             int i = 0;
    try
    {
    i = 8;
    }
    catch(System.Exception e)
    {
    Console.WriteLine(e.Message);
    }
             return i;
    }
      

  8.   

    using System;namespace ConsoleApplication5
    {
    class Class1
    {
    [STAThread]
    static void Main(string[] args)
    {
                               int i=ReturnProblem();
                               if(i!=0) Console.WriteLine("Result={0}",i);
    } private static int ReturnProblem()
    {
                               int i = 0;
    try
    {
    i=8;
    }
    catch(System.Exception e)
    {
    Console.WriteLine(e.Message);
    }
                               return i; 
    }
    }
    }
      

  9.   

    return 不能写在finally里面,这样不能通过编译。
      

  10.   

    一般的地规则,一个函数最好以一个出口,就是上面的最后的一个return i,所以一般会在函数中作一些设置。