In Script#, is it possible to define your own exception classes? I cannot derive from Exception since it's sealed:
public class MyException : Exception {} // Cannot inherit from sealed class Exception
And if I try to throw an exception that doesn't inherit from Exception I get another error:
Error 22 The type caught or thrown must be derived from System.Exception
Seems like there is no support for this?
Its not possible.
You can't catch specific types in generated script. If you've got additional data you'd like to put on the exception, then the Exception type has members to hold on to additional data.
Related
I expect the following program to output an error message (via WRITE), but actually it triggers the run time error UNCAUGHT_EXCEPTION (short dump) at the time of RAISE EXCEPTION TYPE:
REPORT.
CLASS lcx_exception DEFINITION INHERITING FROM cx_static_check.
ENDCLASS.
CLASS lcl_app DEFINITION.
PUBLIC SECTION.
METHODS main.
ENDCLASS.
CLASS lcl_app IMPLEMENTATION.
METHOD main.
RAISE EXCEPTION TYPE lcx_exception. "<===== HERE, run time error UNCAUGHT_EXCEPTION
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
TRY.
NEW lcl_app( )->main( ).
CATCH lcx_exception INTO DATA(ex).
WRITE: / 'Exception:', ex->get_text( ).
ENDTRY.
The solution is to add RAISING lcx_exception (eventually more exception classes may be listed here) to the method declaration, so that the exception is automatically propagated to the calling procedure (and caught, hopefully) when it occurs:
REPORT.
CLASS lcx_exception DEFINITION INHERITING FROM cx_static_check.
ENDCLASS.
CLASS lcl_app DEFINITION.
PUBLIC SECTION.
METHODS main RAISING lcx_exception. "<==================== HERE
ENDCLASS.
CLASS lcl_app IMPLEMENTATION.
METHOD main.
RAISE EXCEPTION TYPE lcx_exception.
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
TRY.
NEW lcl_app( )->main( ).
CATCH lcx_exception INTO DATA(ex).
WRITE: / 'Exception:', ex->get_text( ).
ENDTRY.
Note that you can't list the exception classes which inherit from the root class CX_NO_CHECK, directly or indirectly, because these exceptions are propagated implicitly. Only exception classes inheriting from CX_STATIC_CHECK and CX_DYNAMIC_CHECK, directly or indirectly, need to be indicated after RAISING for being propagated. Those 2 root classes may also be indicated to propagate all exception classes.
Can I make an exception which contains another exception in its constructor? I want to throw an exception about what exception happened lower down.
Yes, like this:
exception Foo
exception Bar of exn
Might get problems with printing.
Fatal error: exception Bar(_)
I have to create a a new exception (MultipleErrorException) which extends MySuperException class. I have another exception (SingleErrorException) which also extends MySuperException.
Looking at the pseudo code below:
for (a number of times)
try
call myMethod(myObject) that throws SingleErrorException
catch(SingleErrorException e)
add the caught SingleErrorException object to a
MultipleErrorException object where a mapping should
be created mapping myObject to SingleErrorException
I need to throw a single "MultipleErrorException" which holds a map (myObject-->SingleErrorExceptions).
Is there any other way to this rather than creating an initial MultipleErrorException object before the for loop, an then adding the mappings as the exceptions are caught?
Thanks
java.lang.Exception as such does not have any method to support your requirement.
If you need it, then one way is to add the Map variable in MultipleErrorException and setting the value of it, but again, editing the exception object is not good.
Instead, you can add the error messages to Map and return the map instead of throwing the exception from the method.
I want to create a custom exception message in case exception is thrown by my mule service. In order to do that, i want to separately capture mule generated ErrorCode. Is there any property using which i can get that value? I tried using #[org.mule.config.ExceptionHelper.getErrorCode(Exception.class)]" but this returned -1 as a value instead of the actual exception code.
What method can i use to fetch ErrorCode?
You are supposed to pass the class of the current exception, ie:
#[org.mule.config.ExceptionHelper.getErrorCode(exception.class)]"
exception is the current exception while Exception is the java.lang.Exception class for which there is no error code associated.
I have c#.net code which calls a method from another external/referenced .net assembly. This method I am calling throws an exception if a certain property from the object I am passing it is null. Here it is in a nutshell:
public void Add(string key, object obj)
{
..
//if the Foo property from obj is null then
throw new Exception("Foo property is null or empty")
..
}
In my client code which calls the DLL's Add method, I would like to be able to detect that this particular exception was raised, maybe distinguished by its "Foo property is null or empty" message. Currently, I get a NullReferenceException when it hits this method, so I catch this exception.
Question1:
Can I get the error message associated with the exception being thrown by the code I am calling (in the referenced assembly)??
Question2:
Is this considered bad practice or maybe just atypical?
Obviously, I can disassemble the third-party DLL to discover that my obj I'm passing in must have this "Foo" property set. So, my question here is somewhat for the sake of exercise (and because I'm a n00b).
Catching System.Exception and showing the exceptions Message property is all I needed. At first, I kept getting a NullReferenceException with "Object reference not set to instant of an object" message at the Add method, but I was expecting to get an Exception with the message "Foo property is null or empty" error. Some condition changed in my code and I now get what I expect.