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 /
avatar image
0
Question by zhuchun · Aug 12, 2013 at 04:15 AM · coroutinewwwyield

How to yiled a try/catch block?

Hi

I have a WWWhelper class to get resources. In case of fails, we usually set a try/catch for it so it can notice user try again later.

     IEnumerator TryGet(){
         try{
             //some codes here may throw exception
             yield return StartCoroutine(WWWHelper.Get());
         }
         catch{
             Debug.log("Oops");
         }
     }

Unfortunately, the Editor said

 Cannot yield a value in the body of a try block with a catch clause

So how can I make it ? Thank you

Comment
Add comment · Show 6
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 CodeAssembler · Aug 12, 2013 at 04:49 AM 0
Share

I think the problem seems to be that when you 'yield' the Coroutine can't really get in contact back with the try{}catch{} block, as it has been 'sent' to another execution space in the game code that is not longer reachable by try/catch.

So I guess the answer to your problem will be to implement a local try{}catch{} on the inside of the WWWHelper.Get() method? So then you just launch the Coroutine knowing that it will inform itself if something went wrong on it's inside codes?

avatar image zhuchun · Aug 12, 2013 at 05:45 AM 0
Share

@CodeAssembler Yes that's work. But it looks better if decouple our code, say, make WWWHelper to handle all WWW requests and scripts focus on calls and feed back. In other word, we may unable to expect what's going to feed back for each call(such as network fail, target ain't exists etc.), although catched exception inside of WWWHelper.

Hence I'm wondering is there any way to let game knows that WWWHelper.Get fails(like throw it as usual) so we can handle exceptions manually.

avatar image zhuchun · Aug 13, 2013 at 03:16 AM 0
Share

Oh, get it.

@CodeAssembler please post your comment as answer so it can be marked and vote up :]

avatar image Benproductions1 · Aug 13, 2013 at 04:11 AM 0
Share

I converted @CodeAssembler's comment to an answer :)

avatar image zhuchun · Aug 13, 2013 at 06:47 AM 0
Share

Thank you :)

Show more comments

2 Replies

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

Answer by CodeAssembler · Aug 12, 2013 at 06:04 AM

I think you should check this link, I feel it explains best what I'm trying to tell you : http://answers.unity3d.com/questions/24640/how-do-i-return-a-value-from-a-coroutine.html

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 Ted-Bigham · May 20, 2015 at 11:55 PM

A pretty clean way to manage try/catch with WWW is to have the helper coroutine exit normally regardless of result. Then have a getter for the response value, which throws an exception if there was an error.

 IEnumerator TryGet()
 {
     WWWHelper helper = new WWWHelper();
     yield return StartCoroutine(helper.Get());
     try{
         //some codes here may throw exception
         helper.GetValue();
     }
     catch{
          Debug.log("Oops");
      }
 }


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 zhuchun · May 21, 2015 at 12:57 AM 0
Share

wow, it looks pretty decent!

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

19 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

Related Questions

Can I use WWW in Start()? 1 Answer

WWW isDone when lost internet connection, it return true and not wait for internet connection (Andriod) 1 Answer

NullReferenceException in nested Coroutine 0 Answers

How to set timer for WWW helper? 1 Answer

Yield Problems 2 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