I want to make a multiplayer game and use facepunch steamworks to create a lobby. I am trying to initialize and validate the client.
try
{
Steamworks.SteamClient.Init(480);
Debug.Log(Steamworks.SteamClient.IsValid);
}
catch ( System.Exception e )
{
Debug.Log(e.Message);
}
But after running this code I get a System.NullReferenceException error. Maybe I installed the library incorrectly. Has anyone faced this problem?
Related
I am wondering if i am able to get a method to throw an exception similarly to the below Java code.
private void iAmChecked() throws FileNotFoundException {
FileReader fr = new FileReader("Not Here.txt");
}
I am currently developing an Android Application using Xamarin, I want the app to display a Dialog message if internet connection is lost. I am wrapping the appropriate method calls within the OnCreate with a try catch. The Catch will display a Dialog message and then terminate the application. I am aiming for the methods to throw an error back to the onCreate method so that I can do this.
I am wondering a few points.
Are there better ways of doing this
How can i replicate the above Java code in c#
Will it work
Thanks,
Joe
That code doesn't fires any exception by itself, it only indicates it can throw an exception, but not by itself but by the functions called inside it. On C# that's unnecesary, you don't need to specify what kind of exceptions your code can throw.
There is a better way of doing this:
bool AmIChecked()
{
return System.IO.File.Exists("Not Here.txt");
}
This question already has answers here:
Catch all exception in Unity
(4 answers)
Closed 5 years ago.
Is it possible to detect an error in a C# script running in Unity without reading the console log ?
I need this when I have to build the game and test it in a mobile. If there is an error when running it will display a message box that shows the error.
I understand that we can use Unity Log Viewer to print all the Log in the device. But I ask for another way to do this. I ask a simpler solution. My solution I think it is a best to detect a minor errors when it runs successfully in editor, but has problems when running in a device due it just displays a showMessageBox Error.
I need to detect if something is wrong when running. I realize that there is Debug.LogError which we can detect an error. But Debug.LogError just print the message type by us to pointing what the object error is. What I need is to detect a global error like console and show error message from unity engine.
What I need may be something like :
void Update() {
showMessageBox(isErrorDetect());
}
showMessageBox => is a function to show message box.
isErrorDetect => this will print an error if detect like a console.
If someone understand what I mean, then please, give me a solution.
Thank You
So no one understand what i mean. But i got the solution my self.
We can use : Application.logMessageReceived that what i mean.
Script below will popup if there is an error when running the game at device. Not tested yet. But iam sure it work. It will help you when no error running in editor but got error when running at mobile.
I make it myself. Example :
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class ErrorHandlePopUp : MonoBehaviour {
public Image PopUp;
string error;
void OnEnable() {
Application.logMessageReceived += HandleLog;
}
void OnDisable() {
Application.logMessageReceived -= HandleLog;
}
void HandleLog(string logString, string stackTrace, LogType type) {
if (type == LogType.Error) {
error = error + "\n" + logString;
PopUp.gameObject.SetActive (true);
PopUp.transform.GetChild (0).GetComponent<Text> ().text = "Error";
PopUp.transform.GetChild (1).GetComponent<Text> ().text = error;
}
}
public void Dismiss() {
PopUp.gameObject.SetActive (false);
}
}
I would recommend you to use the Debugger of the IDE. I assume you are writting your scripts in Monodeveloper or in Visual Studio. Both let you debug.
Even in the official website of Unity they recommend to use the debugger instead of a huge Debug.Log, which will be insane when your game gets complex.
You can read more about this here:
https://unity3d.com/es/learn/tutorials/topics/scripting/monodevelops-debugger
I'm following the docs and using this block:
auth.CreateUserWithEmailAndPasswordAsync(email, password).ContinueWith(
task => {
if (!task.IsCanceled && !task.IsFaulted) {
// User has been created.
} else {
// User creation has failed.
}
});
I know that the creation has failed but I'm trying to figure out why. I tried
print(task.exception.message.tostring())
And it tells me System.Aggregate exception was thrown so then I check the inner exception and when I do ToString() it just says FirebaseException. If I check the message or the data of that firebase exception there is nothing. How do you get the error information?
https://groups.google.com/forum/#!topic/firebase-talk/D6KpMSYeZyw
Found my answer:
"Auth's implementation is stubbed out on the desktop. We're going to be improving it shortly with a stub that doesn't throw exceptions but it will still not actually contact our backends."
I am working on an Excel Addin using C#, Visual Studio 2012. I am trying to get the instance of Excel's Application object so as to keep track of the currently active workbook and worksheet (ActiveWorkbook, ActiveWorksheet).
I see that most other related questions on SO have replies suggesting to use the following:
(Excel.Application)Marshal.GetActiveObject("Excel.Application");
I have also tried using this:
(Excel.Application)Globals.ThisAddIn.Application;
In both of the cases I get NullReferenceException. After looking at the workaround suggested here: http://support.microsoft.com/kb/316125/en-us, I tried the following to test both methods.
public CurrentSpreadSheet()
{
try
{
this.CurrentApplication = (Excel.Application)Globals.ThisAddIn.Application;
}
catch (NullReferenceException)
{
MessageBox.Show("Excel application object not registered. Trying plan B..");
//Getting Excel's application object instance
int iSection = 0, iTries = 0;
tryAgain:
try
{
iSection = 1; //Attempting GetActiveObject
this.CurrentApplication = (Excel.Application)Marshal.GetActiveObject("Excel.Application");
iSection = 0; //GetActiveObject succeeded so resume or go for normal error handling if needed
this.CurrentApplication.Visible = true;
}
catch (Exception err)
{
System.Console.WriteLine("Visual C# .NET Error Attaching to Running Instance of Office Application .. yet.");
if (iSection == 1)
{
//GetObject may have failed because the
//Shell function is asynchronous; enough time has not elapsed
//for GetObject to find the running Office application. Wait
//1/2 seconds and retry the GetObject. If we try 20 times
//and GetObject still fails, we assume some other reason
//for GetObject failing and exit the procedure.
iTries++;
if (iTries < 20)
{
System.Threading.Thread.Sleep(500); // Wait 1/2 seconds.
goto tryAgain; //resume code at the GetObject line
}
else
{
MessageBox.Show("GetObject still failing. Process ended.");
}
}
else
{
//iSection == 0 so normal error handling
MessageBox.Show(err.Message);
}
}
}
}
The output is:
Excel application object not registered. Trying plan B..
GetObject still failing. Process ended.
In some rare cases "plan B" does work; I don't see the second message box.
CurrentSpreadSheet is a singleton and I intend to update it during startup from the provided class ThisAddIn.
In ThisAddIn I have something like:
private CurrentSpreadSheet css = CurrentSpreadSheet.Instance;
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
///...Some code
css.updateCurrentSpreadSheet();
}
Is there a better way of getting the Application object? If this is not possible right during the startup, is there a better way by which I can keep track of currently active worksheet/workbook right from the startup of excel/my add-in? Currently I am depending on the Application object (e.g. (Excel.Workbook)this.CurrentApplication.ActiveWorkbook;) and some event handlers to keep track of the current workbook and worksheet.
I tried using ExcelDNA:
this.CurrentApplication = (Excel.Application)ExcelDna.Integration.ExcelDnaUtil.Application;
This works some of the times but mostly gives this error:
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> ExcelDna.Integration.XlCallException: Exception of type 'ExcelDna.Integration.XlCallException' was thrown.
Keep in mind that an Office add-in runs inside the Office process. So you never want to find an external process. The boilerplate ThisAddIn class that you get when you use a Office project template hands you the Application object you are looking for on a silver platter, use its Application property. The first time you can get to it is in the Startup event, make sure you don't try it earlier. So don't do anything drastic in your constructor and don't initialize class members with initialization expressions, wait until Startup fires.
The relevant MSDN page is here, "Accessing the Object Model of the Host Application" section.
You might take a closer look at http://netoffice.codeplex.com/ There you can find more stuff about Add-In's for Word/Excel/Powerpoint.
Also keep in mind the GuidAttribute when you load your Add-In, because depending on your Office version it won't work anymore and also, check to kept your project whether 32/64-bit, best is Any CPU. Also, keep in mind to register your Add-In into the registry for further usage. If you want further information, don't hesitate to write me a mail.
http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.guidattribute.aspx
I am trying to make an application that may rely on a Wacom tablet. It is not necessary for the program to work, but it is a nice addition. Although, it should also work on computers without the Wintab32.dll installed and I would like to make a check to see if the DLL is available.
This piece of code generates an error and I would like to catch the error before it is generated. I am using WintabDN to support .net Wacom applications.
if (WintabDN.CWintabInfo.IsWintabAvailable())
{
// Initialize Wintab
WintabLib.Initialize(true);
WintabLib.OnWintabPacketReceived += WintabLib_OnWintabPacketReceived;
}
FAILED IsWintabAvailable: System.DllNotFoundException: Unable to load
DLL 'Wintab32.dll': The specified module could not be found.
(Exception from HRESULT: 0x8007007E)
at WintabDN.CWintabFuncs.WTInfoA(UInt32 wCategory_I, UInt32 nIndex_I, IntPtr IpOutput_O)
at WintabDN.CWintabInfo.IsWintabAvailable()
The problem with this error is that it is a messagebox and not an exception thrown by the package. How can I prevent this messagebox from showing up?
It's a bit tricky, because the underlying exception is already being handled.
Trying to block the messagebox from showing is going to be difficult, if not impossible. You can do something really hacky like a watcher thread to catch the foreground window changing, then manually sending a mouse click message to the application's underlying message queue to try and trigger the OK button on the message box.
So I have two suggestions: this is the official topic on DLL search order; you can use it to emulate the search and find the file in advance.
If you don't want to do that you could import the LoadLibrary method from kernel32.dll and then call that to try and load the DLL that the WintabDN library is trying to use; if you get an exception then you know that the WintabDN library will also. Here's an almost related topic that'll help bind LoadLibrary: http://blogs.msdn.com/b/jonathanswift/archive/2006/10/03/dynamically-calling-an-unmanaged-dll-from-.net-_2800_c_23002900_.aspx
Try following which answers:
(The Wintab SDK examples won't run without Wintab installed on the system. How can I make my program work if there is no tablet (no Wintab) on the system?)
http://www.wacomeng.com/windows/docs/WacomWindevFAQ.html
How can I prevent this messagebox from showing up?
You can't:
public static bool IsWintabAvailable()
{
IntPtr buf = IntPtr.Zero;
bool status = false;
try
{
status = (CWintabFuncs.WTInfoA(0, 0, buf) > 0);
}
catch (Exception ex)
{
MessageBox.Show("FAILED IsWintabAvailable: " + ex.ToString());
}
return status;
}
:D
I hate library developers, writing such kind of code. They shouldn't decide, what error-handling mechanism you will choose.
But it is open-source, you can fix this issue.
Or you can post a bug at their tracker and wait, when they will fix it.