Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
2
Question by dkely · Jan 29, 2015 at 11:04 AM · error-handling

Debug.LogError vs throw new exception

What is the difference between

 Debug.LogError("I am an error through Debug.LogError");
 throw new Exception("I am an error through new Exception");

I read that throwing an exception gets handled by Unity and turned into a Debug.LogError message anyway, which is what seems to be the case when i test it. Is it just a matter of preference or is there a reason to use one over the other?

Comment
Add comment · Show 2
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 KurtGokhan · Jan 29, 2015 at 01:18 PM 0
Share

What kind of libraries do you mean? .Net libraries (3.5 and lower) can be used in Unity. There is not a library that is Unity specific. Can you give a specific example and how it is relevant to Logging?

avatar image dkely · Jan 29, 2015 at 01:42 PM 0
Share

Sorry i edited the question to make it more clear.

4 Replies

· Add your reply
  • Sort: 
avatar image
12
Best Answer

Answer by KurtGokhan · Jan 29, 2015 at 02:53 PM

Most of the time they are not the same thing. One obvious difference is, when you throw an exception from a method, rest of the method will not be run and exception will be passed to calling method.

Exceptions are literally for exceptional cases. It means something has gone wrong. When something goes wrong you might want to correct it and continue running, or simply stop running. I can't know for what purpose you use Exception in your case. But know that LogError will not cause function to quit but throwing Exception will.

Comment
Add comment · Show 1 · 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
avatar image dkely · Jan 29, 2015 at 03:01 PM 0
Share

know that LogError will not cause function to quit but throwing Exception will.

This is something i missed. Now when to use each becomes obvious. Thanks for that!

avatar image
15

Answer by NoseKills · Jan 29, 2015 at 03:12 PM

They are two completely different things.

Debug.LogError() is a UnityEngine method that causes something to appear in Unity's console.

Throwing an exception is something that can be done when there's a possibility the code execution enters an unwanted, unsafe path and you want to stop the thread from executing further to prevent further damage.

If an exception is thrown and not caught&handled in the code, the program execution will usually stop and you will be thrown out of the app (back to the operating system).

When you run your code in the Unity editor, the editor catches the thrown errors and does a Debug.LogError() instead of crashing Unity or the app.

When you are debugging a game, it doesn't really matter which you use. You probably have to get rid of any such situations anyway before you can release the app.

If you are making a library of code for someone else and want them to be able to handle exceptions themselves, then throw an exception

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
avatar image
4

Answer by wariatus · Jul 03, 2016 at 12:32 PM

One more thing was not covered by above answers.

There is a difference in handling Exceptions between Android and (for example) iOS applications. When an application encounters an Exception on iOS it usually breaks and closes. However, on Android application continues running - sometimes causing totally unpredictable behaviors. And this might be extremely difficult to test & debug (especially when you are not aware of it).

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
avatar image
0

Answer by Jaakk0S · Aug 29, 2020 at 09:33 PM

Think about it the way that exceptions are a part of game functionality but Debug class is a Unity helper class with tools to help the developer who is debugging. In Unity, this is not as clear-cut design as with traditional loggers with log levels etc that are configured on a build/target platform level. Rather its just a simple helper utility.

Exceptions will work regardless of build or platform, whether "Use Player Log" configuration is on or whether the Debug invocations are is wrapped in precompiler condition checks, as recommended by Unity as a best practice.

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.

Follow this Question

Answers Answers and Comments

23 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Unity Asset Store Not Downloading - "Please Wait" 1 Answer

Error:m TypeTree->m Index != -1 && m Overrides->size() 0 Answers

Rethrow an exception and display new exception's message in console? (C#) 1 Answer

I can not get out from under the Error 0 Answers

can someone fix the loop error.. Why this app running only for a moment in my phone ? 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