locked
Throw or Debug. LogError ? RRS feed

  • Question

  • Hi, everybody.

    Why and when do I use "Debug.LogError" if I can always use "throw"?

    I just don't understand the logic of the choice.

    I have an example of the class "SteamAPI", they are initializing and in cases of failure, "Debug.LogError.... return;" is called where the error is written, BUT why not call "throw"? The same way an error will be written to the console.

    I understand that "throw" interrupts execution.
    Sunday, April 19, 2020 5:29 AM

All replies

  • Hi, everybody.

    Why and when do I use "Debug.LogError" if I can always use "throw"?

    I just don't understand the logic of the choice.

    I have an example of the class "SteamAPI", they are initializing and in cases of failure, "Debug.LogError.... return;" is called where the error is written, BUT why not call "throw"? The same way an error will be written to the console.

    I understand that "throw" interrupts execution.

    Throwing an exception means to stop execution of the current logic and terminate the program. The only time I have ever seen debug.Logerror ever used is in a debug situation.

    Most developers 99.9% of the time are going to throw an exception and stop program execution.


     
    • Edited by DA924x Sunday, April 19, 2020 7:13 PM
    Sunday, April 19, 2020 7:12 AM
  • If one wanted to log the exception, then one would use one of the logging libraries like Log4Net, which can be used to log more than just exceptions.

    https://logging.apache.org/log4net/release/features.html

    Sunday, April 19, 2020 7:17 PM
  • Hi DreadLypa,

    Thank you for posting here.

    The main difference is that Debug.LogError will not stop the program but throw new exception will.

    Assuming such a scenario, there may be multiple errors in your program, you want to find them all, and then modify the code.

    At this time, you may not want to stop the program every time you encounter an error but hope that it will continue to run to find other errors.

    This may be one of the use cases of Debug.LogError.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Monday, April 20, 2020 1:57 AM