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 MylesLambert · Apr 21, 2013 at 01:15 PM · c#networkcatch

Try/ Catch does not work with Network.InitializeServer

Hey, I'm trying to catch an exception on Network.InitializeServer in a C# script, but rather than throwing the exception it is treating it like normal (just throwing the exception in the log and continuing)

 try {
     Network.InitializeServer(32, LevelInitiator.getInfo().HostingPort, false);
 } catch(UnityException e) {
     Debug.Log(e.Message);
 }

This is beign called in OnLevelWasLoaded, if that might cause a problem? Should this be submitted as a bug report or am I missing something? Thanks!

Comment
Add comment
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

2 Replies

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

Answer by fafase · Apr 21, 2013 at 02:25 PM

Ok, using the various comment. It seems you are not looking for an exception.

Network.InitializeServer returns an object of type NetworkConnectionError

I guess you could work work that.

 NetworkConnectionError errorNet = Network.InitializeServer(32, LevelInitiator.getInfo().HostingPort, false);

There is a variable noError that you could use. Fact is I am not familiar with Network connection and the docs is actually quite poor on this as it does not show any example nor what is the type of noError.

Here are the NetworkConnectionError members http://docs.unity3d.com/Documentation/ScriptReference/NetworkConnectionError.html

Comment
Add comment · Show 4 · 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 MylesLambert · Apr 21, 2013 at 02:29 PM 0
Share

Thanks fafase, this is what I was looking for. For anybody else looking into this,

NetworkConnectionError.CreateSocketOrThreadFailure == "Failed to initialize network interface. Is the listen port already in use?"

Thanks again!

avatar image Anxo · Dec 10, 2014 at 10:29 PM 0
Share

Hey LesLambert,

How is the line you commented used?

NetworkConnectionError.CreateSocketOrThreadFailure == "Failed to initialize network interface. Is the listen port already in use?"

avatar image Bunny83 · Dec 10, 2014 at 10:55 PM 0
Share

@Anxo: That line wasn't code. He just said that the error in the console "Failed to initialize network interface. Is the listen port already in use?" will cause Network.InitializeServer to return NetworkConnectionError.CreateSocketOrThreadFailure.

If you want to check if the server initilaization was successful, do this:

if (errorNet == NetworkConnectionError.NoError) { // server successfully started. } else { // error see "errorNet" for more details. }

avatar image Anxo · Dec 10, 2014 at 11:02 PM 0
Share

@Bunny83 Thanks

avatar image
0

Answer by Dave-Carlile · Apr 21, 2013 at 01:37 PM

Not sure I'm understanding the question. The code you're showing would handle the exception by logging it, then it would continue on because you've handled it in catch. If you want to log the exception and then quit you need to re-raise the exception by calling throw e; after you've logged it.

 try
 {    
   Network.InitializeServer(32, LevelInitiator.getInfo().HostingPort, false);
 } 
 catch(UnityException e)
 {
   Debug.Log(e.Message);
   throw e;
 }


C# documentation for try...catch: http://msdn.microsoft.com/en-us/library/0yd65esw(v=vs.90).aspx

Comment
Add comment · Show 5 · 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 fafase · Apr 21, 2013 at 01:58 PM 1
Share

Or simply do not use try/catch and let the program crash.

avatar image MylesLambert · Apr 21, 2013 at 01:58 PM 0
Share

Sorry I did a bad job of explaining it. It does not even call catch(), despite throwing the error "Failed to initialize network interface. Is the listen port already in use?". I realize now that this isn't actually an exception which is why catch() isn't being called. Is there a way I can get catch() to be called?

avatar image Dave-Carlile · Apr 21, 2013 at 02:04 PM 0
Share

Perhaps it's not throwing a subclass of UnityException. Try eli$$anonymous$$ating the exception class from catch, i.e...

 try
 {
   ... your stuff ...
 }
 catch     // <-- remove the exception class
 {
   Debug.Log(e.$$anonymous$$essage);
 }

See if that catches it.

avatar image fafase · Apr 21, 2013 at 02:20 PM 0
Share

Is UnityException inheriting from Exception (I guess it should)? You could simply catch a top exception

 try{
 }catch(Exception e){
    Debug.Log(e.$$anonymous$$essage);
 }

@DaveCarlile, I may be wrong but I think your last example won't do since e is not declared anywhere

avatar image MylesLambert · Apr 21, 2013 at 02:22 PM 0
Share

Thanks Dave, I tried that and catch() is still not being called. I think this is simply down to "Failed to initialize network interface. Is the listen port already in use?" not being an exception.

Is there another way I can do something if Network.InitializeServer throws an error? As I need to essentially just +1 to the port number in the case the port is already being used.

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

13 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

How do I send a simple Vector3 over a network? 1 Answer

Why is there an issue with Network recognition when networkView.isMine is called from within Update function ? 0 Answers

How can i spawn my player in matchmaker using my UI? 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