用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编译通过
{
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编译通过
解决方案 »
- 如何获取文件后缀名(非常规需求)
- 引用类库时,类库的app.config配置信息的处理方式?
- C#使用第三方控件的遗留问题
- (急)关于DataGridView中数据读取问题,如果点击DGV中的数据后,怎么把对应行的数据读取出来呢?(里面有详细的问题说明)
- 问一个正则表达式的问题.
- csFile 的命名空间是什么?
- 数据库问题!
- (三个问题----1)如何自动刷新页面
- 我刚才申请了一个花生护照,同时有了一个免费域名(****.vicp.net),请问我可以让这个域名指到我的局域网的WEB服务器的IP(192.168.1.99)吗?
- 求教C#中关于抽样判断数据的时间点的方法
- 未将对象的引用设置到对象的实例
- C# 技术群!~
{
return null;
}
{
try
{
string str = "TestB";
//throw new Exception();
}
catch
{
return Err_str;
}
return str;
}
{
try
{
string str = "TestB";
//throw new Exception();
return str;
}
catch
{
return null;
}
}
//IntelliSense(Sense) 也就是说当执行到catch的时候就没有return 所以编译器才报错??
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;
}
{
try
{
string str = "TestB";
throw new Exception(); //因为这里抛出异常,被下面的catch捕获,下面的代码跳过
return str;//这里应该有个警告,检测到无法访问的代码
}
catch//这里捕获了异常后,没有返回值,也没有再抛出异常,所以报错。
{
}
}
{
try
{
string str = "TestB";
//throw new Exception();
return str;
}
catch{
//错误处理
}
finally
{
return null;
}
}
Not all code paths return a value意味着方法中至少有一条路径不包括这样的出口点,而try{} catch{}就是一种分支路径