fbpx
Welcome, Guest
Username: Password: Remember me
  • Page:
  • 1

TOPIC: Error handling in X# vs VO

Error handling in X# vs VO 4 months 2 weeks ago #10033

  wriedmann's Avatar Topic Author wriedmann Away Posts: 1912
Hi,

in my VO code I'm using begin/end sequence very often to control where an error is occurring, and to inhibit that the program crashes because of a foreseable error.
The globally set errorblock helps a lot to keep my code small.

In X# there is no global error handler available. Therefore I'm thinking about using try/end try in much fewer places because the needed local error handling blows up my code - I have methods where the error handling has more lines of code than the processing itself, and there is the risk to not see the error because it is handled internally and not passed up to the calling code.

I know I can throw my own exception, and pass the current exception as InnerException, but I have yet to find my solution how to
handle this entire thing.

Maybe someone of you has some suggestion.....

Wolfgang
Wolfgang Riedmann
Meran, South Tyrol, Italy

www.riedmann.it - docs.xsharp.it

Please Log in or Create an account to join the conversation.

Error handling in X# vs VO 4 months 2 weeks ago #10034

  ArneOrtlinghaus's Avatar ArneOrtlinghaus Offline Posts: 134
I have regretted inserting begin/end/try blocks without recover section. So I have tried to check every such usage on correct error handling in the same code part or a code part in an upper code part. I have tried to use a global try block, but together with the GUI-Classes this cannot trap errors correctly if errors appear in events. So a main task in our preparation to X# was to insert as many additional begin/end/try blocks in program parts that call much other code. For example a pushbutton method should include such a blocks . For avoiding having to insert such a sequence in every application pushbutton method, I have modified methods in the GUI-Classes and in our framework derived from the GUI-Classes so that these basic mechanisms contain already correct handling of exceptions.

