• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by alex.shen · Apr 01, 2012 at 07:48 AM · optimizationexception

Using exception when `fast but no exception` is turned on.

Can I use C# exceptions when Fast but no exception optimization is turned on ?

EDIT: Test Code

 using UnityEngine;
 using System.Collections;
 using System;

 public class TestException : MonoBehaviour {
     private string exceptionMessage;
     // Use this for initialization
     void Start () {
     }
 
     // Update is called once per frame
     void Update () {
     
     }
 
     void GenerateExceptionRandomly()
     {
         int a = UnityEngine.Random.Range(0, 2);
         if (a != 0)
         {
         throw new ApplicationException("Exception");
         }
     }
 
     void OnGUI()
     {
         GUILayout.BeginVertical();
         if (exceptionMessage != null)
         {
             GUILayout.Label(exceptionMessage);
         }
         else
         {
             GUILayout.Label("No exception");
         }
     
         if (GUILayout.Button("Reset exception", GUILayout.Width(200), GUILayout.Height(50)))
         {
             exceptionMessage = null;
         }
     
         if (GUILayout.Button("Random exception", GUILayout.Width(200), GUILayout.Height(50)))
         {
             try
             {
                 GenerateExceptionRandomly();
             }
             catch (Exception e)
             {
                 exceptionMessage = e.StackTrace;
             }
         }
         GUILayout.EndVertical();
     }
 }
Comment
Add comment · Show 5
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Kryptos · Apr 02, 2012 at 09:22 AM 1
Share

Your answer is in the question. What don't you understand in Fast but no exception?

Note that this should always be the case for release version as exception handling in iOS is simulated are really really slow.

avatar image alex.shen · Apr 02, 2012 at 11:14 AM 0
Share

@$$anonymous$$ryptos: I tested my code with try/catch block on iPad with Fast but no exception, it worked. I guess that even it worked but the optimization does not guarentee that my code will always work as expected. So if I use the optimization, I have to translate my code so that they do not rely on exceptions .

avatar image Kryptos · Apr 02, 2012 at 01:17 PM 1
Share

Did you really succeed to catch an exception with 'Fast but no exception'? In my experience, the code does compile but fails when an exception is thrown.

avatar image alex.shen · Apr 02, 2012 at 01:33 PM 1
Share

@$$anonymous$$ryptos: I just tested my code. The exception was caught successfully. I print a debug message on the screen when the exception is caught in catch block. See the test code above.

avatar image Kryptos · Apr 05, 2012 at 07:38 AM 0
Share

Good to know. $$anonymous$$aybe this concerns only the exceptions created and managed by Unity engine (such as $$anonymous$$issingReferenceException). Or it just means that no exception will be catched by Unity engine: in my experience NullReferenceException always make the game crash.

Anyway I tend to not use exception and rather rely on proper checking (if-statement and null-comparison) to prevent those exceptions to happen.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by CaptainZen · Sep 01, 2014 at 11:25 AM

Here's information from Unity's devs (as of September 2012):
1. "Fast but no exceptions" has limitation that it will crash if exception is thrown from native code. 100% managed exceptions work fine.
2. "Fast but no exceptions" also breaks some reflection code. Typically it is the code that tries to discover currently executing stack frame / assembly. This going to be improved with Unity 4.0.
3. "Fast but no exceptions" improves managed->native calls by about 3x. So calls like transform.position become ~3x faster.

Source: http://forum.unity3d.com/threads/how-much-slower-is-slow-and-safe-vs-fast-but-no-exceptions.150254/#post-1033345

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Animation of terrain causes drastic performance drop (fps) 0 Answers

Debug build runs better than release build. 0 Answers

Performance and size of NPOT texture in POT atlas? 0 Answers

Help with making script efficient 3 Answers

Fixing fill rate issue on Android - best approach? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges