- Home /
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!
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
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!
Hey LesLambert,
How is the line you commented used?
NetworkConnectionError.CreateSocketOrThreadFailure == "Failed to initialize network interface. Is the listen port already in use?"
@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. }
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
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?
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.
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
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
Follow this Question
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
How can i spawn my player in matchmaker using my UI? 0 Answers