我通过反射调用一种可能导致异常的方法.如何在没有包装器反射的情况下将异常传递给调用者?
我正在重新抛出InnerException,但这会破坏堆栈跟踪.
示例代码:
public void test1()
{
// Throw an exception for testing purposes
throw new ArgumentException("test1");
}
void test2()
{
try
{
MethodInfo mi = typeof(Program).GetMethod("test1");
mi.Invoke(this, null);
}
catch (TargetInvocationException tiex)
{
// Throw the new exception
throw tiex.InnerException;
}
}
Run Code Online (Sandbox Code Playgroud) 捕获异常并重新抛出异常时需要考虑哪些最佳实践?我想确保保留Exception对象InnerException和堆栈跟踪.以下代码块在处理此方式时是否存在差异?
try
{
//some code
}
catch (Exception ex)
{
throw ex;
}
Run Code Online (Sandbox Code Playgroud)
VS:
try
{
//some code
}
catch
{
throw;
}
Run Code Online (Sandbox Code Playgroud) 我想阻止对象的进一步处理,如果它是null.
在下面的代码中,我通过以下任一方法检查对象是否为null:
if (!data.Equals(null))
Run Code Online (Sandbox Code Playgroud)
和
if (data != null)
Run Code Online (Sandbox Code Playgroud)
不过,我收到NullReferenceException的dataList.Add(data).如果对象为null,则它应该永远不会进入if-statement!
因此,我问这是否是检查对象是否为null的正确方法:
public List<Object> dataList;
public bool AddData(ref Object data)
bool success = false;
try
{
// I've also used "if (data != null)" which hasn't worked either
if (!data.Equals(null))
{
//NullReferenceException occurs here ...
dataList.Add(data);
success = doOtherStuff(data);
}
}
catch (Exception e)
{
throw new Exception(e.ToString());
}
return success;
}
Run Code Online (Sandbox Code Playgroud)
如果这是检查对象是否为null的正确方法,那么我做错了什么(如何防止对对象进一步处理以避免NullReferenceException)?
我想知道将异常从一种方法传递给另一种方法的正确方法是什么.
我正在开发一个分为Presentation(web),Business和Logic层的项目,并且需要在链中传递错误(例如SqlExceptions)以在出现问题时通知Web层.
我见过3种基本方法:
try
{
//error code
}
catch (Exception ex)
{
throw ex;
}
Run Code Online (Sandbox Code Playgroud)
(只是重新抛出)
try
{
//error code
}
catch (Exception ex)
{
throw new MyCustomException();
}
Run Code Online (Sandbox Code Playgroud)
(抛出自定义异常,以便不传递对数据提供程序的依赖关系)
然后简单地说
//error code
Run Code Online (Sandbox Code Playgroud)
(根本不做任何事情,让错误自己冒出来)
当然,catch块中也会发生一些日志记录.
我更喜欢3号,而我的同事使用方法1,但我们都不能真正激励为什么.
使用每种方法有哪些优点/缺点?有一种我不知道的更好的方法吗?有没有被接受的最佳方式?
在网上引用了很多文档,尤其是关于SO的文档,例如:在C#中重新抛出异常的正确方法是什么? "扔e"之间应该有区别 和"扔".
但是,来自:http://bartdesmet.net/blogs/bart/archive/2006/03/12/3815.aspx,
这段代码:
using System;
class Ex
{
public static void Main()
{
//
// First test rethrowing the caught exception variable.
//
Console.WriteLine("First test");
try
{
ThrowWithVariable();
}
catch (Exception ex)
{
Console.WriteLine(ex.StackTrace);
}
//
// Second test performing a blind rethrow.
//
Console.WriteLine("Second test");
try
{
ThrowWithoutVariable();
}
catch (Exception ex)
{
Console.WriteLine(ex.StackTrace);
}
}
private static void BadGuy()
{
//
// Some nasty behavior.
//
throw new Exception();
}
private …Run Code Online (Sandbox Code Playgroud) 我已经搜索了我的问题的答案,但找不到答案.抱歉,如果答案在那里,我正在重复!
我一直在看try/catch代码......
try
{
//Do whatever
}
catch (Exception ex)
{
MessageBox.Show("Oops, something went wrong!");
}
Run Code Online (Sandbox Code Playgroud)
这将导致警告ex从未使用过.
所以我的问题是......虽然从未使用过ex,但声明中是否有任何好处?有人告诉我,它可能会为堆栈跟踪添加细节吗?有时我看到catch(Exception)会停止警告但是这带来了什么好处呢?如果我要写这个并且不以任何方式使用异常我不会声明ex ...
try
{
//Do whatever
}
catch
{
MessageBox.Show("Oops, something went wrong!");
}
Run Code Online (Sandbox Code Playgroud)
这不是一个大问题,但肯定知道会很好!
谢谢
弗雷德
我有一个调用SOAP接口的类,并获取一个数据数组.但是,如果此请求超时,则会引发异常.这很好.但是,我希望我的程序再次尝试进行此调用.如果它超时,我希望继续拨打这个电话,直到它成功为止.我怎么能做到这一点?
例如:
try
{
salesOrdersArray = MagServ.salesOrderList(sessID, filter);
}
catch
{
?? What Goes Here to FORCE the above line of code to rerun until it succeeds.
}
Run Code Online (Sandbox Code Playgroud) 可能重复:
在C#中重新抛出异常的正确方法是什么?
我想了解为什么"throw ex"用法会隐藏原始堆栈跟踪?在设计c#编译器时,幕后的基本理念是什么?
我正在创建一个TCP连接的服务器.TCP连接在其自己的线程中运行无限长的时间.是否有一个好的模式允许安全关闭TcpListener和客户端以及线程?以下是我到目前为止的情况.
private volatile bool Shudown;
void ThreadStart1()
{
TcpListener listener = null;
TcpClient client = null;
Stream s = null;
try
{
listener = new TcpListener(60000);
client = listener.AcceptTcpClient();
Stream s = client.GetStrea();
while(!Shutdown) // use shutdown to gracefully shutdown thread.
{
try
{
string msg = s.ReadLine(); // This blocks the thread so setting shutdown = true will never occur unless a client sends a message.
DoSomething(msg);
}
catch(IOException ex){ } // I would like to avoid using Exceptions …Run Code Online (Sandbox Code Playgroud) public void DoRiskyThings(List<Action> tasks)
{
List<Exception> exceptions = new List<Exception>();
foreach(Action task in tasks)
{
try {task()}
catch (Exception e) {exceptions.Add(e)};
}
if (exceptions.Any())
{
//... what goes here?
}
}
Run Code Online (Sandbox Code Playgroud)
我想保留所有信息(特别是消息和堆栈跟踪).