相关疑难解决方法(0)

如何重新抛出InnerException而不会丢失C#中的堆栈跟踪?

我通过反射调用一种可能导致异常的方法.如何在没有包装器反射的情况下将异常传递给调用者?
我正在重新抛出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)

.net c# exception

290
推荐指数
9
解决办法
7万
查看次数

捕获和重新抛出.NET异常的最佳实践

捕获异常并重新抛出异常时需要考虑哪些最佳实践?我想确保保留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)

.net c# exception-handling rethrow

279
推荐指数
8
解决办法
18万
查看次数

检查C#中的对象是否为null

我想阻止对象的进一步处理,如果它是null.

在下面的代码中,我通过以下任一方法检查对象是否为null:

if (!data.Equals(null))
Run Code Online (Sandbox Code Playgroud)

if (data != null)
Run Code Online (Sandbox Code Playgroud)

不过,我收到NullReferenceExceptiondataList.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)?

c# null nullreferenceexception

196
推荐指数
11
解决办法
78万
查看次数

传递异常的正确方法是什么?(C#)

我想知道将异常从一种方法传递给另一种方法的正确方法是什么.

我正在开发一个分为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,但我们都不能真正激励为什么.

使用每种方法有哪些优点/缺点?有一种我不知道的更好的方法吗?有没有被接受的最佳方式?

c# exception

20
推荐指数
4
解决办法
3万
查看次数

扔VS重新抛出:同样的结果?

在网上引用了很多文档,尤其是关于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)

c# exception-handling throw rethrow

10
推荐指数
1
解决办法
3161
查看次数

简单的try/catch没有使用异常

我已经搜索了我的问题的答案,但找不到答案.抱歉,如果答案在那里,我正在重复!

我一直在看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)

这不是一个大问题,但肯定知道会很好!

谢谢

弗雷德

c# error-handling

9
推荐指数
1
解决办法
6262
查看次数

在C#中重复一个函数,直到它不再抛出异常

我有一个调用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# exception

8
推荐指数
2
解决办法
2万
查看次数

为什么"扔前"; 隐藏原始堆栈跟踪

可能重复:
在C#中重新抛出异常的正确方法是什么?

我想了解为什么"throw ex"用法会隐藏原始堆栈跟踪?在设计c#编译器时,幕后的基本理念是什么?

c#

6
推荐指数
1
解决办法
1470
查看次数

正确的方法来中止被阻止的线程

我正在创建一个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)

.net c# multithreading design-patterns

5
推荐指数
2
解决办法
2288
查看次数

发生了多次异常,重新抛出的正确方法是什么?

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)

我想保留所有信息(特别是消息和堆栈跟踪).

c# exception

2
推荐指数
1
解决办法
814
查看次数