.Net Exception Not Propagating

Hi, an error is occurring in my app (see below). I've fixed the error but what concerns me is that the exception was unhandled and the application crashes out. I have a Try/Catch block further up the stack that's designed to catch any exception and I can't understand why the error is not caught here.

My understanding of exception handling in .Net is that the error should propagate all the way back to main and only then if there's no user error handling in place through an unhandled exception.

Any help would be appreciated

System.ArgumentOutOfRangeException was unhandled by user code
  HResult=-2146233086
  Message=Index and length must refer to a location within the string.
Parameter name: length
  ParamName=length
  Source=mscorlib
  StackTrace:
       at System.String.InternalSubStringWithChecks(Int32 startIndex, Int32 length, Boolean fAlwaysCopy)
       at System.String.Substring(Int32 startIndex, Int32 length)
       at MasMobile.Tablet.UI.AddPaymentWindow.PopulateWorldNetFields(String emailAddress, String sitePhone1) in C:\PhoneWatch_Software_Projects\MasMobileTablet\MASMobile.Tablet.UI\AddPaymentWindow.xaml.vb:line 66
       at MasMobile.Tablet.UI.AddPaymentWindow.Initialise(String emailAddress, String sitephone1) in C:\PhoneWatch_Software_Projects\MasMobileTablet\MASMobile.Tablet.UI\AddPaymentWindow.xaml.vb:line 42
       at MasMobile.Tablet.UI.AddPaymentWindow..ctor(String emailAddress, String sitePhone1) in C:\PhoneWatch_Software_Projects\MasMobileTablet\MASMobile.Tablet.UI\AddPaymentWindow.xaml.vb:line 24
       at MasMobile.Tablet.UI.PaymentsWindow.AddPayment() in C:\PhoneWatch_Software_Projects\MasMobileTablet\MASMobile.Tablet.UI\PaymentsWindow.xaml.vb:line 50
       at MasMobile.Tablet.UI.PaymentsWindow.btnAddPayment_Click(Object sender, RoutedEventArgs e) in C:\PhoneWatch_Software_Projects\MasMobileTablet\MASMobile.Tablet.UI\PaymentsWindow.xaml.vb:line 46
       at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
       at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
       at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)
       at System.Windows.UIElement.RaiseEvent(RoutedEventArgs e)
       at System.Windows.Controls.Primitives.ButtonBase.OnClick()
       at System.Windows.Controls.Button.OnClick()
       at System.Windows.Controls.Primitives.ButtonBase.OnMouseLeftButtonUp(MouseButtonEventArgs e)
       at System.Windows.UIElement.OnMouseLeftButtonUpThunk(Object sender, MouseButtonEventArgs e)
       at System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget)
       at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
       at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
       at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
       at System.Windows.UIElement.ReRaiseEventAs(DependencyObject sender, RoutedEventArgs args, RoutedEvent newEvent)
       at System.Windows.UIElement.OnMouseUpThunk(Object sender, MouseButtonEventArgs e)
       at System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget)
       at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
       at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
       at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
       at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)
       at System.Windows.UIElement.RaiseTrustedEvent(RoutedEventArgs args)
       at System.Windows.UIElement.RaiseEvent(RoutedEventArgs args, Boolean trusted)
       at System.Windows.Input.InputManager.ProcessStagingArea()
       at System.Windows.Input.InputManager.ProcessInput(InputEventArgs input)
       at System.Windows.Input.InputProviderSite.ReportInput(InputReport inputReport)
       at System.Windows.Interop.HwndMouseInputProvider.ReportInput(IntPtr hwnd, InputMode mode, Int32 timestamp, RawMouseActions actions, Int32 x, Int32 y, Int32 wheel)
       at System.Windows.Interop.HwndMouseInputProvider.FilterMessage(IntPtr hwnd, WindowMessage msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
       at System.Windows.Interop.HwndSource.InputFilterMessage(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
       at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
       at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
       at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
       at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler)
  InnerException:

Open in new window

monserobAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jacques Bourgeois (James Burger)PresidentCommented:
Exceptions do propagate along the call stack. But it could be hard to follow the path through a call stack.

Is the method in which you have a Try...Catch listed in the call stack presented by the exception information? If not, then you Try...Catch is not implicated. If yes, are you sure that the call that leads to the line on which the error happened is inside the Try...Catch?

Do you know that you can trap almost all errors in the application by trapping unhandled exceptions events, with code such as the following:
Public Sub Main

	AddHandler AppDomain.CurrentDomain.UnhandledException, AddressOf UnhandledExceptionHandler
	AddHandler Application.ThreadException, AddressOf ThreadExceptionHandler

	'Sub Main code

End Sub

Private Sub ThreadExceptionHandler(ByVal sender As Object, ByVal e As Threading.ThreadExceptionEventArgs)
		'Global error handling
End Sub

Private Sub UnhandledExceptionHandler(ByVal sender As Object, ByVal e As UnhandledExceptionEventArgs)
		'Global error handling
End Sub

Open in new window

0
monserobAuthor Commented:
Hi, the method that has the Try/Catch is not listed in the call stack, which as you suggest means it's not getting that far...

To recreate the issue I've created a simple app, which has 4 windows (window 1, window 2 etc.) and each window contains a button that opens the next window in the sequence. In the constructor of window 4 I'm throwing an exception. The try/Catch is in the button click event of window 1 wrapped around the call to open window 2. When I navigate to window 4 I get an unhandled exception i.e. just like in my app the Try/Catch is ignored

Can you explain why this is so that I can better understand what's going on. In the meantime I'm going to look at your global exception handling suggestion

Thanks
0
monserobAuthor Commented:
I've tried implementing the code above (repasted below) to trap unhandled exceptions and after a lot of faffing around I've come to the conclusion that the code supplied does not work as is in a WPF application. Could you supply a WPF equivalent?

Public Sub Main

	AddHandler AppDomain.CurrentDomain.UnhandledException, AddressOf UnhandledExceptionHandler
	AddHandler Application.ThreadException, AddressOf ThreadExceptionHandler

	'Sub Main code

End Sub

Private Sub ThreadExceptionHandler(ByVal sender As Object, ByVal e As Threading.ThreadExceptionEventArgs)
		'Global error handling
End Sub

Private Sub UnhandledExceptionHandler(ByVal sender As Object, ByVal e As UnhandledExceptionEventArgs)
		'Global error handling
End Sub 

Open in new window

0
Bootstrap 4: Exploring New Features

Learn how to use and navigate the new features included in Bootstrap 4, the most popular HTML, CSS, and JavaScript framework for developing responsive, mobile-first websites.

Jacques Bourgeois (James Burger)PresidentCommented:
I am not an expert of WPF, but I think that Application.DispatcherUnhandledException provides the same type of feature: http://msdn.microsoft.com/en-us/library/system.windows.application.dispatcherunhandledexception.aspx

Cannot say if the ThreadException should also be handled as is the case for Windows Applications.
0
Jacques Bourgeois (James Burger)PresidentCommented:
Sorry, I did not see your first feedback to my post.

As I told you, I am not an expert on WPF, so I am not aware of how things go between 2 windows in that environment. WPF has been built to minimize the differences between an application that runs in Windows vs an application that runs in a browser. I would thus not be surprised to learn that under the hood, it works more like a web application, where each page is a little isolated program. If such, then you cannot expect an exception to propagate from one window to another.
0
monserobAuthor Commented:
I've decided to implement the approach detailed in the link below:

http://devcenter.auburnrandall.com/wpfunhandledexceptions.aspx

I still don't understand why Try/Catch statements further up the call stack are not being hit and the suggested fix was not really specific to WPF (not suggesting that it couldn't be made to work with WPF though)
0
Jacques Bourgeois (James Burger)PresidentCommented:
The approach that you have decided to implement just links back to the exact same page that I pointed to you.

