I'm trying to catch an error displayed in Revit to perform some handle operations. Error is due to the connection of points as shown in the image below.
Error image
This is what I have tested with so far.
try
{
var pipe = Pipe.Create(doc, firstPipeType.Id, level.Id, startCon,
pathXyz[0]);
}
catch (Autodesk.Revit.Exceptions.InvalidOperationException e)
{
message = e.Message;
return Result. Failed;
}
Based on the documentation, I am trying to catch and handle the following exception.
"Autodesk.Revit.Exceptions.InvalidOperationException: Thrown when the new pipe fails to connect with the connector."
The error message is different from the exception. The instructions on how to handle the error message are provided by The Building Coder in the topic group on Detecting and Handling Dialogues and Failures.
Related
I'm getting a very strange exception using a UWP StreamSocket, 99.9% of the time this code functions as expected and the communication with the device works properly, however I get the following exception every once in a while.
The exception message:
The operation failed because an invalid combination of workqueue ID and flags was specified. (Exception from HRESULT: 0xC00D36FF)
Sample code for the issue:
using (StreamSocket analyzerSocket = new StreamSocket())
{
HostName hostName = new HostName(host);
// Set NoDelay to false so that the Nagle algorithm is not disabled
analyzerSocket.Control.NoDelay = false;
try
{
// Connect to the server
await analyzerSocket.ConnectAsync(hostName, port.ToString()).AsTask(new CancellationTokenSource(_timeout).Token);
}
catch (Exception e)
{
var x = e;
}
}
Screenshot of exception in code:
The Stack Trace:
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
at Analyzer.<SendMessageAsync>d__120.MoveNext() in zzz.cs:line 779
I've tried my GoogleFu and I was able to find issues with MediaPlayer or Linux kernels but nothing seemed to relate to this issue with StreamSockets.
While I can trap this error and work around the issue I would like to know what's going on in case it's a symptom of a bigger issue.
Thanks in advance.
Edit 1
I thought this might be related to http://referencesource.microsoft.com/#mscorlib/system/runtime/compilerservices/TaskAwaiter.cs,be57b6bc41e5c7e4 based on the code comment of "// And throw an exception if the task is faulted or canceled.".
However I still get this expcetion when I am not using the ".AsTask(new CancellationTokenSource(timeout.Value).Token)"
Edit 2
When I have this in a loop, to constantly send messages to our device, the messages are being received until this exception occurs. Once the exception occurs and I tell it to continue and try again, the exception re-occurs over and over in the loop and the device stops receiving messages.
Edit 3
So I've tried the following, to connect to a different device, with a different instance of the StreamSocket object... and it generates the same error!
using (StreamSocket analyzerSocket = new StreamSocket())
{
HostName hostName = new HostName(host);
// Set NoDelay to false so that the Nagle algorithm is not disabled
analyzerSocket.Control.NoDelay = false;
try
{
// Connect to the server
await analyzerSocket.ConnectAsync(hostName, port.ToString()).AsTask(new CancellationTokenSource(_timeout).Token);
}
catch (Exception e)
{
using (StreamSocket analyzerSocket2 = new StreamSocket())
{
HostName hostName2 = new HostName("xxx.xxx.xxx.xxx");
// Set NoDelay to false so that the Nagle algorithm is not disabled
analyzerSocket.Control.NoDelay = false;
// Connect to the server
await analyzerSocket2.ConnectAsync(hostName2, port.ToString());
}
throw;
}
}
It feels like some sort of cross threading type of issue... I'm grasping at straws right now as I cannot trap and bypass the error as once the error occurs I can no longer talk to the devices and I must exit the application to get it to work again.
Does anyone have any other ideas or confirmation that it looks like cross threading type of issue?
Thanks in advance.
The only way I have found to prevent this issue is to stop using the "StreamSocket" altogether.
I switched my code to use the "System.Net.Sockets" namespace using the Socket object instead and with some modifications to my code I haven't encountered this issue since.
Code sample:
https://msdn.microsoft.com/en-us/library/windows/apps/hh202858%28v=vs.105%29.aspx?f=255&MSPPError=-2147217396
I've developed an application used by a third-party company.
Since I'm a horrible coder the application does still have some bugs which causes it to crash (unhandled nullpointerexception for example).
It's a Windows-forms application running on .NET 4.5 and now they are just getting the classic "An unhandled exception caused the app to terminate, press details for more info".
Trying to convince them that pressing "Details" and sending the stack-trace to me is really useful but they all seem reluctant.
Would it be possible to automate this behaviour, like show them a custom global "Exception catcher" where they can just press a button to send it to me by E-mail.
Inbefore "Global exception handling is bad" and "Why does your application throw nullpointerexceptions, you are a bad coder etc."
BR Tomas Anyuru
I guess the exceptions you get are unhandled.
Because of this, you will have to use the Application.ThreadException event to handle them. Because there is no .NET automatic mail sending and message display, you will have to implement your own inside this event.
Please have a look of an answer I wrote to have some examples of Exception catching strategies.
wrap your whole main() function in try-catch statement.
this way any un-handled exception will roll back and will be catched in your catch block:
static void main()
{
try
{
// the application code...
}
catch (Exception ex)
{
DialogResult result = MessageBox.Show(
"Some error occured, please click ok to send it to the develpoer");
if (result = OK)
email(ex); // this is your function to send the email.
// useful information is also in ex.message
// here program will exit without error!
}
}
you can use log 4 net it is open source logging tools, use a lot by Java developer, and this version is specially for .Net http://logging.apache.org/log4net/
In addition to #Shamim code, you can wrap your main function in try, catch block, since the catch block here will track down the exception occurred inside any function called in the try block.
Shooting a mail inside catch block sometime throws and exception about Thread abort, so finally would be the right place to do so :
catch (Exception err)
{
mailBody = "Error: " + Convert.ToString(err.Message) + "<br /> Source: " + Convert.ToString(err.Source);
//Can display some message to user in an Literal Control from here.
}
finally
{
if (!string.IsNullOrEmpty(mailBody))
{
mailObject.To.Add(mailTo);
mailObject.CC.Add(mailCc);
mailObject.Body = mailBody;
MailService(mailObject);
}
}
MailService is a method to send mail which accept a MailObject as parameter.
Something is fishy, and I know I must miss something.
I'm trying to connect to an Oracle instance, and due to firewalls, I cannot debug from my development machine. So, for now I'm writing to the event log.
My ASP.net page displays "Object reference not set to an instance of an object", and I'm trying to find the error.
The comments in the code points to my funny issue.
Here is a code sample:
try
{
OracleConnection oc = new OracleConnection(MyConnectionString);
//Event log successfully created
oc.Open();
//Event log NOT created, thus error occurred in previous statement (I would assume)
}
catch (Exception ex)
{
//Event log NOT created (?? - okay maybe there was no error)
}
finally
{
//Event log successfully created
}
So it seems like the catch clause are not called, therefor I would assume no error. But yet, I receive an error on the page.
The only other logical explanation is that the error occurs at the end of the finally clause or in the beginning of the catch clause, but the code to write the event log is the same as in the beginning.
So, what else can it be? Or perhaps, how else can I test it to find the error?
The problem statement turned out to be that the logging in the catch clause produced the error because ex.Message was null.
I'm using early bound classes and upon calling SaveChanges() I'm getting this generic error message which tells me nothing.
The inner exception is "an unexpected error occured"
Tracing is turned on for the server and just reposts the same error message.
Is there some way to get useful information out of this?
Thanks
Try turning customErrors on in the config file.
Try wrapping your code in:
try
{
//crm code here
}
// Catch any service fault exceptions that Microsoft Dynamics CRM throws.
catch (FaultException<Microsoft.Xrm.Sdk.OrganizationServiceFault>)
{
// You can handle an exception here or pass it back to the calling method.
throw;
}
If you catch the specific exception that's being thrown it may shed more light on the problem. Failing that, post the code / ask it in a new question and we might be able to help further.
There is a details section in the soap exception, this usually includes some additional details that help resolve your problem.
try
{
response = crm.Execute(request);
}
catch (SoapException e)
{
//Console.Write(e.Detail.InnerXml);
throw new Exception (e.Detail.InnerXml, e);
}
I have created a WCF service and client and it all works until it comes to catching errors. Specifically I am trying to catch the EndpointNotFoundException for when the server happens not to be there for whatever reason. I have tried a simple try/catch block to catch the specific error and the communication exception it derives from, and I've tried catching just Exception. None of these succeed in catching the exception, however I do get
A first chance exception of type
'System.ServiceModel.EndpointNotFoundException'
occurred in System.ServiceModel.dll
in the output window when the client tries to open the service. Any ideas as to what I'm doing wrong?
I was able to replicate your issue and got interested (since I needed the same). I even researched a way to handle \ catch first chance exceptions but unfortunately it is not possible (for managed code) for .net framework 3.5 and below.
On my case I always get a System.ServiceModel.CommunicationObjectFaultedException whenever something gets wrong on the service or whenever I access a down service. It turns out that c#'s using statement is the cause since behind the scene, the using statement always closes the service client instance even if an exception was already encountered (it doesn't jump to catch statement directly).
What happens is that the original exception System.ServiceModel.EndpointNotFoundException will be replaced by the new exception System.ServiceModel.CommunicationObjectFaultedException whenever the using tries to close the service client instance.
The solution i've made is to not use the using statement so that whenever an exception is encountered inside the try block it will instantly throw the exception to the catch blocks.
Try to code something like:
DashboardService.DashboardServiceClient svc = new Dashboard_WPF_Test.DashboardService.DashboardServiceClient();
try
{
svc.GetChart(0);
}
catch (System.ServiceModel.EndpointNotFoundException ex)
{
//handle endpoint not found exception here
}
catch (Exception ex)
{
//general exception handler
}
finally
{
if (!svc.State.Equals(System.ServiceModel.CommunicationState.Faulted) && svc.State.Equals(System.ServiceModel.CommunicationState.Opened))
svc.Close();
}
Instead of:
try
{
using (DashboardService.DashboardServiceClient svc = new Dashboard_WPF_Test.DashboardService.DashboardServiceClient())
{
svc.GetChart(0);
}
}
catch (System.ServiceModel.EndpointNotFoundException ex)
{
//handle endpoint not found exception here (I was never able to catch this type of exception using the using statement block)
}
catch (Exception ex)
{
//general exception handler
}
And you'll be able to catch the right exception then.
Take a look at this post for details on this possible solution. The code shows use of a generate proxy but is valid on ChannelFactory and others as well.
Typical here-be-dragons pattern
using (WCFServiceClient c = new WCFServiceClient())
{
try
{
c.HelloWorld();
}
catch (Exception ex)
{
// You don't know it yet but your mellow has just been harshed.
// If you handle this exception and fall through you will still be cheerfully greeted with
// an unhandled CommunicationObjectFaultedException when 'using' tries to .Close() the client.
// If you throw or re-throw from here you will never see that exception, it is gone forever.
// buh bye.
// All you will get is an unhandled CommunicationObjectFaultedException
}
} // <-- here is where the CommunicationObjectFaultedException is thrown
Proper pattern:
using (WCFServiceClient client = new WCFServiceClient())
{
try
{
client.ThrowException();
}
catch (Exception ex)
{
// acknowledge the Faulted state and allow transition to Closed
client.Abort();
// handle the exception or rethrow, makes no nevermind to me, my
// yob is done ;-D
}
}
Or, as expressed in your question without a using statement,
WCFServiceClient c = new WCFServiceClient();
try
{
c.HelloWorld();
}
catch
{
// acknowledge the Faulted state and allow transition to Closed
c.Abort();
// handle or throw
throw;
}
finally
{
c.Close();
}
This may be a reporting issue for the debugger, rather than not actually catching the exception. this post gives some tips on resolving it, if that is the case... Why is .NET exception not caught by try/catch block?
What is a First Chance Exception?
First chance exception messages most
often do not mean there is a problem
in the code. For applications /
components which handle exceptions
gracefully, first chance exception
messages let the developer know that
an exceptional situation was
encountered and was handled.
Place a try catch block in the CompletedMethod.
An Example:
...
geocodeService.ReverseGeocodeCompleted += ReverseGeocodeCompleted(se, ev);
geocodeService.ReverseGeocodeAsync(reverseGeocodeRequest);
}
private void ReverseGeocodeCompleted(object sender, ReverseGeocodeCompletedEventArgs e)
{
try
{
// something went wrong ...
var address = e.Result.Results[0].Address;
}
catch (Exception)
{ // Catch Exception
Debug.WriteLine("NO INTERNET CONNECTION");
}