Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some of the new wrapped Java exceptions use BCL exceptions that differ from the related BCL types #4127

Open
brendanzagaeski opened this issue Jan 15, 2020 · 3 comments
Assignees

Comments

@brendanzagaeski
Copy link
Contributor

Context

60363ef added an option to wrap Java exception types in .NET exception types for certain Android APIs.

JavaDictionary.Get() is an example:

https://github.com/xamarin/xamarin-android/blob/8d7557aad23a3c0724e1996aade3072b1a09f298/src/Mono.Android/Android.Runtime/JavaDictionary.cs#L72-L86

Expected behavior

JavaDictionary inherits from Java.Lang.Object and implements IDictionary:

https://github.com/xamarin/xamarin-android/blob/8d7557aad23a3c0724e1996aade3072b1a09f298/src/Mono.Android/Android.Runtime/JavaDictionary.cs#L12

The documentation for the IDictionary.Item[Object] indexer property mentions two exceptions:

  • ArgumentNullException
  • NotSupportedException

Actual behavior

JavaDictionary.Get() can currently throw the following two different exceptions instead:

  • InvalidCastException
  • NullReferenceException

Depending on the goals of the Java exception wrapping, throwing different System exceptions from JavaDictionary.Get() than the ones documented for IDictionary.Item[Object] might be unintended. Maybe only the exception conditions documented for IDictionary.Item[Object] (like "key is null") need to be surfaced as System exceptions, and other exceptions, if any, can be left as Java exceptions?

Related scenarios

If this JavaDictionary.Get() scenario does look good to change, then there are probably a few additional similar cases to change too.

Another slightly different case is JavaCollection.Add(). I think it might have been wrapped because JavaCollection implements ICollection, but I just noticed today that the non-generic ICollection interface doesn't include an Add() method. Only the generic ICollection<T> includes an Add() method. So maybe JavaCollection.Add() doesn't need to wrap any exceptions?

@brendanzagaeski
Copy link
Contributor Author

Additional background context: From a first quick chat with @jonpryor, it sounded like this might be unintentional, so I added an issue here to help with tracking potential changes to the behavior.

@jonpryor jonpryor modified the milestones: Under Consideration, d16-5 Jan 16, 2020
@brendanzagaeski
Copy link
Contributor Author

Another related case is that with the new exception mode enabled Android.Runtime.InputStreamInvoker.Read() will at the moment throw things like:

  • Java.Lang.IndexOutOfBoundsException instead of ArgumentException or ArgumentOutOfRangeException
  • Java.Lang.NullPointerException instead of ArgumentNullException
  • System.IO.IOException instead of ObjectDisposedException

@brendanzagaeski
Copy link
Contributor Author

One more interesting case is AndroidClientHandler, which currently throws exceptions like Java.Net.UnknownHostException instead of System.Net.Http.HttpRequestException. I think that type might have been unintentionally overlooked in the first set of changes because it isn't a binding for an existing Android or Java type.

Adjusting AndroidClientHandler.SendAsync() to throw the documented excpetions for HttpClient.SendAsync() and HttpClientHandler.SendAsync() will fix #3216.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants