Skip to content

Exceptions are not propagated properly #2

@nandaccio

Description

@nandaccio

If RandomOrgClient roc = RandomOrgClient.GetRandomOrgClient(YOUR_API_KEY_HERE); is executed without an active internet connection, the resulting exception is not handled by the surrounding try/catch block.

Execution always stops here:
Image

Instead of getting to the catch clause of my try/catch block:

 try
	 {
             var client = RandomOrgClient.GetRandomOrgClient(RANDOM_ORG_KEY);
             var randomNumbers = client.GenerateIntegers(10, 1, 10, true);
	 }
	 catch (Exception ex)
	 {
	 	// IT NEVER GETS HERE
	 }

In other words, exceptions triggered inside ThreadedRequestSending (the "core" part of the library) are not propagated back to the caller and they just make the applications crash.

How to reproduce:

  1. Implement a simple console application containing the code above
  2. Run it WITHOUT being connected to the Internet or just throw an exception from ThreadedRequestSending

The issue is likely caused by the fact that requested are sent in separate threads (ThreadedRequestSending), but this approach prevents proper handling of exceptions raised by the library. If that is the actual reason for this behaviour, I would suggest to use one of the following approaches to propagate the error back to the caller:

  • Ashared data structure (e.g., ConcurrentQueue).
  • Events or callbacks.
  • Switch to Task and async/await for easier exception propagation.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions