Re: [Ikvm-developers] Catching exceptions in c# from java code
Brought to you by:
jfrijters
From: <Sha...@tr...> - 2010-05-17 19:05:11
|
I guess I am still a bit puzzled. In Java public void methodCall() { String x = null; try { x.substring(3); } catch (Exception e) { e.printStackTrace(); } } in c# try { methodCall(); } catch (System.Exception x) ... Should I be able to catch the NullPointerException that is thrown in Java? } This doesn't seem to work for me. Shawn Roney Jeroen Frijters <je...@su...> 04/30/2010 12:38 AM To "Sha...@tr..." <Sha...@tr...>, "ikv...@li..." <ikv...@li...> cc Subject Re: [Ikvm-developers] Catching exceptions in c# from java code Exception interop is a little bit messy. When you catch System.Exception you'll get all exceptions (including the Java exceptions), but when you're looking for a specific exception some more work is needed. Especially in the case of a runtime exception (i.e. an exception generated by the JVM or in this case the CLR). If a Java method throws a java.lang.NullPointerException, this can surface on the C# side as either a System.NullPointerException or a java.lang.NullPointerException, depending on whether the Java code explicitly threw the exception or the CLR threw the exception for dereferencing a null reference. In the case below, if you just want to catch the java.lang.Exception thrown by " throw new Exception("TriMinEx - " + output[2]);", then you can simply use "catch (java.lang.Exception)" in C#. Finally, if you want the exception as Java code would see it, you can do this: try { ... } catch (System.Exception x) { x = ikvm.runtime.Util.mapException(x); if (x is java.lang.NullPointerException) { ... } else if (x is ...) { } } Regards, Jeroen > -----Original Message----- > From: Sha...@tr... [mailto:Sha...@tr...] > Sent: Thursday, April 29, 2010 11:10 PM > To: ikv...@li... > Subject: [Ikvm-developers] Catching exceptions in c# from java code > > > Hello, > I have some Java code that I want to reuse in a c# project. How do I catch the > exceptions thrown in Java and print them out in c#? I have tried, but can't > seem to get it to work. > > Here is the java method: > > public String[] sendModel2(String user, String password, String xml) { > String[] output = new String[4]; > > try { > > > TriMinDocument triDoc = XMLDomUtil.returnModel2Type(xml); > String docId = triDoc.getDocumentId(xml); > if (docId == null || docId.trim().length() == 0) { > output = new String[] { "Failure", xml, "The document > indentifier must be available in the XML document.", "" }; > throw new Exception("TriMinEx - " + output[2]); > } > > // triDoc is null, so should throw a null pointer exception > } catch (Exception e) { > StringUtil.StringToFile("Hello","c:\\temp\\test.txt"); > String errMsg = e.getMessage(); > if (errMsg == null) > errMsg = ""; > if (!errMsg.startsWith("TriMinEx -")) { > > output = new String[] { "Failure", xml, "Invalid XML sent > to service", "" }; > } > throw new Exception("TriMinEx - " + output[2]); > } > > return output; > } > > > I cannot get an exception to print out in c#. > > Thanks in advance. > > Shawn > > Shawn Roney > Director, Advanced Applications > TriMin Government Solutions > Phone: 651-604-3653 > Cell: 651-587-3804 > http://www.triminsystems.com ------------------------------------------------------------------------------ _______________________________________________ Ikvm-developers mailing list Ikv...@li... https://lists.sourceforge.net/lists/listinfo/ikvm-developers |