I have a section of code that correctly load images from http URIs when the URIs are valid but I cannot figure how to catch the exception OpenAsync throws when the URI is invalid (results in 404).
The problem is that when the lambda which contains the call to OpenAsync exits, the exception is thrown; the exception is not thrown while in the try/catch block.
The question is:
What is the correct way to catch the exception thrown by StorageFile::OpenAsync?
auto bm = ref new BitmapImage();
try {
Uri^ uri = ref new Uri("http://invaliduri.tumblr.com/avatar/128");
auto task = Concurrency::create_task(CreateStreamedFileFromUriAsync("temp-web-file.png", uri, nullptr));
task.then([] (StorageFile^ file) {
try {
return file->OpenAsync(FileAccessMode::Read);
} catch (...) {
// this does not catch the exception because the exception
// occurs after this lambda is exitted
}
}).then([bm](IRandomAccessStream^ inputStream) {
try {
return bm->SetSourceAsync(inputStream);
} catch (...) {
// this does not catch the exception because the exception
// occurs before this lambda is entered
}
});
} catch (...) {
// and obviously this would not catch the exception
}
I had this question 3 years later. I referenced this article. My scenario was, then, solved as follows,
#include<ppltasks.h>
...
auto file = ref new Windows::Foundation::Uri::Uri("ms-appx:///SomeFile.txt");
concurrency::create_task(Windows::Storage::StorageFile::GetFileFromApplicationUriAsync(data))
.then([](Windows::Storage::StorageFile^ f) {
return Windows::Storage::FileIO::ReadTextAsync(f);
})
.then([this](String^ s) {
this->someFileContent = s;
})
.then([](concurrency::task<void> t) {
try {
t.get();
} catch(Platform::COMException^ e) {
OutputDebugString(e->Message->Data());
}
});
This async task chain may fail in GetFileFromApplicationUriAsync or in ReadTextAsync throwing an exception. The key is that when thrown the only matching then(...) prototype is the final one. On entering the try block, task::get re-throws the exception caught by the concurrency classes on your behalf.
task.then([] (StorageFile^ file) { // this is where the exception is actually thrown
The exception is most likely thrown on this line because to be able to pass in the StorageFile to the lambda the .then is doing a non-explicit get() on the task. You're using what is called a "value continuation" while you probably want a "task continuation" and check for exceptions there.
auto task = Concurrency::create_task(CreateStreamedFileFromUriAsync("temp-web-file.png", uri, nullptr));
task.then([] (concurrency::task<StorageFile^> fileTask) {
StorageFile^ file;
try
{
file = fileTask.get(); // this is what actually throws if Uri is wrong
create_task(file->OpenAsync(FileAccessMode::Read)).then(/* ... */);
} catch (...)
{
// nothing to do here
}
});
Related
I want the user to pick a Date() from the CalendarDatePicker that return Windows::Foundation::IReference. However I call the Windows::Foundation::IReference::GetDateTime() will be able to raise an exception.
Output message:
Exception thrown at 0x00007FFDC893A839 (KernelBase.dll) in BlankApp1.exe: WinRT originate error - 0x80004001 : 'Not implemented'.
Exception thrown at 0x00007FFDC893A839 in BlankApp1.exe: Microsoft C++ exception:winrt::hresult_not_implemented at memory location 0x00000071C2DFCB18.
Exception thrown at 0x00007FFDC893A839 in
BlankApp1.exe: Microsoft C++ exception: [rethrow] at memory location
0x0000000000000000.
BlankApp1.exe has triggered a breakpoint.
The code that raised the exception:
Windows::Foundation::IReference<Windows::Foundation::DateTime> date = MyCalendar().Date();
if (date != nullptr) {
auto dt = date.GetDateTime();
}
else {
OutputDebugStringA("You not pick a date.");
}
The application breakdown in the UnhandledException of App.cpp:
App::App() {
InitializeComponent();
Suspending({ this, &App::OnSuspending });
#if defined _DEBUG && !defined DISABLE_XAML_GENERATED_BREAK_ON_UNHANDLED_EXCEPTION
UnhandledException([this](IInspectable const&, UnhandledExceptionEventArgs const& e) {
if (IsDebuggerPresent()) {
auto errorMessage = e.Message();
__debugbreak();
}
});
#endif
}
The value of errorMessage is "Not implemented".
Does anyone has an idea on how to get a Date?
I'm trying to ignore a exception that happen when run my test Methods.
I`m using a unitTest proyect.
The problem apears when
TestCleanup Method runs. It's a recursive method. This method clean all entities created in DB during the test. This is a recursive method because of dependences.
Anyway this method call to delete generic method in my ORM(Petapoco). It throws an exception if it can't delete the entity. Any problem, it run again with recursive way until delete it.
Now the problem, if i'm debugging VS stop a lot of times in Execute method because of failed deletes. But I can't modify this method to ignore it. I need a way to ignore this stops when i'm debugging tests. A way like DebuggerHiddenAttribute or similar.
Thanks!
I tried to use DebuggerHiddenAttribute, but cannot works in methods called by main method.
[TestCleanup(), DebuggerHidden]
public void CleanData()
{
ErrorDlt = new Dictionary<Guid, object>();
foreach (var entity in TestEntity.CreatedEnt)
{
try
{
CallingTest(entity);
}
catch (Exception e)
{
if (!ErrorDlt.ContainsKey(entity.Key))
ErrorDlt.Add(entity.Key, entity.Value);
}
}
if (ErrorDlt.Count > 0)
{
TestEntity.CreatedEnt = new Dictionary<Guid, object>();
ErrorDlt.ForEach(x => TestEntity.CreatedEnt.Add(x.Key, x.Value));
CleanData();
}
}
public int Execute(string sql, params object[] args)
{
try
{
OpenSharedConnection();
try
{
using (var cmd = CreateCommand(_sharedConnection, sql, args))
{
var retv = cmd.ExecuteNonQuery();
OnExecutedCommand(cmd);
return retv;
}
}
finally
{
CloseSharedConnection();
}
}
catch (Exception x)
{
OnException(x);
throw new DatabaseException(x.Message, LastSQL, LastArgs);
}
}
Error messages are not required.
I am trying to explicitly cancel a TryCatchFinally statement using:
`TryCatchFinally executionTask = new TryCatchFinally(){
#Override
doTry(){
//some nested try catch in #Async methods here.
}`
#Override
protected void doCatch(final Throwable e) throws Throwable {
//All fatal exceptions
if (e instanceof GameDayExecutionFailureException) {
//doSomethin
} else if (e instanceof CancellationException) {
//doSomething
} else if (e instanceof Exception) {
//doSomething
}
#Override
protected void doFinally() throws Throwable {
//doCleanUp from here.
}
};
Then inside an #Signal method:
executionTask.cancel(null); //tried passing a new RunTime Exception here as well.
The problem is, for some reason, after all child ActivityTasks are cancelled and doFinally() of the executionTask is executed, the workflow rather than gracefully exiting is throwing further CancellationException, which is leading to the following stack trace:
com.amazonaws.services.simpleworkflow.flow.DataConverterException: Failure serializing "java.util.concurrent.CancellationException" of type "class java.util.concurrent.CancellationException" when mapping key "null"
at com.amazonaws.services.simpleworkflow.flow.JsonDataConverter.throwDataConverterException(JsonDataConverter.java:90)
at com.amazonaws.services.simpleworkflow.flow.JsonDataConverter.toData(JsonDataConverter.java:78)
at com.amazonaws.services.simpleworkflow.flow.pojo.POJOWorkflowDefinition.throwWorkflowException(POJOWorkflowDefinition.java:177)
at com.amazonaws.services.simpleworkflow.flow.pojo.POJOWorkflowDefinition.access$300(POJOWorkflowDefinition.java:30)
at com.amazonaws.services.simpleworkflow.flow.pojo.POJOWorkflowDefinition$1.doCatch(POJOWorkflowDefinition.java:93) at --- continuation ---.(:0)
at com.amazonaws.services.simpleworkflow.flow.pojo.POJOWorkflowDefinition.execute(POJOWorkflowDefinition.java:67) at com.amazonaws.services.simpleworkflow.flow.worker.AsyncDecider$WorkflowExecuteAsyncScope.doAsync(AsyncDecider.java:68)
Caused by: com.fasterxml.jackson.databind.JsonMappingException: Infinite recursion (StackOverflowError) (through reference chain: com.fasterxml.jackson.databind.ObjectMapper["factory"]->
com.fasterxml.jackson.databind.MappingJsonFactory["codec"]->
com.fasterxml.jackson.databind.ObjectMapper["factory"]->
com.fasterxml.jackson.databind.MappingJsonFactory["codec"]->
com.fasterxml.jackson.databind.ObjectMapper["factory"]->
com.fasterxml.jackson.databind.MappingJsonFactory["codec"]->
com.fasterxml.jackson.databind.ObjectMapper["factory"]->
com.fasterxml.jackson.databind.MappingJsonFactory["codec"]->
com.fasterxml.jackson.databind.ObjectMapper["factory"]->
com.fasterxml.jackson.databind.MappingJsonFactory["codec"]->
..
I checked and most of the values inside CancellationException instance are null (messege,cause) etc.
But, shouldn't the exception be consumed by the doCatch ? Why is it being thrown out again ?
I'm running the following test code on SolrCloud using Solrj library:
public static void main(String[] args) {
String zkHostString = "192.168.56.99:2181";
SolrClient solr = new CloudSolrClient.Builder().withZkHost(zkHostString).build();
List<MyBean> beans = new ArrayList<>();
for(int i = 0; i < 10000 ; i++) {
// creating a bunch of MyBean to be indexed
// and temporarily storing them in a List
// no Solr operations performed here
}
System.out.println("Adding...");
try {
solr.addBeans("myCollection", beans);
} catch (IOException | SolrServerException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
System.out.println("Committing...");
try {
solr.commit("myCollection");
} catch (SolrServerException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
This code fails due to the following exception
Exception in thread "main" java.lang.NullPointerException
at org.apache.solr.client.solrj.impl.CloudSolrClient.requestWithRetryOnStaleState(CloudSolrClient.java:1175)
at org.apache.solr.client.solrj.impl.CloudSolrClient.request(CloudSolrClient.java:1057)
at org.apache.solr.client.solrj.SolrRequest.process(SolrRequest.java:160)
at org.apache.solr.client.solrj.SolrClient.add(SolrClient.java:106)
at org.apache.solr.client.solrj.SolrClient.addBeans(SolrClient.java:357)
at org.apache.solr.client.solrj.SolrClient.addBeans(SolrClient.java:312)
at com.togather.solr.testing.SolrIndexingTest.main(SolrIndexingTest.java:83)
This is the full stacktrace of the exception. I just "upgraded" from a Solr standalone installation to a SolrCloud (with an external Zookeeper single instance, not the embedded one). With standalone Solr the same code (with just some minor differences, like the host URL) used to work perfectly.
The NPE sends me inside the SolrJ library, which I don't know.
Anyone can help me understand where the problem originates from and how I can overcome it? Due to my unexperience and the brevity of the error message, I can't figure out where to start inquiring from.
Looking at your code, I would suggest to specify the default collection as first thing.
CloudSolrClient solr = new CloudSolrClient.Builder().withZkHost(zkHostString).build();
solr.setDefaultCollection("myCollection");
Regarding the NPE you're experiencing, very likely is due to a network error.
In these lines your exception is raised by for loop: for (DocCollection ext : requestedCollections)
if (wasCommError) {
// it was a communication error. it is likely that
// the node to which the request to be sent is down . So , expire the state
// so that the next attempt would fetch the fresh state
// just re-read state for all of them, if it has not been retired
// in retryExpiryTime time
for (DocCollection ext : requestedCollections) {
ExpiringCachedDocCollection cacheEntry = collectionStateCache.get(ext.getName());
if (cacheEntry == null) continue;
cacheEntry.maybeStale = true;
}
if (retryCount < MAX_STALE_RETRIES) {//if it is a communication error , we must try again
//may be, we have a stale version of the collection state
// and we could not get any information from the server
//it is probably not worth trying again and again because
// the state would not have been updated
return requestWithRetryOnStaleState(request, retryCount + 1, collection);
}
}
try {
// some error
} catch (any e) {
e.extendedInfo = 'New extended info';
//throw(e);
//cfcatch.extendedInfo = 'New extended info';
rethrow;
}
When I (re)catch this exception the extendedInfo is not displayed. What I want to happen is the raised exception keeps all of its pre-catch properties including the original tagContext and line numbers etc but gets a new value for extendedInfo.
I've tried copying the attributes of e into a new attributeCollection and throwing that with throw(e) or <cfthrow attributeCollection="#e#" /> but then the context is changed and the error displays the wrong line of source code.
While I'm at it is there a way to to drop the topmost stack object so an exception appears to have been thrown from the calling context. ie:
function myRethrow(e) (
throw(e); // <!-- error is actually throw here BUT ...
)
myRethrow(e); // <-- error should appear to have 'happened' here
Using Railo 3.2
I think you can use throw function like this:
try {
try {
// some error
}
catch (any e) {
e.extendedInfo = 'New extended info';
throw(argumentCollection = e);
}
}
catch (any e) {
WriteDump(e);
}
Works for me.