As I told you before, while in a standard Windows Application, most of the code is linked, in a WPF application, as I understand it, each page is like a little individual program, so each run on their own call stack, preventing the exception from climbing up. There is also the fact that errors that trigger in a thread won't move up to the initiator of the thread, you have to trap them in a ThreadException event (at least in a Windows application).
0
monserobAuthor Commented:
Well, I don't agree that what you suggested links back to the exact same page that explains how to implement a global exception handling strategy in WPF; I'm using the DispatcherUnhandledException, which I had to go and discover myself and is not mentioned at all in your code sample.

As for your suggestion as to why the exception doesn't propagate can you provide a link that backs up your understanding please
0
Jacques Bourgeois (James Burger)PresidentCommented:
Click my link again. It brings you directly to the official Microsost documentation for the Application.DispatcherUnhandledException Event. The same page that is pointed to as an implication in the link that you presented.

For the way pages are handled by the system, you have only to look at the documentation again for Navigation Oveview page of the WPF documentation : http://msdn.microsoft.com/en-us/library/ms750478.aspx
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
monserobAuthor Commented:
Sorry, you posted two comments in a row and I only say your 2nd comment! Missed the one with the MSDN link
0
Jacques Bourgeois (James Burger)PresidentCommented:
Apologies accepted :-)

I hope this information will help you go forward in that grand adventure of learning to work in that universe that is .NET.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.