Re: [Ikvm-developers] Catching exceptions in c# from java code
Brought to you by:
jfrijters
From: Stuart B. <stu...@gm...> - 2010-05-17 20:32:20
|
The catch is what makes sure that you never throw the exception. Java requires you to EITHER catch every exception OR specify that you throw it in the declaration of your method. If you say "public void methodCall throws Exception" then it will be thrown and you can catch it from outside. This has nothing to do with IKVM or C# - the same thing would happen if you were trying to catch it in another Java method. Stuart. On Mon, May 17, 2010 at 16:28, <Sha...@tr...> wrote: > > I tried putting the "throw e;" in java and got a compiler error message: > > "Unhandled exception type Exception". > > So, I put a "try catch block " around that statement and it will not throw > the original error. > > I have to be doing something wrong. I know there must be a way to catch > Java errors in c#. > > Shawn Roney > > > > *Stuart Ballard <stu...@gm...>* > > 05/17/2010 02:07 PM > To > Sha...@tr... > cc > Jeroen Frijters <je...@su...>, " > ikv...@li..." < > ikv...@li...> > Subject > Re: [Ikvm-developers] Catching exceptions in c# from java code > > > > > Your methodCall() never throws an exception, because it catches it instead. > > If you put a "throw e;" after your printStackTrace() it should work. > > On Mon, May 17, 2010 at 15:05, <*Sha...@tr...*<Sha...@tr...>> > wrote: > > 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...* <je...@su...>*>* > > 04/30/2010 12:38 AM > > To > "*Sha...@tr...* <Sha...@tr...>" <* > Sha...@tr...* <Sha...@tr...>>, "* > ikv...@li...*<ikv...@li...>" > <*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...* <Sha...@tr...> [mailto:* > Sha...@tr...* <Sha...@tr...>] > > Sent: Thursday, April 29, 2010 11:10 PM > > To: *ikv...@li...*<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* <http://www.triminsystems.com/> > > > ------------------------------------------------------------------------------ > _______________________________________________ > Ikvm-developers mailing list* > **Ikv...@li...*<Ikv...@li...> > * > **https://lists.sourceforge.net/lists/listinfo/ikvm-developers*<https://lists.sourceforge.net/lists/listinfo/ikvm-developers> > > > > ------------------------------------------------------------------------------ > > > _______________________________________________ > Ikvm-developers mailing list* > **Ikv...@li...*<Ikv...@li...> > * > **https://lists.sourceforge.net/lists/listinfo/ikvm-developers*<https://lists.sourceforge.net/lists/listinfo/ikvm-developers> > > > > > -- * > **http://sab39.netreach.com/* <http://sab39.netreach.com/> > -- http://sab39.netreach.com/ |