I am struggling with some Monodroid code that should be simple, but is causing an unexpected exception. The code throws an exception when I try to invoke SelectNodes() on an XmlDocument object.
This is the code:
XmlDocument handXmlDocument = new XmlDocument();
XmlDeclaration dec = handXmlDocument.CreateXmlDeclaration("1.0", null, null);
handXmlDocument.AppendChild(dec);
XmlElement root = handXmlDocument.CreateElement("friendslist");
handXmlDocument.AppendChild(root);
XmlElement friendElement = handXmlDocument.CreateElement("friend");
friendElement.SetAttribute("locationx", "35");
friendElement.SetAttribute("locationy", "46");
friendElement.SetAttribute("phone", "4085556803");
root.AppendChild(friendElement);
XmlNodeList friendNodeList = handXmlDocument.SelectNodes("/friendslist/friend");
and here is the exception:
E/mono ( 365): [0x45eec0:] EXCEPTION handling: System.TypeInitializationException: An exception was thrown by the type initializer for System.Console
E/FriendsView( 365): Exception parsing XML document: An exception was thrown by the type initializer for System.Console
E/FriendsView( 365): Inner exception parsing XML document: System.ExecutionEngineException: SIGILL
E/FriendsView( 365): at System.Console..cctor () [0x00019] in /Users/plasma/Work/mono-android-trunk/mcs/class/corlib/System/Console.cs:127
I have no idea why System.Console is involved here! Thanks for any help, Martin
Just adding this in the hopes it might help anyone else who hits the same problem. It turns out that this was a bug in Monodroid 1.0.8980 which was fixed by the time of Monodroid 1.0.9186. Upgrading to the new release fixed my problem.
Related
I'm running into an exception in C# code intended to set the contents of a WinUI3 Image object. I don't know how to interpret the exception message.
Here's the code:
using var responseStream = await response.Content.ReadAsInputStreamAsync();
var memStream = new InMemoryRandomAccessStream();
await RandomAccessStream.CopyAsync(responseStream, memStream);
memStream.Seek(0);
var retVal = new Image();
var source = new BitmapImage();
// the exception is thrown on the next line
await source.SetSourceAsync( outStream );
retVal.Source = source;
The exception message is
Unspecified error (Error HRESULT E_FAIL has been returned from a call to a COM component.)
What's weird is that calls to this code sometimes succeed and sometimes don't, even when the exact same parameters (which define the Uri that the response stream is derived from) are specified and the exact same response is received.
That suggests there's something in the context of the call that's the problem. The cases that succeed are when the code is called from a viewmodel in a WinUI3 test app. The cases that fail are when the code is called from a custom WinUI3 control I've written.
I thought there might be a "called from the wrong thread" problem but in both the success and failure cases the code is being called from the main UI thread.
Another odd thing is that the code throwing the exception is contained within a try/catch block (not shown for simplicity)...but the catch block doesn't catch the exception. Instead, it gets caught by the unhandled exception handler generated by WinUI3.
Advice on how to proceed with researching what's going wrong -- or potential solutions! -- would be appreciated, thanx.
In a native-C++-Dll (MFC): how to throw an Exception (with some additional text) that can be catched and handled from a .NET-Client?
This is the native-MFC-Dll which wants to throw an Exception:
void RGaugeTVDDataReader::LoadDataFromFile_HandleException(const CString& szPath) const
{
CString sMessage;
sMessage.Format(_T("TVD-File %s"), static_cast<LPCTSTR>(szPath));
/*1*/ AfxThrowFileException(CFileException::genericException, -1, static_cast<LPCTSTR>(sMessage));
/*2*/ throw std::runtime_error(CT2A(sMessage));
}
The Caller is a .Net-Dll which uses the native dll via a .net-generated-COM-Wrapper.
Both variants (/1/ and /2/) fall into the catch-Block of the .Net-Component but what I get here is just a ´System.Runtime.InteropServices.SEHException´ and the exceptionmessage says "External component has thrown an exception.". Included ErrorCode and HResult is 0x80004005. There is no inner exception, nothing to find about my given text and the stacktrace only contains the managed part.
So the question is: how to throw an excpetion from the native-c++-dll so that a .net-client using it via COM can at least see the message-string?
Alternative question: how to catch and handle the thrown exception in .net correctly?
regards
I can't resolve ILogger instance in my console application...
I'm using Unity IoC and Serilog.
The registration is done like this:
container = new UnityContainer();
container.RegisterFactory<ILogger>(factory =>
{
ILogger log = new LoggerConfiguration()
.WriteTo.Console()
.CreateLogger();
return log;
}, new ContainerControlledLifetimeManager());
And then when I try this:
var logger = container.Resolve<ILogger>();
I get an exception:
Unity.ResolutionFailedException HResult=0x80131500 Message=Failed
to select a constructor for Serilog.Core.Logger
_____________________________________________________ Exception occurred while:
•resolving type: 'ILogger' mapped to 'Logger'
Source=Unity.Container StackTrace: at
Unity.UnityContainer.ExecuteValidatingPlan(BuilderContext& context)
at Unity.UnityContainer.Unity.IUnityContainer.Resolve(Type type,
String name, ResolverOverride[] overrides) at
Unity.UnityContainerExtensions.Resolve[T](IUnityContainer container,
ResolverOverride[] overrides)
Inner Exception 1: InvalidOperationException: Failed to select a
constructor for Serilog.Core.Logger
Inner Exception 2: InvalidRegistrationException: Exception of type
'Unity.Exceptions.InvalidRegistrationException' was thrown.
I even tried registering it like this:
container = new UnityContainer();
ILogger log = new LoggerConfiguration()
.WriteTo.Console()
.CreateLogger();
container.RegisterInstance<ILogger>(log);
but with the same resulting exception thrown.
Please help
Found the problem...
The problem was this line of code down the road, overriding my configuration:
container.RegisterTypes(AllClasses.FromAssembliesInBasePath(), WithMappings.FromMatchingInterface, WithName.Default);
Using Groovy / Grails and log4j is there any way to ensure every exception thrown in the code is logged at error level.
Rather than having to find every catch block and explictly log it?
If not groovy / grails - a java suggestion will suffice.
Thanks
I don't believe there's any way to do this for handled exceptions, but you can do it for unhandled exceptions by adding the following to UrlMappings.groovy
"500"(controller: 'error')
Then create an ErrorController.groovy under grails-app/controllers
class ErrorController {
def index() {
Throwable exception = request?.exception?.cause
log.error 'something bad happened', exception
}
}
We have a custom error controller that gets called after all of our errors. However, most of our errors that get thrown end up coming into the controller as null pointers, even though the original error was not a null pointer. Any ideas? Code below. Bootstrap and UrlMappings available if needed. Thanks
Error Handler method
def HandleErrors =
{
def exception = request.exception.cause.class
if (exception)
{
Exception ex = request.exception //This exception is always a NPE
...
Block of code throwing the exception. I originally did not have a try catch in here, but wanted to add it so that I was sure the exception being thrown was Not a NPE. Its a file not found exception.
try{
def writer = new FileWriter( new File(fileSaveLocation));
}
catch ( ex)
{
throw(ex)
}
Edit: Adding the exception that is pushed to the exception handler
Exception:org.codehaus.groovy.grails.web.errors.GrailsWrappedRuntimeException
Cause:org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.NullPointerException
It's not because you're referencing something that is null inside the error handler, and so are inadvertently throwing another exception, which is again caught?
can you try changing:
def exception = request.exception.cause.class
to
def exception = request?.exception?.cause?.class