Handling AccessViolation exception in try catch c# - wpf

How to catch the AccessViolation exception in try-catch block:
here is the code below:
public static BP GetBloodPressure(string vendorid, string productid)
{
BP Result = new BP();
try
{
GETBPData BPreadings = new GETBPData();
UInt16 VendorId = Convert.ToUInt16(vendorid, 16);
UInt16 ProductId = Convert.ToUInt16(productid, 16);
if (HealthMonitorData.HidDataTap_GetBloodPressure(VendorId, ProductId, ref BPreadings)) // error here
{
if (BPreadings.ucSystolic == 0 && BPreadings.ucDiastolic == 0 && BPreadings.DeviceId1 == 0 && BPreadings.DeviceId2 == 0 && BPreadings.ucPulse == 0)
{
Result = null;
}
else
{
Result.UcSystolic = BPreadings.ucSystolic;
Result.UcDiastolic = BPreadings.ucDiastolic;
Result.UcPulse = BPreadings.ucPulse;
Result.DeviceId1 = BPreadings.DeviceId1;
Result.DeviceId2 = BPreadings.DeviceId2;
}
}
}
catch (Exception ex)
{
}
return Result;
}
I am importing one dll to read the blood pressure values from the device. I have try to catch the exception but the control does not go beyond the "if" statement where the access violation exception is coming.
Kindly Suggest?
Thanks

Handling of AccessViolationExceptions and other corrupted state exceptions has been changed in .NET 4. Generally you should not catch these exceptions, so the runtime has been changed to reflect this. If you really need to catch these, you must annotate the code with the HandledProcessCorruptedStateExceptions attribute.
Please keep in mind, that the behavior was changed with good reason. Most applications will not be able to handle these exceptions in any meaningful way and thus should not catch them.

its HandleProcessCorruptedStateExceptions not HandleDProcessCorruptedStateExceptions

Related

Why does File.listFiles() throws NullPointerException on some devices?

Please pay attention to all the tests in the following code:
File folder = new File(sFolderPath);
if (folder == null) {
//do nothing
} else {
if (folder.exists()) {
File[] flist = folder.listFiles();
}
}
folder.listFiles() throws the following exception:
java.lang.NullPointerException: Attempt to get length of null array
It rarely happens. I am wondering how this can be possible after passing the tests of null and existence.

Is it possible to selectively ignore service exception in Hystrix?

I have client API jar which internally makes external calls and throws single generic Service exception in case of issues. I have written hystrix wrapper over the API calls. There are cases like "user not found" returning exception. Though the call was successful and service responded with valid response, the hystrix is treating it as a failure. I know that we can ignore the exception in Hystrix; but it will whitelist the only exception thrown by service calls. Is there a way to selectively ignore exception thrown by the service calls based on message in exception or http status code or something?
If the external service throw different exceptions in different cases, then you can probably ignore those exceptions like this
#HystrixCommand(ignoreExceptions = {SomeException.class})
But if you have to ignore exceptions bases on error message then the best way to tackle this is put a try catch around your external call. And in the catch block check if it is one of those exceptions which needs to be ignored. If so don't do anything. If not rethrow this exception. Something like this will do. More info about HystrixBadRequestException
#HystrixCommand(fallbackMethod = "fallBackMethod", groupKey = "CircuitBreaker", commandKey = "somekey", threadPoolKey = "somekey",
commandProperties = {
#HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "10000"),
#HystrixProperty(name = "execution.timeout.enabled", value = "false"),
#HystrixProperty(name = "circuitBreaker.requestVolumeThreshold", value = "20"),
#HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds", value = "1200000"),
},
threadPoolProperties = {
#HystrixProperty(name = "coreSize", value = "30"),
#HystrixProperty(name = "metrics.rollingStats.timeInMilliseconds", value = "180000")
})
public void someMethod(....){
try {
// Call external service
} catch(Exception e) {
if(exception to be ignored)
throw new HystrixBadRequestException("Some message", e);
else
throw e
}
}

How to look for a webelement with out an exception being thrown?

I am trying to verify to see some webelement is present or not on my result page, using id attribute. It is throwing an exception when such webelement is not present and it is NOT acceptable. In the Selenium API JavaDoc, it was recommended that 'findElement' should not be used to look for non-present elements, use 'findElements(By)' instead and assert zero length response. But this is also throwing an exception, I don't know why !! Any alternate suggestions?
try{
// THE PRESENT CODE TO BE MODIFIED. NOT TO THROW exception.***********
// if(driver1.findElement(By.id(orderResultsCheckbox_0)) != null)
// return true;
// *****************************
//Instead I wrote as below...
if(
(driver1.findElements(By.id(orderResultsCheckbox_0) != null)) && (driver1.findElements(By.id(orderResultsCheckbox_0)).length() > 0)
)
return true;
else
return false;
} catch (Exception e) {
println "Exception Thrown ==========>";
return false;
}
The findElements method returns a list of webelements and you can check whether the list contains any webelements using the size() method. Hence, replace your existing code with following and let me know whether it resolves your issue:
if(driver1.findElements(By.id(orderResultsCheckbox_0)).size() != 0)
return true;
else
return false;

Lost on how to implement writeCharecteristic in my android code

I am using android studio's example bluetoothlegatt, which has a read function and and works completely find (it interacts with the TI keyfob I have which sends over values when pressed).
However, I have no idea how to put in writeCharecteristic because my previous attempts have failed.
Can someone help me with this? I would like to send a "1" over so that the keyfob's led lights up.
Edit: I have tried something like this
public void writeCharacteristic(byte[] value) { BluetoothGattService LumService = mBluetoothGatt.getService(UUID_LUM_APP);
if (LumService == null) {
System.out.println("LumService null"); return; }
BluetoothGattCharacteristic LumChar = LumService.getCharacteristic(UUID_LUM_CHAR);
if (LumChar == null) {
System.out.println("LumChar null"); return; }
LumChar.setValue(value); boolean status = mBluetoothGatt.writeCharacteristic(LumChar);
System.out.println("Write Status: " + status);
This is not my code, though, just an example. I want to integrate this into the bluetoothlegatt given already, but I have had no success with it.

Else-part of code being executed, even if string.equals(otherstring) is true

The problem with this code seems to be that the 'else' part of the if-statement is executed, even if the variables match (so 'if' is true). Any advice, please?
Thanks!
public void CheckInstalledDBVersion() throws NullPointerException, IOException {
try {
//TRY TO OPEN DATABASE AND READ VERSION
//WRITE VERSION TO InstalledDBversion
} catch(RuntimeException e) {
//IF TABLE COULD NOT BE QUERIED
//SET InstalledDBversion to Bogus value
InstalledDBversion = "00";
Log.d("RTE", ".. but we've catched it!");
} finally {
if (InstalledDBversion.equals(PackedDBversion)){
// Installed DBVersion == Packed DBVersion .. nothing happens
}
else
showDialog(DBCHECKFAILDIALOG);
initialiseDatabase = false;
copyDB();
}
}
So, when I execute, copyDB(); gets called even if InstalledDBversion.equals(PackedDBversion) == true
else
showDialog(DBCHECKFAILDIALOG);
initialiseDatabase = false;
copyDB();
Fixed indentation for you. copyDB is outside of the if/then/else block. Use an IDE with code formatting.
What lines are supposed to be included in the else block? the showDialog(DBCHECKFAILDIALOG) is only included. Are you missing a set of {} for the else block?

Resources