The main place between Xamarin System.Exception and Java.Lang.Exception needs

Source: Internet
Author: User

Reprint Please specify source: http://blog.csdn.net/supluo/article/details/42101569


Xamarin can use C # to develop Android apps, and that's not a lot to say.

Talk about using C # to develop Android and Java to develop Android between the two things to note in the exception handling:

See the following short code:

button. Click + = delegate {
Try
{
throw new System.Exception ("Throw SystemException");
}
catch (Java.Lang.Exception ex)
{
Text.text = "Capture Java.lang. Exception: "+ ex. Message;
}
};

Button2. Click + = Delegate
{
try {
throw new Java.Lang.Exception ("Throw Java.lang.");
}
catch (Exception ex) {
Text.text = "Capture System.Exception:" + ex. Message;
}

};

The code is very simple, I believe we all understand that using Xamarin to develop Android is probably the original C #, so you want to move through this mobile platform, but also some Java, but the work needs to be turned to C # to develop.

The reason for this, mainly to elicit, in Java development Android This well-known platform, we all know that in Java the base class is Java.Lang.Exception, and in C # is System.Exception, Everyone in the development process may misunderstand that all C # related will all be converted to Java-related (because the habit of Android is developed by Java), so it may be misunderstood as Java.Lang.Exception = System.Exception, At least it is possible to think of the two as equal in effect.

But after testing, the two are very different in effect, the above example code, the first button event can not catch the exception, and the second button event to catch the exception, the first button will throw uncaughtexception, see this everyone may be a little surprised, in the course of practice, We caught the anomaly, but sometimes it throws some uncaught exception, and I think one of the reasons is that, at least I've made this mistake, because when it comes to clear space, we might use Java's Exception class namespace using it, So the Exception in the program will default to Java.Lang.Exception, and catch this exception, and can not capture System.Exception, finally know the reason, happy! At least through my testing practice, I think the reason for this is also due to the difference in platform and the intermediate interpretation layer and the experience of the developers in the past. I also understand why in the application sub-class set up Java.Lang.Thread.DefaultUncaughtExceptionHandler but can not catch the program unhandled exception, occasionally can be obtained, The reason should be that it can only capture exceptions for Java.lang.exception and its subclass classes!


The main place between Xamarin System.Exception and Java.Lang.Exception needs

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.