Jar代码抛出子类,但是xamarin绑定代码抛出基类

时间:2014-06-15 06:53:26

标签: android xamarin xamarin.android

Jar中的Java代码:

public class FatherClass
{
    public void throwChildAClassException()
    {
        throw new FatherClass.ChildAClassException();
    }

    public class ChildAClassException extends RuntimeException
    {
        private static final long serialVersionUID = 1L;
    }

    public class ChildBClassException extends RuntimeException
    {
        private static final long serialVersionUID = 1L;
    }
}
在Xamarin中

当我调用方法ThrowChildAClassException()时,它会将基类抛出为RuntimeException

↓↓↓代码↓↓↓

try
{
    new FatherClass().ThrowChildAClassException()
}
catch(ChildAClassException childAE)
{
    // debugged but does not reach here
}
catch(ChildAClassException childBE)
{
    // no invoking
}
catch(RuntimeException e)
{
    // debugger approaches here. Isn't it wrong or a bug?
}

那么,我怎样才能获得像ChildAClassException这样的子类? 谢谢,全部。

1 个答案:

答案 0 :(得分:0)

由我修复......

try
{
    new FatherClass().ThrowChildAClassException()
}
catch(RuntimeException e)
{
     // Android.Runtime.Extensions.JavaCast<ChildAClassException>(e);
     // Android.Runtime.Extensions.JavaCast<ChildBClassException>(e);

    string currentExceptionSimpleName = e.Class.SimpleName;
    string classChildAClassExceptionSimpleName = typeof(ChildAClassException).Name;
    string classChildBClassExceptionSimpleName = typeof(ChildBClassException).Name;

    if(currentExceptionSimpleName == classChildAClassExceptionSimpleName)
    {
        // ...
    }
    else if(currentExceptionSimpleName == classChildBClassExceptionSimpleName)
    {
        // ...
    }
}