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

У меня есть приложение 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());

  }

}

person Julien Poulin    schedule 09.06.2009    source источник
comment
Возможно, вы захотите поделиться кодом для этого   -  person AlexCuse    schedule 09.06.2009


Ответы (2)


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

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-й ответ после вопроса.

person Viper    schedule 14.10.2009

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

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

person Simon P Stevens    schedule 09.06.2009
comment
Я создал оба обработчика, но исключение не отображается ни в одном из них. Есть ли другие? - person Julien Poulin; 09.06.2009
comment
В Visual Studio нажмите «Отладка-› Исключения »и установите флажок рядом с« Исключение среды выполнения Common Language ». Это должно заставить отладчик проникнуть в ваш код, как только возникнет исключение, и вы сможете использовать F10 / F11, чтобы пройти по пути, по которому оно следует. - person Simon P Stevens; 10.06.2009