Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I have a WPF .Net5 VB.Net application with many local RDLC reports,

Some reports when rendering the report make the application enter break mode with:

Attempt to divide by zero exception

I try other reports until one of them loads correctly, then the previous ones the caused the exception run correctly with the same exact data..

Once I close the application and open it again, the reports show the exception again..

There is nothing in my report definition code that attempts division at all, by zero or otherwise.

EDIT:

got the following details:

System.DivideByZeroException
  HResult=0x80020012
  Message=Attempted to divide by zero.
  Source=System.Drawing.Common
  StackTrace:
   at System.Drawing.ImageAnimator.ImageInfo.AdvanceAnimationBy(Int64 milliseconds)
   at System.Drawing.ImageAnimator.AnimateImages()
   at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
--- End of stack trace from previous location ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Threading.ThreadHelper.ThreadStart()

  This exception was originally thrown at this call stack:
    [External Code]




What I have tried:

1. Added a handler for DispatcherUnhandledException event, but the exception is not caught by it.

2. Intentionally added a divide by zero code to the report, but that code doesn't cause an exception, the field just displays an infinity symbol.
Posted
Updated 22-Aug-21 22:16pm
v4
Comments
[no name] 21-Aug-21 13:19pm    
You know "some" reports are a problem. You're just guessing until you determine which ones are the problem. Then you need to determine why.

Chances are you've forgotten an initialization value of some form, that is set by the time later reports are done - but we can't help you fix that at all!

Contact the people who wrote it, and ask them - RDLC is an MS product, and their tech support isn't too bad (if a bit on the slow side).
 
Share this answer
 
Comments
Ahmad_kelany 21-Aug-21 8:04am    
Thanks for replying

I was able to get more info about the exception,

I added exception info to it..

Does this help?
After a lot of searching:

The exception is caused by external code, namely the loading animation that appears when the reportviewer is rendering the report.

That external code is something that I cannot change..

Communicating with Microsoft support proved to be a dead end.

The only thing in my power is to find a way to prevent the exception from terminating the application.

You'll find some answers on the web that suggests adding the following line to the app.config file:

<configuration>
  <runtime>
    <legacyUnhandledExceptionPolicy enabled="1" />
  </runtime>
</configuration>


but that doesn't work with .Net 5
====================================

The solution for me was:
in the Application.xaml.vb file, I added the following code:

Private Sub Application_Startup(sender As Object, e As StartupEventArgs)
        
        SetupExceptionHandling()
    End Sub

    Private Sub SetupExceptionHandling()
        AddHandler AppDomain.CurrentDomain.UnhandledException, Sub(s, e)
                                                                   Dim x = CType(e.ExceptionObject, Exception)
                                                                   If x.GetType() = GetType(OutOfMemoryException) Then
                                                                       LogError("Out Of Memory Exception")
                                                                       Application.Current.Shutdown()
                                                                   Else
                                                                       LogException(CType(e.ExceptionObject, Exception))
                                                                       Thread.CurrentThread.Join()
                                                                   End If
                                                               End Sub

        AddHandler DispatcherUnhandledException, Sub(s, e)
                                                     LogException(e.Exception)
                                                     e.Handled = True
                                                 End Sub

        AddHandler TaskScheduler.UnobservedTaskException, Sub(s, e)
                                                              LogException(e.Exception)
                                                              e.SetObserved()
                                                          End Sub
    End Sub
    Private Sub LogException(ex As Exception)
        //your error logging code
    End Sub


The key points here are:
-handling the AppDomain.CurrentDomain.UnhandledException as the exception is not caught by the DispatcherUnhandledException

-Thread.CurrentThread.Join() this is the line that prevents the app from terminating.

-some could argue that this is a bad idea since it allows the app to continue in a potentially incorrect state..
which is a valid point, alas I couldn't find a better solution to prevent code that I have no control over from terminating the application, and thus I excluded OutOfMemoryException from the stop termination process..
and might exclude other types of exceptions or add some logic to guard against faulty state..
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900