Closed jbellis closed 12 years ago
I have added the use of LastErrorException with this test case which passes
@Test
public void getSetAffinity() {
long init = JNAAffinity.INSTANCE.getAffinity();
JNAAffinity.INSTANCE.setAffinity(0x01);
assertEquals(0x01, JNAAffinity.INSTANCE.getAffinity()) ;
try {
JNAAffinity.INSTANCE.setAffinity(0x00);
fail("This should fail");
} catch (LastErrorException expected) {
assertEquals("errno was "+expected.getErrorCode(), expected.getMessage());
}
JNAAffinity.INSTANCE.setAffinity(init);
assertEquals(init, JNAAffinity.INSTANCE.getAffinity()) ;
}
Thank you for your advice.
The modern way to handle errno from JNA is to declare your native methods as "throws LastErrorException." Then you can access getErrorCode on a caught LEE. For example, https://github.com/ryanking/apache-cassandra/blob/trunk/src/java/org/apache/cassandra/utils/CLibrary.java (which has some instanceof dancing that you may not care about if you only support versions of JNA new enough to define LEE).
Calling errno directly is problematic (it's often a macro), and even Native.getLastError (JNA's old way to expose it semi-portably) doesn't always work (apparently it can itself trigger syscalls that reset errno...). LastErrorException does work reliably and is the recommended way to handle errno now.