Skip to content

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

@brendanzagaeski

Description

@brendanzagaeski

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?

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions