C
C#2y ago
Boson

❔ crashing

Any idea how to prevent this code from crashing unity?
SerialPort sp = new SerialPort("COM3", 9600);
sp.Open();

While true()
{
if (sp.IsOpen)
{
try
{
serialEvent(sp);
}
catch (Exception)
{
throw;
}
}

}

void serialEvent(SerialPort myPort)
{
string myString = myPort.ReadLine();
if (myString != null)
{
Debug.Log("Read " + myString);
}
}
SerialPort sp = new SerialPort("COM3", 9600);
sp.Open();

While true()
{
if (sp.IsOpen)
{
try
{
serialEvent(sp);
}
catch (Exception)
{
throw;
}
}

}

void serialEvent(SerialPort myPort)
{
string myString = myPort.ReadLine();
if (myString != null)
{
Debug.Log("Read " + myString);
}
}
11 Replies
Boson
BosonOP2y ago
Any idea how to prevent this code from crashing?
SerialPort sp = new SerialPort("COM3", 9600);
sp.Open();

While true()
{
if (sp.IsOpen)
{
try
{
serialEvent(sp);
}
catch (Exception)
{
throw;
}
}

}

void serialEvent(SerialPort myPort)
{
string myString = myPort.ReadLine();
if (myString != null)
{
Debug.Log("Read " + myString);
}
}
SerialPort sp = new SerialPort("COM3", 9600);
sp.Open();

While true()
{
if (sp.IsOpen)
{
try
{
serialEvent(sp);
}
catch (Exception)
{
throw;
}
}

}

void serialEvent(SerialPort myPort)
{
string myString = myPort.ReadLine();
if (myString != null)
{
Debug.Log("Read " + myString);
}
}
Angius
Angius2y ago
While true() this is not code that will even compile
Boson
BosonOP2y ago
Sorry I meant while (true)
Angius
Angius2y ago
Well, then, that's probably why it crashes, an infinite loop with no exit condition
Boson
BosonOP2y ago
using UnityEngine;
using System.Collections;
using System.IO.Ports;
using System.IO;
using System;
using System.Linq;
using System.Collections.Generic;

public class Arduino : MonoBehaviour
{
SerialPort sp = new SerialPort("COM3", 9600);

void Start() //called once when the program is run
{
sp.Open();
}

void Update() //called once every frame
{
if (sp.IsOpen)
{
try
{
serialEvent(sp);
}
catch (Exception)
{
throw;
}
}

}

void serialEvent(SerialPort myPort)
{
string myString = myPort.ReadLine();
if (myString != null)
{
Debug.Log("Read " + myString);
}
}
}
using UnityEngine;
using System.Collections;
using System.IO.Ports;
using System.IO;
using System;
using System.Linq;
using System.Collections.Generic;

public class Arduino : MonoBehaviour
{
SerialPort sp = new SerialPort("COM3", 9600);

void Start() //called once when the program is run
{
sp.Open();
}

void Update() //called once every frame
{
if (sp.IsOpen)
{
try
{
serialEvent(sp);
}
catch (Exception)
{
throw;
}
}

}

void serialEvent(SerialPort myPort)
{
string myString = myPort.ReadLine();
if (myString != null)
{
Debug.Log("Read " + myString);
}
}
}
That’s the original code
Angius
Angius2y ago
Could be because you're throwing nothing if an exception occurs Hard to tell why it crashes if there's no error message
Aaron
Aaron2y ago
they're not throwing nothing, throw; is a rethrow
MODiX
MODiX2y ago
Windows10CE#8553
REPL Result: Failure
try
{
throw new ArgumentException("idk");
}
catch (Exception)
{
throw;
}
try
{
throw new ArgumentException("idk");
}
catch (Exception)
{
throw;
}
Exception: ArgumentException
- idk
- idk
Compile: 342.404ms | Execution: 27.679ms | React with ❌ to remove this embed.
Aaron
Aaron2y ago
it rethrows the exception with a preserved stacktrace catch (Exception) { throw; } is the exact same as having no try/catch at all
Angius
Angius2y ago
Huh, TIL. Thought it's more like catch (_) { throw new Exception() }
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server