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
0
Question by Rydrako · Oct 04, 2011 at 02:25 AM · errorcatch

Why is my Try and Catch not working?

Hello, I have a very annoying problem. I have my game save all these variables to a certain file path that I believe should work on all computers but, in case it doesn't work I would like my game to notice that it fails to save and show up a GUI box saying my custom fail message to the player. So I tried this:

  // Close the file (^ above is where I write the var's into the file)
 saveStream.Close();
 
 try 
 { 
 
     throw saveStream.Close ();
 } 
 
 catch (err)
 {
 
         MoveAround.saveFailed = true;
         return;
 }
 
 // Give confirmation that the file has been saved
 
 print ( "Data Saved at: " + filePath2 + "/" + fileName2 );
 
 MoveAround.saveWork = true;

in the save script's save function, and I get these errors:

 'void' is not a valid argument type for throw, only strings and exceptions are allowed.

so um...HOW DO I FIX THIS (I've seen a c# example but not a javascript example of a try+catch)???!!!!! Your help will be greatly appreciated!

Comment
Add comment · Show 3
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 vxssmatty · Oct 04, 2011 at 02:30 AM 0
Share

Those errors are just incomplete syntax?... fix those and see if you have any actual syntax errors with your code?

avatar image Rydrako · Oct 04, 2011 at 02:45 AM 0
Share

I don't know how to...I never did a try catch before...not at all

avatar image Rydrako · Oct 04, 2011 at 02:47 AM 0
Share

I've seen a c# example but not a javascript example.

1 Reply

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

Answer by Bunny83 · Oct 04, 2011 at 03:44 AM

The keywork throw is used to manually throw an exception. Like the error states: throw must be followed by an instantce of an exception class or a string.

If you just want catch errors that happens during saveStream.Close() just remove the "throw". All eaxceptions that are thrown within a try - catch block are catched by the nearest try-catch-block.

 try 
 { 
     saveStream.Close ();
 }
 catch (err)
 {
     MoveAround.saveFailed = true;
     return;
 }

Here's a simple example of throw:

 try
 {
     throw "Error, plz abort";
     Debug.Log("This will never execute");
 }
 catch(err)
 {
     Debug.Log("Exception catched: " + err);
 }

ps: the code is not tested and i'm not sure if the syntax is the same in UnityScript

Comment
Add comment · Show 2 · 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 Bunny83 · Oct 04, 2011 at 03:47 AM 0
Share

btw: if the saving fails it would most likely fail when you open your stream or when you write to the stream, so you should put that code into the try block and not just the Close()

avatar image CHPedersen · Oct 04, 2011 at 06:41 AM 2
Share

Agreed. On a related note, what is the type of that err variable? Is that just Exception, that is, the base class?

JavaScript's typing scheme confuses me this way. If that's a FileStream, then the only exception Close can throw is an IOException, and he should be catching that, not the general one. If he decides to add more code to the try-block later, catching the base exception might unintentionally swallow other types of exceptions invisibly, making this harder to debug.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How can I check what levels are included in the build in code? 1 Answer

Try/ Catch does not work with Network.InitializeServer 2 Answers

Try and catch very slow 1 Answer

Can't Catch IndexOutOfRangeException 1 Answer

SUCCEEDED(hr) and grey captured images? 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