Bezdepbonusu7Программирование: статьи и ответы

Вопрос

Исключение проглочено при перетаскивании

Автор вопроса: Julien PoulinОтветов: 2

У меня есть приложение WinForms, в котором я перетаскиваю между двумя TreeView.

В какой-то момент я хочу отклонить действие в базовой бизнес-реализации, поэтому генерирую исключение. Я вижу исключение в окне вывода, но проблема в том, что я не вижу его в пользовательском интерфейсе, и оно не дает сбоев.

Куда делось исключение?

Вот код, описывающий проблему:

private TreeView tvLeft;
private TreeView tvRight;
private Dictionary<string, int> dico = new Dictionary<string, int>();

void tvLeft_DragDrop(object sender, DragEventArgs e) {

  if (e.Data.GetDataPresent(typeof(TreeNode))) {

    var tnSource = (TreeNode) e.Data.GetData(typeof(TreeNode));
    var tnDestination = tvLeft.GetNodeAt(tvLeft.PointToClient(new Point(e.X, e.Y)));

    // if I drag-drop the same node twice, there sould be an Exception
    // since the key is already in the dictionary...
    // ...but I get no Exception in the UI, the Application.ThreadException
    // or Appomain.CurrentDomain.UnhandledException handlers
    dico.Add(tnSource.Name, (new Random()).Next());

  }

}
  • Возможно, вы захотите поделиться кодом для этого AlexCuse 09.06.2009

exceptiondrag-and-dropwinforms

Ответы

Ответ 1 · рейтинг 12 · Viper

Я нашел это объяснение в Интернете:

Even with drag-and-drop within the same application, the drag-and-drop is handled through the standard OLE drag-drop mechanism. From OLE's point of view it's dealing with two applications, the source and the target and decouples them appropriately. Since OLE has been around far longer than .NET, OLE has no concept of a .NET exception and therefore can't communicate an exception from the target back to the source. Even if it could, why should the source care that the target couldn't perform the drop? If you want to handle an exception during a DragDrop event you must handle it within your DragDrop event handler, it won't propagate beyond that event handler because there is a managed to unmanaged to managed code transition between the source and the target.

См. здесь 1-й ответ после вопроса.

Ответ 2 · Simon P Stevens

Исключение, вероятно, происходит где-то в фоновом потоке. вам необходимо создать обработчик для AppDomain.CurrentDomain.UnhandledException или события Application.ThreadException.

См. здесь для более подробной информации.

Перевод вопроса и ответов сообщества Stack Overflow, лицензия CC BY-SA. Оригинал на Stack Overflow.

Похожие материалы