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 sgmongo · Sep 13, 2013 at 06:03 PM · javascriptcatch

Try Catch not executing.

I'm trying to use a try catch statement but the catch portion is not executing. Does the try catch argument get called with unity?

my code:

 try{
             connectionTestResult = Network.TestConnection();
         }
 catch (Exception) {
             Debug.Log("Internet is not available.");
         }

My error code from the console:

Cannot resolve connection tester address, you must be connected to the internet before performing this or set the address to something accessible to you. UnityEngine.Network:TestConnection() Servertesting:TestConnection() (at Assets/2d/Scripts/Servertesting.js:37) Servertesting:OnGUI() (at Assets/2d/Scripts/Servertesting.js:30)

What I'm trying to accomplish: Here

Full code I'm working on:

 var testStatus = "Testing network connection capabilities.";
     var testMessage = "Test in progress";
     var shouldEnableNatMessage : String = "";
     var doneTesting = false;
     var probingPublicIP = false;
     var serverPort = 9999;
     var connectionTestResult = ConnectionTesterStatus.Undetermined;
     
 
     var timeTaken : float = 0.0F;
     var maxTime : float = 5.0F;
     var internetconnection : boolean = true;
 
     
     // Indicates if the useNat parameter be enabled when starting a server
     var useNat = false;
 
     
 
     
     function OnGUI() {
         GUILayout.Label("Current Status: " + testStatus);
         GUILayout.Label("Test result : " + testMessage);
         GUILayout.Label(shouldEnableNatMessage);
         if (!doneTesting)
             
             TestConnection();
     }
     
     function TestConnection() {
         // Start/Poll the connection test, report the results in a label and 
         // react to the results accordingly
         try{
             connectionTestResult = Network.TestConnection();
         }catch (Exception) {
             Debug.Log("Internet is not available.");
             internetconnection = false;
             doneTesting = true;
         }
         
         
         if (internetconnection)    { 
             Debug.Log("Internet Connection Available.");
         }
         
         
         if (internetconnection) {
         
             if (timeTaken < maxTime) {
                 timeTaken += Time.deltaTime;
             }
             else {
                 doneTesting = true;
                 return;
             }
         
             switch (connectionTestResult) {
                 case ConnectionTesterStatus.Error: 
                     testMessage = "Problem determining NAT capabilities";
                     doneTesting = true;
                     break;
                     
                 case ConnectionTesterStatus.Undetermined: 
                     testMessage = "Undetermined NAT capabilities";
                     doneTesting = false;
                     break;
                                 
                 case ConnectionTesterStatus.PublicIPIsConnectable:
                     testMessage = "Directly connectable public IP address.";
                     useNat = false;
                     doneTesting = true;
                     break;
                     
                 // This case is a bit special as we now need to check if we can 
                 // circumvent the blocking by using NAT punchthrough
                 case ConnectionTesterStatus.PublicIPPortBlocked:
                     testMessage = "Non-connectable public IP address (port " +
                         serverPort +" blocked), running a server is impossible.";
                     useNat = false;
                     // If no NAT punchthrough test has been performed on this public 
                     // IP, force a test
                     if (!probingPublicIP) {
                         connectionTestResult = Network.TestConnectionNAT();
                         probingPublicIP = true;
                         testStatus = "Testing if blocked public IP can be circumvented";
                         timer = Time.time + 10;
                     }
                     // NAT punchthrough test was performed but we still get blocked
                     else if (Time.time > timer) {
                         probingPublicIP = false;         // reset
                         useNat = true;
                         doneTesting = true;
                     }
                     break;
                 case ConnectionTesterStatus.PublicIPNoServerStarted:
                     testMessage = "Public IP address but server not initialized, "+
                         "it must be started to check server accessibility. Restart "+
                         "connection test when ready.";
                     break;
                                 
                 case ConnectionTesterStatus.LimitedNATPunchthroughPortRestricted:
                     testMessage = "Limited NAT punchthrough capabilities. Cannot "+
                         "connect to all types of NAT servers. Running a server "+
                         "is ill advised as not everyone can connect.";
                     useNat = true;
                     doneTesting = true;
                     break;
                     
                 case ConnectionTesterStatus.LimitedNATPunchthroughSymmetric:
                     testMessage = "Limited NAT punchthrough capabilities. Cannot "+
                         "connect to all types of NAT servers. Running a server "+
                         "is ill advised as not everyone can connect.";
                     useNat = true;
                     doneTesting = true;
                     break;
                 
                 case ConnectionTesterStatus.NATpunchthroughAddressRestrictedCone:
                 case ConnectionTesterStatus.NATpunchthroughFullCone:
                     testMessage = "NAT punchthrough capable. Can connect to all "+
                         "servers and receive connections from all clients. Enabling "+
                         "NAT punchthrough functionality.";
                     useNat = true;
                     doneTesting = true;
                     break;
         
                 default: 
                     testMessage = "Error in test routine, got " + connectionTestResult;
             }
             if (doneTesting) {
                 if (useNat)
                     shouldEnableNatMessage = "When starting a server the NAT "+
                         "punchthrough feature should be enabled (useNat parameter)";
                 else
                     shouldEnableNatMessage = "NAT punchthrough not needed";
                 testStatus = "Done testing";
             }
         }
     }


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 perchik · Sep 13, 2013 at 08:27 PM 0
Share

