问题描述:
运行程序出现此错误 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,而且程序也不报告错?
运行程序出现此错误 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,而且程序也不报告错?
{
int i=8;
}
catch(System.Exception e)
{
Console.WriteLine(e.Message);
}
finally
{
return i;
}
你应该运行一下你的代码试试,不要想当然。
{
int i=8;
return i;
}
catch(System.Exception e)
{
Console.WriteLine(e.Message);
return 0;
}
{
int i=8;
return i;
}
catch(System.Exception e)
{
Console.WriteLine(e.Message);
return 0;
}
支持楼上的...这个才正确啊..
try
{
i=8
}
catch(System.Exception e)
{
Console.WriteLine(e.Message);}
return i;
{
int i = 0;
try
{
i = 8;
}
catch(System.Exception e)
{
Console.WriteLine(e.Message);
}
return i;
}
{
int i = 0;
try
{
i = 8;
}
catch(System.Exception e)
{
Console.WriteLine(e.Message);
}
return i;
}
{
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;
}
}
}