Catch CLR errors

Hi,

I am trying to catch CLR error. But nothing comes to my catch block when I debug code on server.

But When the code is run on client and debugged, I can catch the error.

As I am trying to catch the CLR errors and report them in my batch job.

Thanks,

Varry

What’s your version of AX? If AX2012, is the code on server running as X++ or CIL? Can you confirm that you can catch X++ errors without troubles even on server?

And can you show us your code so we can try to reproduce the problem?

Hi Martin,

I am using AX 2012. I can catch error defined But not CLR for my following code.

Basically i am passing sid from userinfo table as parameter and trying to disable AD account. My server account has not got enough permissions to disable AD.

So I want to catch that error which is “System.UnauthorizedAccessException: Access is denied”.

The error is seen in the batch job log.

"

public void disableADAcc(Sid ADsid)
{
System.DirectoryServices.AccountManagement.PrincipalContext p;
System.DirectoryServices.AccountManagement.UserPrincipal u;
System.Exception ex;
Set permissionSet;
Exception exception;
str message;
;
try
{
// permissionSet = new Set(Types::Class);
// permissionSet.add(new InteropPermission(InteropKind::ClrInterop));
//CodeAccessPermission::assertMultiple(permissionSet);
p = new System.DirectoryServices.AccountManagement.PrincipalContext(System.DirectoryServices.AccountManagement.ContextType::Domain);
u = System.DirectoryServices.AccountManagement.UserPrincipal::FindByIdentity(p, System.DirectoryServices.AccountManagement.IdentityType::Sid, ADsid);

//disable active directory account
u.set_Enabled(false);
u.Save();
//CodeAccessPermission::revertAssert();

}
catch(exception::CLRError)
{
this.getClrErrorMessage();//copied this method from aifutil class
}

}

Thanks,

Varry

How do you verify that the exception get caught? AifUtil::getClrErrorMessage simply returns a string and you don’t do anything with it, therefore your code simply catches and swallows the exception without any observable output.

Hi,

Yeah. But when I debug, the method in the catch block is not called and no value is returned.

I tried doing a Full CIL but still have the same issue.

Replaced the code, doesn’t work.

System.DirectoryServices.AccountManagement.PrincipalContext p;
System.DirectoryServices.AccountManagement.UserPrincipal u;
System.Exception ex;
Set permissionSet;
Exception exception;
str message;
System.Convert c;
;
try
{
permissionSet = new Set(Types::Class);
permissionSet.add(new InteropPermission(InteropKind::ClrInterop));
CodeAccessPermission::assertMultiple(permissionSet);
p = new System.DirectoryServices.AccountManagement.PrincipalContext(System.DirectoryServices.AccountManagement.ContextType::Domain);
u = System.DirectoryServices.AccountManagement.UserPrincipal::FindByIdentity(p, System.DirectoryServices.AccountManagement.IdentityType::Sid, ADsid);

//disable active directory account
u.set_Enabled(false);

System.Convert::ToInt32(“HelloWorld”);

//u.Save();
CodeAccessPermission::revertAssert();

}
catch(Exception::CLRError)
{
error(“clr error”);
}
catch(Exception::Error)
{
error(“error”);
}
catch(Exception::Internal)
{
error(“internal error”);
}

Thanks,

Varry

The only idea I have is that the code is in a transaction. When an exception is thrown in a transaction, nothing else inside the transaction is executed, including catch statements.

I would also suggest walking through the code in debugger to see exactly what’s happening there (whether any exception is thrown or not, what code gets executed after that and so on).

Hi,

Please check the TTS state (it should be 0).

Go through the following link.

http://blogs.msdn.com/b/emeadaxsupport/archive/2010/07/22/working-with-clr-exceptions-in-dynamics-ax-x-code.aspx

Hope this helps you.

Thanks Martin,

Yes you are right. It is in a transaction.

It works now. Great help.

Cheers,

Varry.

Thanks It is solved now.