try catch一个有趣的问题。
static public int AppendOneFileToAnother() { StreamWriter sw = null; StreamReader sr = null; string tempLineStr = string.Empty; try { return 1; } catch (Exception ex) { Console.Write("AppendFileErro :" + ex.Message); logger.Error("AppendFileErro :" + ex.Message); throw ex; } finally { sw.Close(); sr.Close(); } }static int Main(string[] args) { try {return 1;} catch (Exception ex) { Console.Write("AppendFileErro :" + ex.Message); logger.Error("AppendFileErro :" + ex.Message); throw ex; } finally { }using System;using System.Collections.Generic;using System.Text;namespace ConsoleApplication1{ class Program { static public int AppendOneFileToAnother() { try { return 1; } catch (Exception ex) { Console.Write("AppendFileErro :" + ex.Message); throw ex; } finally { } } static int Main(string[] args) { try { return 1; } catch (Exception ex) { Console.Write("AppendFileErro :" + ex.Message); throw ex; } finally { } } }}
[解决办法]
没报错 `
[解决办法]
回复内容太短了!。。。
我也想知道原因
[解决办法]
static public int AppendOneFileToAnother()
{
StreamWriter sw = null;
int returnNumber=0;
StreamReader sr = null;
string tempLineStr = string.Empty;
try
{
//todo
}
catch (Exception ex)
{
returnNumber=-1;
Console.Write("AppendFileErro :" + ex.Message);
logger.Error("AppendFileErro :" + ex.Message);
throw ex;
}
finally
{
sw.Close();
sr.Close();
}
return returnNumber;
}
最好不要那么写。没有意义的话。
虽然没什么错。
我靠
我今天又她妈帅了
[解决办法]
main函数还要throw ex....
[解决办法]
普通的函数lz的写法自然没错,因为throw异常出去也算是一种return。但是lz所示的Main函数必须有返回值,否则throw的这个异常的写法就是有问题的啦,你打算让CLR帮你处理这个异常?