C# 在catch子句中尝试catch
我目前正在以不同的方式加载图像,如下所示:C# 在catch子句中尝试catch,c#,try-catch,C#,Try Catch,我目前正在以不同的方式加载图像,如下所示: try { // way 1 } catch { // way 1 didn't work try { // way 2 } catch { // etc. } } private static MyImage LoadFirstWay(string name) { return ... } private static MyImage LoadSecondWay(strin
try {
// way 1
}
catch
{ // way 1 didn't work
try {
// way 2
}
catch
{
// etc.
}
}
private static MyImage LoadFirstWay(string name) {
return ...
}
private static MyImage LoadSecondWay(string name) {
return ...
}
private static MyImage LoadThirdWay(string name) {
return ...
}
...
public MyImage LoadImage(string name) {
Func<string,MyImage>[] waysToLoad = new Func<string,MyImage>[] {
LoadFirstWay
, LoadSecondWay
, LoadThirdWay
};
foreach (var way in waysToLoad) {
try {
return way(name);
} catch (Exception e) {
Console.Error("Warning: loading of '{0}' failed, {1}", name, e.Message);
}
}
return null;
}
我想知道是否有更干净的方法来做这件事。目前这不是一个问题,但如果我再添加一些方法,它会变得一团糟。请注意,加载映像的方法也以相同的方式尝试捕获,因为它可能不是映像。
它基本上是在尝试一堆东西来弄清楚您将什么拖到应用程序中。您可以编写一个方法,接受任意数量的委托,尝试所有委托,并在其中一个成功运行后停止。这将异常处理抽象到一个位置,并避免所有重复:
public static void AttemptAll(params Action[] actions)
{
var exceptions = new List<Exception>();
foreach (var action in actions)
{
try
{
action();
return;
}
catch (Exception e)
{
exceptions.Add(e);
}
}
throw new AggregateException(exceptions);
}
如果这些方法计算结果,您也可以创建第二个重载来处理该结果:
public static T AttemptAll<T>(params Func<T>[] actions)
{
var exceptions = new List<Exception>();
foreach (var action in actions)
{
try
{
return action();
}
catch (Exception e)
{
exceptions.Add(e);
}
}
throw new AggregateException(exceptions);
}
publicstatict尝试调用(params Func[]actions)
{
var exceptions=新列表();
foreach(行动中的var行动)
{
尝试
{
返回动作();
}
捕获(例外e)
{
例外情况。添加(e);
}
}
抛出新的AggregateException(异常);
}
您可以编写一个方法,该方法接受任意数量的委托,尝试所有委托,并在其中一个委托成功运行后停止。这将异常处理抽象到一个位置,并避免所有重复:
public static void AttemptAll(params Action[] actions)
{
var exceptions = new List<Exception>();
foreach (var action in actions)
{
try
{
action();
return;
}
catch (Exception e)
{
exceptions.Add(e);
}
}
throw new AggregateException(exceptions);
}
如果这些方法计算结果,您也可以创建第二个重载来处理该结果:
public static T AttemptAll<T>(params Func<T>[] actions)
{
var exceptions = new List<Exception>();
foreach (var action in actions)
{
try
{
return action();
}
catch (Exception e)
{
exceptions.Add(e);
}
}
throw new AggregateException(exceptions);
}
publicstatict尝试调用(params Func[]actions)
{
var exceptions=新列表();
foreach(行动中的var行动)
{
尝试
{
返回动作();
}
捕获(例外e)
{
例外情况。添加(e);
}
}
抛出新的AggregateException(异常);
}
假设加载映像的“不同方式”都会在失败时引发异常,您可以迭代不同的方式,直到其中一种方式成功。下面的示例使用函数
显示一个无参数函数,该函数在成功后返回图像。在具体的示例中,函数中可能也包含参数
List<Function<Image>> imageLoaders = LoadTheListSomehow();
foreach (var loader in imageLoaders)
{
try
{
var image = loader();
break; // We successfully loaded the image
}
catch (Exception ex)
{
// Log the exception if desired
}
}
List-imageLoaders=loadthelist();
foreach(imageLoaders中的var加载程序)
{
尝试
{
var image=loader();
break;//我们成功加载了映像
}
捕获(例外情况除外)
{
//如果需要,记录异常
}
}
假设加载映像的“不同方式”都会在失败时引发异常,您可以迭代不同的方式,直到其中一种方式成功。下面的示例使用函数
显示一个无参数函数,该函数在成功后返回图像。在具体的示例中,函数中可能也包含参数
List<Function<Image>> imageLoaders = LoadTheListSomehow();
foreach (var loader in imageLoaders)
{
try
{
var image = loader();
break; // We successfully loaded the image
}
catch (Exception ex)
{
// Log the exception if desired
}
}
List-imageLoaders=loadthelist();
foreach(imageLoaders中的var加载程序)
{
尝试
{
var image=loader();
break;//我们成功加载了映像
}
捕获(例外情况除外)
{
//如果需要,记录异常
}
}
那里的嵌套看起来确实不必要。我将隔离将图像加载到其自己的私有方法中的每种方法,然后在循环中将这些方法作为委托调用,如下所示:
try {
// way 1
}
catch
{ // way 1 didn't work
try {
// way 2
}
catch
{
// etc.
}
}
private static MyImage LoadFirstWay(string name) {
return ...
}
private static MyImage LoadSecondWay(string name) {
return ...
}
private static MyImage LoadThirdWay(string name) {
return ...
}
...
public MyImage LoadImage(string name) {
Func<string,MyImage>[] waysToLoad = new Func<string,MyImage>[] {
LoadFirstWay
, LoadSecondWay
, LoadThirdWay
};
foreach (var way in waysToLoad) {
try {
return way(name);
} catch (Exception e) {
Console.Error("Warning: loading of '{0}' failed, {1}", name, e.Message);
}
}
return null;
}
私有静态MyImage LoadFirstWay(字符串名称){
返回。。。
}
私有静态MyImage LoadSecondWay(字符串名称){
返回。。。
}
私有静态MyImage LoadThirdWay(字符串名称){
返回。。。
}
...
公共MyImage LoadImage(字符串名称){
Func[]waysToLoad=新Func[]{
装货第一道
,第二条路
,第三次装货
};
foreach(方式装载中的var方式){
试一试{
返回方式(名称);
}捕获(例外e){
错误(“警告:加载“{0}”失败,{1}”,名称,e.Message);
}
}
返回null;
}
那里的嵌套看起来确实不必要。我将隔离将图像加载到其自己的私有方法中的每种方法,然后在循环中将这些方法作为委托调用,如下所示:
try {
// way 1
}
catch
{ // way 1 didn't work
try {
// way 2
}
catch
{
// etc.
}
}
private static MyImage LoadFirstWay(string name) {
return ...
}
private static MyImage LoadSecondWay(string name) {
return ...
}
private static MyImage LoadThirdWay(string name) {
return ...
}
...
public MyImage LoadImage(string name) {
Func<string,MyImage>[] waysToLoad = new Func<string,MyImage>[] {
LoadFirstWay
, LoadSecondWay
, LoadThirdWay
};
foreach (var way in waysToLoad) {
try {
return way(name);
} catch (Exception e) {
Console.Error("Warning: loading of '{0}' failed, {1}", name, e.Message);
}
}
return null;
}
私有静态MyImage LoadFirstWay(字符串名称){
返回。。。
}
私有静态MyImage LoadSecondWay(字符串名称){
返回。。。
}
私有静态MyImage LoadThirdWay(字符串名称){
返回。。。
}
...
公共MyImage LoadImage(字符串名称){
Func[]waysToLoad=新Func[]{
装货第一道
,第二条路
,第三次装货
};
foreach(方式装载中的var方式){
试一试{
返回方式(名称);
}捕获(例外e){
错误(“警告:加载“{0}”失败,{1}”,名称,e.Message);
}
}
返回null;
}
“为什么”它不起作用?
处理这是一种更好的设计实践,而不是让某个东西抛出错误并希望你用嵌套捕获来覆盖它(特别是对于这样的东西)。如果您试图加载映像时抛出错误,会发生什么情况?我坚信catch
处理程序应该只包含与处理异常相关的代码,而不应包含其他内容。如果您需要类似于示例中的内容,您应该重新构造代码。同意@alykins。第一个异常的原因实际上是异常的,还是更像是“这个文件没有正确的签名,所以我要尝试另一种可能有效的方法”?天哪…这个地方怎么了?向下投票?“为什么”不起作用?
处理这是一种更好的设计实践,而不是让某个东西抛出错误并希望你用嵌套捕获来覆盖它(尤其是对于这样的东西)。如果您试图加载映像时抛出错误,会发生什么情况?我坚信catch
处理程序应该只包含与处理异常相关的代码,而不应包含其他内容。如果您需要类似于示例中的内容,您应该重新构造代码。同意@alykins。第一个例外的原因是事实上的例外还是更严重