C# 处理不同异常的不同方面
从我所做的阅读中,我希望我能够创建从OneExceptionSpect继承的不同方面,从而允许我在代码中以不同的方式处理不同的异常 为此,我创建了两个方面类,如下所示:C# 处理不同异常的不同方面,c#,postsharp,onexception,C#,Postsharp,Onexception,从我所做的阅读中,我希望我能够创建从OneExceptionSpect继承的不同方面,从而允许我在代码中以不同的方式处理不同的异常 为此,我创建了两个方面类,如下所示: [Serializable] [AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true, Inherited = false)] public class CommonExceptionAspect : OnExceptionAspect { publ
[Serializable]
[AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true, Inherited = false)]
public class CommonExceptionAspect : OnExceptionAspect
{
public override void OnException(MethodExecutionArgs args)
{
string msg = string.Format("{0} had an error @ {1}: {2}\n{3}",
args.Method.Name, DateTime.Now,
args.Exception.Message, args.Exception.StackTrace);
Trace.WriteLine(msg);
if (args.Exception.GetType() != typeof (PppGeneralException))
{
throw new Exception("There was a problem");
}
else
{
args.FlowBehavior = FlowBehavior.RethrowException;
}
}
}
public void EstablishConnection(string connectionString, DateTime d1, int connections)
{
Thread.Sleep(5000);
if (connectionString.Length > 0)
{
throw new ExternalException("This is my external exception");
}
}
public void StopDatabase(string connectionString)
{
Thread.Sleep(5000);
if (connectionString.Length > 0)
{
throw new AuthenticationException("This is my detailed exception");
}
else
{
throw new ArgumentException("This is just an argument exception");
}
}
和另一个方面类:
[Serializable]
[AttributeUsage(AttributeTargets.Assembly | AttributeTargets.Class | AttributeTargets.Method)]
[MulticastAttributeUsage(MulticastTargets.Method, TargetMemberAttributes = (MulticastAttributes.Public | MulticastAttributes.NonAbstract | MulticastAttributes.Instance | MulticastAttributes.UserGenerated))]
public class AuthenticationExceptionAspect : OnExceptionAspect
{
public override void OnException(MethodExecutionArgs args)
{
string msg = string.Format("{0} had an error @ {1}: {2}\n{3}",
args.Method.Name, DateTime.Now,
args.Exception.Message, args.Exception.StackTrace);
Trace.WriteLine(msg);
throw new Exception("You are not authorized!");
}
public override Type GetExceptionType(System.Reflection.MethodBase targetMethod)
{
return typeof(AuthenticationException);
}
}
我的调用者方法如下:
[Serializable]
[AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true, Inherited = false)]
public class CommonExceptionAspect : OnExceptionAspect
{
public override void OnException(MethodExecutionArgs args)
{
string msg = string.Format("{0} had an error @ {1}: {2}\n{3}",
args.Method.Name, DateTime.Now,
args.Exception.Message, args.Exception.StackTrace);
Trace.WriteLine(msg);
if (args.Exception.GetType() != typeof (PppGeneralException))
{
throw new Exception("There was a problem");
}
else
{
args.FlowBehavior = FlowBehavior.RethrowException;
}
}
}
public void EstablishConnection(string connectionString, DateTime d1, int connections)
{
Thread.Sleep(5000);
if (connectionString.Length > 0)
{
throw new ExternalException("This is my external exception");
}
}
public void StopDatabase(string connectionString)
{
Thread.Sleep(5000);
if (connectionString.Length > 0)
{
throw new AuthenticationException("This is my detailed exception");
}
else
{
throw new ArgumentException("This is just an argument exception");
}
}
AssemblyInfo.cs文件中有以下条目:
[assembly: CommonExceptionAspect(AspectPriority = 3, AttributePriority = 1)]
[assembly: CommonExceptionAspect(AttributeExclude = true, AspectPriority = 3, AttributePriority = 0, AttributeTargetMembers = "ConsoleApplication3.ConnectionManager.StopDatabase", AttributeTargetTypeAttributes = MulticastAttributes.Public, AttributeTargetMemberAttributes = MulticastAttributes.Public, AttributeTargetElements = MulticastTargets.Method)]
[assembly: AuthenticationExceptionAspect(AspectPriority = 1)]
我的期望是,当第一个调用方方法引发“ExternalException”时,“CommonExceptionSpect”的OneException方法将处理它。当从第二个调用方方法引发“AuthenticationException”时,“AuthenticationExceptionSpect”的OneException方法
但在这两种情况下,调用都指向“CommonExceptionSpect”。有人能告诉我我做错了什么吗?如果这种理解是错误的,并且这种情况完全有可能实现
感谢提前加载。对于
AspectPriority
而言,数字越大表示将首先应用方面(或者更准确地说是转换)。因此,在您的案例中,您首先应用了CommonExceptionAspect
,无论其他方面如何,此方面都将接收所有异常
你需要以另一种方式设置优先级
作为旁注-多个OneException(或OnMethodBoundary)方面的组合不会使用多个catch语句创建单个try-catch块,而是会创建多个嵌套的try-catch块。因此,行为可能不同。欢迎使用StackOverflow!请看,这里的共识是“不,他们不应该”。另外,值得注意的是,如果我从AuthenticationExceptionSpect引发一般异常,那么它会在CommonExceptionSpect中被捕获,这再次隐藏了我希望在AuthenticationExceptionSpect引发的异常中传递的实际信息。引发可以在CommonExceptionAspect中忽略的特定类型的异常,并将FlowBehavior设置为Rethrow,以允许解决此问题。再次感谢!