xsharp.eu • Suggestions for conversion VO programs using VO GUI classes - Page 2
Page 2 of 2

Suggestions for conversion VO programs using VO GUI classes

Posted: Sun Jun 18, 2017 11:43 pm
by ppiko
Hi guys,

I'll come out of lurker mode ;)

Years ago when I did conference sessions on exception handling I suggested looking at this for ideas: C# Exception Handler. Of course it is not just for C#, and can be applied to any .NET language.

And don't forget to do something about any exceptions that aren't explicitly handled. Put these into your startup code as required:
  • AppDomain.UnhandledException= Notification of uncaught exceptions
  • Application.ThreadException= Triggered by exceptions that occur in Windows Forms threads; by default doesn’t trigger UnhandledException
  • Application.DispatcherUnhandledException= Triggered by exceptions occurring in WPF UI
E.g.

In XAML
<Application x:Class="ExceptionExampleWPF.App"
xmlns="http://schemas.microsoft.com/winfx/2006 ... esentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
DispatcherUnhandledException="MyDispatcherUnhandledExceptionLogger"
StartupUri="Window1.xaml">
</Application>

In code
PROTECTED METHOD OnStartup(e AS StartupEventArgs) AS VOID
// Vulcan
Application.Current:DispatcherUnhandledException += ;
DispatcherUnhandledExceptionEventHandler{SELF,;
@MyDispatcherUnhandledExceptionLogger()}
SUPER:OnStartUp(e)
RETURN

Regards,
Paul

Suggestions for conversion VO programs using VO GUI classes

Posted: Mon Jun 19, 2017 6:25 am
by wriedmann
Hi Chris,

thank you very much!

Wolfgang

Suggestions for conversion VO programs using VO GUI classes

Posted: Mon Jun 19, 2017 6:45 am
by wriedmann
Hi Paul,

your contribution is very welcome! It's a long time I have read something from you, and I'm really happy that you are here.
The Exception handler link is very interesting, thank you!

Wolfgang

Suggestions for conversion VO programs using VO GUI classes

Posted: Mon Jun 19, 2017 8:56 am
by NickFriend
Hi Chris,

We use something like this for error logging in conjunction with Log4Net. I'd recommend this to everyone... very simple to implement and use and very flexible.

Nick

Suggestions for conversion VO programs using VO GUI classes

Posted: Mon Jun 19, 2017 2:08 pm
by Chris
That's very nice hearing from you Paul!
It's been a while, hope you're doing well and thanks for contributing!

Chris

Suggestions for conversion VO programs using VO GUI classes

Posted: Mon Jun 19, 2017 2:14 pm
by Chris
Hi Nick,

Thanks for the pointer about Log4Net! But what did you mean you use in conjunction with it? I think you meant Paul's code?

Chris

Suggestions for conversion VO programs using VO GUI classes

Posted: Mon Jun 19, 2017 3:21 pm
by NickFriend
Hi Chris,

In our base classes we have some common error logging code which receives the Exception object from the Catch, and does the loop through the InnerExceptions as you showed, then the output can get logged via Log4Net.

Very simple and gives good clear log info for clearing up errors detected at runtime (never happens of course), or just for general logging to see what the program is doing.

Nick

Suggestions for conversion VO programs using VO GUI classes

Posted: Tue Jun 20, 2017 10:57 am
by Otto
Arne Ortlinghaus wrote:Hi Otto,
can you give some more information about what you are using?
Arne
Hi Arne,
yes I can.

I use PostSharp, which implements Aspect Oriented Programming.
Your methods focus on the key businesslogic, and all default other kind BS is seperated and backed into Aspects.
If all of your methods must have some kind of exception handling, you define it in one place, and point the methods you want to augment with that behaviour.

In the following example an exception is thrown in the method MyBuggyMethod, caught in the aspect MyOnExceptionAspect, written to the console and ignored (by changing the FlowBehavior to FlowBehavior.Return). You can add logic to log the error, decide whether this exception can be ignored etc, and continue.

Aspects can be used for logging, checking parameters of methods etc.
You can mix XSharp and CSharp in creating Aspects or using them without problem.

I post here the essential pieces of the code:

Code: Select all

BEGIN NAMESPACE Aspects

    /// <summary>
    /// The MyOnExceptionAspect class.
    /// </summary>
    [PSerializable];
    public class MyOnExceptionAspect INHERIT OnExceptionAspect
    
        override public METHOD OnException(args AS MethodExecutionArgs) AS VOID STRICT
        
            Console.WriteLine(i"From within the XSharp OnExceptionAspect: {args.Exception.Message}")
            args:FlowBehavior := FlowBehavior.Return
            return
    END CLASS
END NAMESPACE

Code: Select all

BEGIN NAMESPACE PostSharpTest
    public CLASS Test

            [MyOnExceptionAspect];
            METHOD MyBuggyMethod(mustThrow as LOGIC) as void strict
                IF mustThrow
                    throw Exception{"I had to throw an exception"}
                ENDIF
            RETURN

    END CLASS	
END NAMESPACE // PostSharpTest

Code: Select all

    FUNCTION Start() AS VOID
        Console.WriteLine("Hello World!")
        LOCAL t AS Test
        t := Test{}
        Console.WriteLine("before the buggy method")
        t:MyBuggyMethod(true)
        Console.WriteLine("after the buggy method")

        Console.WriteLine("Press any key to continue...")

        Console.ReadKey()
    RETURN
The output in the console is:

Code: Select all

before the buggy method
From within the XSharp OnExceptionAspect: I had to throw an exception
after the buggy method

Suggestions for conversion VO programs using VO GUI classes

Posted: Tue Jun 20, 2017 12:43 pm
by Meinhard
Hi guys!

Just my two cents...

You should definitely look into the exception handling block of Microsofts Enterprise Library!

Regards
Meinhard

Suggestions for conversion VO programs using VO GUI classes

Posted: Wed Jun 21, 2017 1:01 pm
by ArneOrtlinghaus
Dieter Crispien 2 years ago proposed also installing a global error handler via AppDomain for all unhandled exceptions similar to the code below saying that this is only for exceptions that cannot be handled ordinarily by a correct error block.
I tested it with a simulated simple runtime error within the GUI classes. The message was output on the screen, but then the app crashed nevertheless.

In Start function after creating shellobject
AppDomain.CurrentDomain:UnhandledException += UnhandledExceptionEventHandler{ oWinShell , @WinShell.UnhandledException()}

PARTIAL CLASS WinShell

METHOD UnhandledException(sender AS OBJECT, e AS UnhandledExceptionEventArgs) AS VOID
LOCAL ex AS Exception

ex := (Exception)e:ExceptionObject

msgerror ("Unhandled exception in program " + errorobject2string(ex))
RETURN

END CLASS