Did you edit the question and change the question? Seems like the current answers no longer apply.

It also looks like it didn't throw an exception

avatar image gardian06 · Sep 13, 2013 at 08:31 PM 0
Share

@perchik originally the question read "... catch(UnityEngine){..." and it looks like Network.TestConnection(); might throw some kind of exception, but I don't know of any off hand, and even if it does I would expect more to be in the try block

avatar image sgmongo · Sep 13, 2013 at 08:51 PM 0
Share

The question has not really changed... I'm getting some kind of error from Network.TestConnection() when I attempt it without a active internet connection. However, it is not executing the catch portion of the code.

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by gardian06 · Sep 13, 2013 at 08:29 PM

if you are going to give arguments to catch then they either have to be extensions of the exception class, or already exist in System.Exception.

if you want the catch block to execute of a specific type of exception then you either need to define it, or use one that is defined, but that exception will still need to be thrown (either by the system, or by you ( throw MyException();)

if you want the catch block to execute on any exception then give catch no arguments

 try{
 }catch{
 }
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 sgmongo · Sep 13, 2013 at 08:41 PM 0
Share

Gives sintax errors... $$anonymous$$onoDevelop won't accept it.

avatar image
0

Answer by Seizure · Sep 13, 2013 at 07:57 PM

Remove UnityEngine from the catch.

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 flaviusxvii · Sep 13, 2013 at 07:59 PM 0
Share

Can't just remove it..

avatar image
0

Answer by flaviusxvii · Sep 13, 2013 at 07:58 PM

catch(UnityEngine)

This isn't going to work. That would catch something thrown of type 'UnityEngine' which isn't something that is thrown. Use 'Exception' or some other more specific Exception class.

Comment
Add comment · Show 8 · 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 sgmongo · Sep 13, 2013 at 08:19 PM 0
Share

I have used the following variations without success:

catch(Exception)

catch(err)

catch(UnityException)

catch(e)

None of them have activated. I'll update the op.

avatar image flaviusxvii · Sep 13, 2013 at 08:29 PM 1
Share

Network.TestConnection() doesn't throw any exceptions.. http://docs.unity3d.com/Documentation/ScriptReference/Network.TestConnection.html

You have to get the return value and exa$$anonymous$$e it.

avatar image sgmongo · Sep 13, 2013 at 08:33 PM 0
Share

What is this in reference to then?

Cannot resolve connection tester address, you must be connected to the internet before perfor$$anonymous$$g this or set the address to something accessible to you. UnityEngine.Network:TestConnection() Servertesting:TestConnection() (at Assets/2d/Scripts/Servertesting.js:37) Servertesting:OnGUI() (at Assets/2d/Scripts/Servertesting.js:30)

It shows up in the console as a error.

avatar image sgmongo · Sep 13, 2013 at 08:48 PM 0
Share

In the interest of full disclosure, here is my full code, and a link to the issue I set out to solve. The current code is basically that testconnection code with a little bit inserted to deal with the error sent by initiating the " connectionTestResult = Network.TestConnection(); " portion without a active internet connection. I'm trying to find an intelligent way of testing for a active connection that doesn't spit error codes, since it is not inherent in the engine for some reason.

BTW: Thanks for bearing with me, I know this is probably simple for ya, but its a new frontier for myself ^.^

Original Problem

Too much to add in one comment. Adding full code to op. :/

avatar image flaviusxvii · Sep 13, 2013 at 08:57 PM 1
Share

I've never messed with networking stuff in Unity, so it's out of my depth. I do know that if you can't catch this error with a generic 'Exception', then it may just be uncatchable. Not all errors are formally thrown as exceptions!

Show more comments

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

21 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

Related Questions

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

Try and catch very slow 1 Answer

Can't Catch IndexOutOfRangeException 1 Answer

can I use eval to test the syntax on code a user enters during runtime? 0 Answers

Why is my Try and Catch not working? 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