Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Force fatal error if class used in wrong way

Discussion in 'Scripting' started by Zephni, Jan 18, 2016.

  1. Zephni

    Zephni

    Joined:
    Apr 23, 2015
    Posts:
    100
    Hopefully my question makes sense, and I have Googled several different phrases but it keeps thinking I'm talking about something else.

    What I would actually like to do is cause a fatal error inside an if statement. In other words do the same thing that happens if you were to try and change the color.a property of an object, in that case you would have to set color equal to a new Color() object, otherwise Unity would show in red text down the bottom that this is not possible and the application would quit.

    Is there a way I can force my class to do this. Say I want a value to always be positive when passed in to the constructor, and if it isn't positive I want the application to quit and say "Blabla value must be positive" or whatever.

    I hope that makes sense, is something like this possible?
     
  2. Munchy2007

    Munchy2007

    Joined:
    Jun 16, 2013
    Posts:
    1,732
    Last edited: Jan 18, 2016
    Kiwasi likes this.
  3. Glockenbeat

    Glockenbeat

    Joined:
    Apr 24, 2012
    Posts:
    669
    In case you don't want to stop execution but only want to show and error you can use Debug.LogError. Depends on what you want to do and where you want to do it.
     
    Kiwasi likes this.
  4. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    You generally can't, and don't want to, cause a fatal error. Read up on exception handling. In most cases your app should be able to gracefully recover from an exception. Crashing should be a last resort.
     
    Munchy2007 likes this.
  5. Munchy2007

    Munchy2007

    Joined:
    Jun 16, 2013
    Posts:
    1,732
    I sometimes throw exceptions during development, which is what I assumed the OP intentions were. I agree however that this isn't something you'd want for a released version.
     
  6. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Throwing exceptions is fine. Not catching exceptions that shouldn't happen is fine too.

    Fatal error, that smacks of the blue screen of death from the Windows 95 days...

    Edit: To clarify, fatal error is a term used by the operating system when you really, really mess things up. The system must reboot. You don't want to cause fatal errors.
     
  7. Munchy2007

    Munchy2007

    Joined:
    Jun 16, 2013
    Posts:
    1,732
    I assumed the OP incorrectly used the term fatal error when he meant throw an exception, hence my first post. On reflection I should have mentioned that at the time.