- Home /
What to do when there is an error at runtime
Hi!
In a Java program, when the program is executing and there is an error, the compiler stops the program and throws an exception. Is there something similar to this in Unity? I mean, in case my player reference is null or something similar, I want to some how now that it is null and to quit the game. I do not want the user to be able to continue playing if something is broken in the game. Anything already implemented in Unity or should I have a method that is called every time there is an error to quit the application?
Thanks!
Answer by Kiwasi · Jul 16, 2014 at 01:45 AM
C# uses the try-catch-finally and throw methods
I tried using a try catch block. I did this:
using System;
try {
//something that breaks
}
catch(Exception e) {
Debug.LogException(e, this);
}
This, however, does not stop or quit the game. I can continue playing. Should I do Application.quit()
?
Thanks!
If you want it to quit then yes, you should call Application.quit
However in most cases you will want to attempt to recover from the error first, or at the least display a dialog box with the error details.
So recovering is just trying to fixing what is wrong. A dialog box will just be a window on the screen? Also, is this good practice for a final release? I mean, these tips might work when debugging, but what about a game that is already released? Should I also display a window to the user with the error? (They probably won't understand anything). Should I quit and somehow send a log file to my account or something similar?
Thanks!