Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
avatar image
1
Question by David_Knopp · Jan 08, 2016 at 11:37 AM · c#consoleerror-handling

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

I want to catch an exception, and rethrow with a more specific error message:

 Dictionary<string, string> m_dictionary;
 
 try
 {
      var value = m_dictionary["key"];
      ...
 } 
 catch ( KeyNotFoundException e )
 {
      throw new KeyNotFoundException( "Error message.", e );
 }

But Unity will display the original exception's error message in the console. It gives this:

 KeyNotFoundException: The given key was not present in the dictionary.

Instead of this:

 KeyNotFoundException: Error message.

Is there any way to get this behavior without throwing away the call stack information of the original exception?

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 Bonfire-Boy · Jan 08, 2016 at 12:02 PM 0
Share

It should be displaying the new message too, but further down where it says "rethrow as", right?

So the information you have added is there. Also, a client that catches your new exception will see your new message as the exception.message.

So I'm curious, what's your use case that makes it important that the new message appears at the top of the console entry when the exception isn't caught?

avatar image David_Knopp Bonfire-Boy · Jan 08, 2016 at 05:49 PM 0
Share

Yeah, it does provide the "rethrow as" message, but I feel the outer exception's information should be more readily available than that. When I see an error, I want to be able to look at the first message as it is displayed in the console and have that information be immediately useful.

If the dictionary in the example was a dictionary of states for a state machine, seeing "StateNotFoundException: state 'coolState' was not present in the State$$anonymous$$achine." is so much clearer than a $$anonymous$$eyNotFoundException.

So really I just don't want to have to do any thinking :P

avatar image Bonfire-Boy David_Knopp · Jan 09, 2016 at 06:46 PM 0
Share

That's fair enough. I think you're right that it would be more helpful shown the other way round, outer exception first,

avatar image vintar · Jan 09, 2016 at 08:18 PM 0
Share

Better to just use : if (!dictionary.TryGetValue(key, out value))

avatar image Goldseeker vintar · Feb 17, 2016 at 03:42 PM 0
Share

And do what exactly if key is in fact not found?

1 Reply

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

Answer by Bunny83 · Feb 17, 2016 at 08:03 PM

As Bonfire said the innermost exception is the most important and usually the most detailed exception. If an exception is catched by a more general system somewhere up the callstack, it usually don't know what exactly happend. They usually just add a more general exception as outer exception and keep the inner exception.

If you actually handle the exception properly and you want to provide a more detailed exception, you might want to remove the inner exception and just throw your customized exception.

However This approach is actually bad design, especially for realtime applications. A try-catch block adds quite a bit of overhead. Exceptions should never be used as a way of flowcontrol. If an exception can be avoided you should go that route.

In the case of a dictionary the method which does this is TryGetValue

 string value;
 if (m_dictionary.TryGetValue("key", out value))
 {
     // key was found, value has been filled.
 }
 else
 {
     // key wasn't found, take further actions.
     // Either fill "value" manually if possible or throw an exception if there's no way around.
 }

Finally some information on when to use exceptions and when not.

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

7 People are following this question.

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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Build a console similar to minecraft 1 Answer

Error message from console after destroying an Object with sound Logical Error ? 1 Answer

Illuminating a 3D object's edges OnMouseOver (script in c#)? 1 Answer


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