(Exception handling is nice, but also risky. I remember a project a colleague made in JAVA many years ago. He was so proud handling EVERYTHING with exceptions. But he forgot to decode the exceptions correctly in the calling parts. So we spent much time discovering the real reason for strange exceptions happening at the customer's site that put us in complete wrong directions for searching the reason.)

Please Log in or Create an account to join the conversation.

Error handling in X# vs VO 4 months 2 weeks ago #10035

  NickFriend's Avatar NickFriend Offline Posts: 211
Hi Wolfgang,

I have a different approach.... I never allow exceptions to propagate up through code. Any method that may be subject to some sort of exception (eg anything accessing data) always returns a 'Result' object that can wrap up any error.

Let's say I have a method that should return an int. In the method I'll use try/catch as normal, but the return type of the method won't be int, it'll be MyResultObject<int>. In the calling method you can then interrogate the result object to see what happened, without having to deal directly with an exception.

My result class is something like this...
public class CIOResult<T>
{
    /// <summary>
    /// Enum of different outcomes eg Success, NotFound, etc
    /// </summary>
    public CIOResultType Result { get; set; }

    /// <summary>
    /// Optional Exception object that can be returned for debugging.
    /// </summary>
    public Exception CIOException { get; set; }

    /// <summary>
    /// Construtor used when a call has failed, and an exception is to be returned.
    /// </summary>
    /// <param name="result">Result enum value.</param>
    /// <param name="excep">Exception object.</param>
    public CIOResult(CIOResultType result, Exception excep)
    {
        Result = result;
        CIOException = excep;
    }

    /// <summary>
    /// Constructor used when a value is to be returned.
    /// </summary>
    /// <param name="result">Result enum value.</param>
    /// <param name="cargo">Data object to be returned.</param>
    public CIOResult(CIOResultType result, T cargo)
    {
        Result = result;
        Cargo = cargo;
    }

    /// <summary>
    /// Constructor used when only the result is to be returned.
    /// </summary>
    /// <param name="result">Enum value of result.</param>
    public CIOResult(CIOResultType result)
    {
        Result = result;
    }
    
    /// <summary>
    /// Constructor for successful completion and to return a data object.
    /// </summary>
    /// <param name="cargo">Data object to return.</param>
    public CIOResult(T cargo) : base()
    {
        Result = CIOResultType.success;
        Cargo = cargo;
    }

    /// <summary>
    /// Constructor for successful completion but without a data object.
    /// </summary>
    public CIOResult()
    {
        Result = CIOResultType.success;
    }

    /// <summary>
    /// Data object to be included in the CIOResult object.
    /// </summary>
    public T Cargo { get; set; }
}

Obviously if there's some runtime error that's not caught by this it'll still blow up the program, but this organises everything else and makes error trapping much more manageable.

Not sure if I answered the question or not to be honest!!

Nick

Please Log in or Create an account to join the conversation.

Error handling in X# vs VO 4 months 2 weeks ago #10036

  wriedmann's Avatar Topic Author wriedmann Away Posts: 1912
Hi Nick,

Any method that may be subject to some sort of exception (eg anything accessing data) always returns a 'Result' object that can wrap up any error.

This is really a nice idea!
I have to think about, but it could be that I will steal it <g>.

Wolfgang
Wolfgang Riedmann
Meran, South Tyrol, Italy

www.riedmann.it - docs.xsharp.it

Please Log in or Create an account to join the conversation.

Error handling in X# vs VO 4 months 2 weeks ago #10037

  wriedmann's Avatar Topic Author wriedmann Away Posts: 1912
Hi Nick,
based on your code I have now written the following class:
public class ResultEx<T>
	protect _oResult as T
	protect _oException as Exception
	protect _oCargo as object

constructor( oResult as T, oException as Exception, oCargo as object )

	_oResult := oResult
	_oException := oException
	_oCargo := oCargo

	return

constructor( oResult as T, oException as Exception )

	self( oResult, oException, null )

	return

constructor( oResult as T )

	self( oResult, null, null )

	return

constructor( oResult as T, oCargo as object )

	self( oResult, null, oCargo )

	return

public property Result as T get _oResult set _oResult := value
public property Exception as Exception get _oException set _oException := value
public property Cargo as object get _oCargo set _oCargo := value

end class

And this a sample how to use it:
function TestResultEx( lSuccess as logic ) as void
	local oResult			as ResultEx<logic>

	oResult := ExecuteTestResultEx( lSuccess )

	if oResult:Result
		System.Console.WriteLine( "Execution succeeded" )
	else
		System.Console.WriteLine( String.Format( "Execution failed with error message '{0}'", oResult:Exception:Message ) )
	endif

function ExecuteTestResultEx( lSuccess as logic ) as ResultEx<logic>
	local oResult as ResultEx<logic>

	oResult := ResultEx<logic>{ true }

	try

	if lSuccess == false
		throw Exception{ "I have to fail" }
	endif

	catch oEx as Exception

	oResult:Exception	:= oEx
	oResult:Result		:= false

	end try

	return oResult

Thank you again!

Wolfgang

P.S. Did I mentioned that I like generics?
Wolfgang Riedmann
Meran, South Tyrol, Italy

www.riedmann.it - docs.xsharp.it

Please Log in or Create an account to join the conversation.

Last edit: by wriedmann.

Error handling in X# vs VO 4 months 2 weeks ago #10038

  wriedmann's Avatar Topic Author wriedmann Away Posts: 1912
Hi Arne,

I have regretted inserting begin/end/try blocks without recover section.

In VO, this works well because of the global error handler.
Such a code in VO does what it should:
function MyFunc() as logic
local lReturn as logic

lReturn := true

begin sequence

< processing something >

recover

lReturn := false

end sequence

return lReturn
because it shows an eventual error message using the global error handler.
But in .NET this does not reports the error, unfortunately.
I have lots of these constructs in my code, unfortunately.
Wolfgang
Wolfgang Riedmann
Meran, South Tyrol, Italy

www.riedmann.it - docs.xsharp.it

Please Log in or Create an account to join the conversation.

Error handling in X# vs VO 4 months 2 weeks ago #10039

  NickFriend's Avatar NickFriend Offline Posts: 211
Hi Wolfgang,

That's not quite how I'd do it. My intention was that Cargo is the generic object to hold any data or return value from the method call, and Result is something to give info on the actual process (success, failure, no data found, exception thrown, etc), so can be an enum of pre-established values to describe different outcomes.

So when you return a result object, the generic T type is the return type, not the type of process result. And the process outcome can be used to direct code for error handling etc in the calling method.

So if you had ResultEx<MyObject> as a return value from a method you'd know there was an object of type MyObject in Cargo and Result contains something like success/failure/notfound which gives you info about the outcome of the called method. This makes it easy to strongly type everything as well.

Nick

Please Log in or Create an account to join the conversation.

Error handling in X# vs VO 4 months 2 weeks ago #10040

  ArneOrtlinghaus's Avatar ArneOrtlinghaus Offline Posts: 134
Hi Wolfgang,
yes, I know that it works in VO and we have had recoverable errors with this solution many thousands of times. However I haven't found any similar solution in X#. The global error blocks as proposed by different colleagues like Dieter Crispien worked with batch executions, but did not work correctly with the GUI in my tests.

Arne

Please Log in or Create an account to join the conversation.

Error handling in X# vs VO 4 months 2 weeks ago #10041

  wriedmann's Avatar Topic Author wriedmann Away Posts: 1912
Hi Nick,

That's not quite how I'd do it. My intention was that Cargo is the generic object to hold any data or return value from the method call, and Result is something to give info on the actual process (success, failure, no data found, exception thrown, etc), so can be an enum of pre-established values to describe different outcomes.


Yes, I understand that from your code. But I prefer it the way as I have written it. Most of the times a logic return is enough for me, and very often I return some sort of object. And if I need a result enumeration, I can simply use that as the result type.
It is more the cargo member that I currently don't need, but it could be useful, so I have implemented it.
And I have added a subclass like this one (to cover the most used return type without generic):
class LogicResultEx inherit ResultEx<logic>

constructor( lResult as logic, oException as Exception, oCargo as object )

	super( lResult, oException, oCargo )

	return

constructor( oResult as logic, oException as Exception )

	self( oResult, oException, null )

	return

constructor( oResult as logic )

	self( oResult, null, null )

	return

constructor( oResult as logic, oCargo as object )

	self( oResult, null, oCargo )

	return

Wolfgang
Wolfgang Riedmann
Meran, South Tyrol, Italy

www.riedmann.it - docs.xsharp.it

Please Log in or Create an account to join the conversation.

Error handling in X# vs VO 4 months 2 weeks ago #10042

  wriedmann's Avatar Topic Author wriedmann Away Posts: 1912
Hi Arne,

However I haven't found any similar solution in X#. The global error blocks as proposed by different colleagues like Dieter Crispien worked with batch executions, but did not work correctly with the GUI in my tests.


I do think that the development team needs to add something to the translation of begin/end sequence.
Maybe they can give us the possibility to set a global error handler, maybe using the ErrorBlock() function from VO, and put a call to that in the recover clause of the begin/end sequence construct.
I had asked for that when the development had started, but probably it was too early and it either got lost or it was forgotten.

This is one of the incompatibilities between VO and X I'm be most afraid of.

Wolfgang
Wolfgang Riedmann
Meran, South Tyrol, Italy

www.riedmann.it - docs.xsharp.it

Please Log in or Create an account to join the conversation.

Last edit: by wriedmann.

Error handling in X# vs VO 4 months 2 weeks ago #10043

  NickFriend's Avatar NickFriend Offline Posts: 211
Fair enough! Anyway, glad the idea was useful.

Nick

Please Log in or Create an account to join the conversation.

Error handling in X# vs VO 4 months 2 weeks ago #10044

  ArneOrtlinghaus's Avatar ArneOrtlinghaus Offline Posts: 134
I think that it is not possible to add something like this, because it is a contradiction to exception handling. Especially when thinking in future multithreaded processes, a global error handler for all threads reacting on one thread without interfering the other should not be possible.

When working with Windows Forms it seems to be that every window event like pressing push buttons, setting/loosing focus etc. seems to be capsulated by windows forms using correct try blocks and calling the application code from there. This way, if an error is not handled correctly, an exception occurs only for that event and not for the whole application.

For the GUI classes we must insert similar behavior to get robust applications, otherwise a single error crashes the application. I have seen that sometimes unhandled errors in the GUI classes in X# cash the program without any error message at all.

Arne

Please Log in or Create an account to join the conversation.

Error handling in X# vs VO 4 months 2 weeks ago #10045

  wriedmann's Avatar Topic Author wriedmann Away Posts: 1912
Hi Arne,

I was thinking for that only for the VO dialect and when using begin/end sequence.

In newly written code it would be better to use try/end try directly, without any addition.

Wolfgang
Wolfgang Riedmann
Meran, South Tyrol, Italy

www.riedmann.it - docs.xsharp.it

Please Log in or Create an account to join the conversation.

Error handling in X# vs VO 4 months 2 weeks ago #10046

  Chris's Avatar Chris Offline Posts: 1525
Hi Wolfgang,

That was not forgotten, it's just one of the things that will have to wait for probably 2.1 or 2.2, same with being able to pass arguments by reference to untyped methods, debugging improvements, further VS integration improvements etc. Just not possible to do all we wanted for 2.0, but obviously development does not stop there.

If something like you propose will be implemented (after making enough investigation to make sure it is a viable solution), it will be implemented most probably optionally only, with a compiler option. But still, I suspect it will not help much, because IMO the biggest difference between VO and .Net is that VO provides error handling even without BEGIN SEQUENCE blocks at all, allowing you to continue execution after the error has happened, from the next line! End even if you have SEQUENCE blocks like this:

BEGIN SEQUENCE

CallSomeCodeThatCAusesAnException()

// rest of the code

RECOVER

END SEQUENCE

then if an exception happens inside the call, then program control is not automatically moved to the RECOVER section, but the user (or the programmer) still has the option to keep executing the "rest of the code" section, or even the code inside the function that caused the exception and has no error handling itself at all.

This is indeed a powerful feature of VO, but unfortunately there's nothing in .Net that could be used to emulate this. Unless maybe if the compiler automatically (optionally again) inserted a TRY block for EVERY single line of code, implementing this kind of error handling. Of course this would lead to HUGE exes/dlls, but maybe this is not a very big problem. Probably it would also hurt performance a lot, but then again maybe it will not be too much...it's something to experiment with.
XSharp Development Team
chris(at)xsharp.eu

Please Log in or Create an account to join the conversation.

Last edit: by Chris.

Error handling in X# vs VO 4 months 2 weeks ago #10049

  wriedmann's Avatar Topic Author wriedmann Away Posts: 1912
Hi Chris,

I do know only my code - and I have written much VO code the last 20 years <g>. When moving code to X#, several times I had code that failed in X# and I didn't knew where...
An option to put every single line of code in a try/catch statement would be very welcome for debugging purposes, but for the "normal" migrated code a begin/end sequence replacement as I suggested would help - of course optional with a compiler switch, but this should be set by default by the Xporter.
It is very important that migrations work without the minor issues possible, otherwise people will loose the confidence.
And, as I wrote, I see this different behaviour between VO and X# as one of the most challeging ones.
Wolfgang
Wolfgang Riedmann
Meran, South Tyrol, Italy

www.riedmann.it - docs.xsharp.it

Please Log in or Create an account to join the conversation.

Error handling in X# vs VO 4 months 2 weeks ago #10051

  Fab64's Avatar Fab64 Offline Posts: 56
Hi Chris, Wolfgang,

All my VO code contains

BEGIN SEQUENCE
.....
RECOVER
......
END SEQUENCE

or

BEGIN SEQUENCE

...........
IF ........
BREAK
END IF

..............

RECOVER
..........
END SEQUENCE

and a way to implement it also in .NET is welcome.

Fabrizio

Please Log in or Create an account to join the conversation.

Last edit: by Fab64.

Error handling in X# vs VO 4 months 2 weeks ago #10052

  wriedmann's Avatar Topic Author wriedmann Away Posts: 1912
Ciao Fabrizio,
AFAIK the code
begin sequence
<code>
recover
<code>
end sequence
will be replaced by
try
<code>
catch
<code>
end try
This is not the real problem.
The real problem is that in VO you can set your own errorblock, and it is called every time an error is occurring.
In .NET this is not the case - you have to catch the errors by yourself in the catch block.
Therefore my suggestion was to replace the VO begin/end sequence by something like this (pseudo-code):
try
<code>
catch oEx as Exception
ExecuteErrorBlock( oEx )
<code>
end try
or even a sequence without recover block with the same construct.
Wolfgang
Wolfgang Riedmann
Meran, South Tyrol, Italy

www.riedmann.it - docs.xsharp.it

Please Log in or Create an account to join the conversation.

Error handling in X# vs VO 4 months 2 weeks ago #10056

  Fab64's Avatar Fab64 Offline Posts: 56
HI Wolfgang,
ok, now it's clearer to me.
the real problem is managing the ERRORBLOCK in X # (.NET).

Fabrizio

Please Log in or Create an account to join the conversation.

Error handling in X# vs VO 4 months 2 weeks ago #10057

  Chris's Avatar Chris Offline Posts: 1525
Hi Fabrizio,

Fab64 wrote: HI Wolfgang,
ok, now it's clearer to me.
the real problem is managing the ERRORBLOCK in X # (.NET).


Yeah, apart from ErrorBLock() handling, everything else in this area works the same way in X# as in VO. The pseudo code you posted compiles also in X# with zero changes. What Wolfgang mentioned about BEGIN SEQUENCE code being replaced by TRY...END TRY is what happens under the surface by the compiler, but your own source code does not need to change at all.
XSharp Development Team
chris(at)xsharp.eu

Please Log in or Create an account to join the conversation.

Error handling in X# vs VO 4 months 1 week ago #10058

  Fab64's Avatar Fab64 Offline Posts: 56
Hi Chris,
ok.
Fabrizio

Please Log in or Create an account to join the conversation.

  • Page:
  • 1