I have the following code in my project, deleteselector is a form that has a datagridview (with autosize columns) on it.
try
{
if (deleteSelector.ShowDialog() == DialogResult.OK)
{
}
}
catch (InvalidOperationException)
{
//Bug workaround
}
The try catch is because a pop-up form with a gridview on it trows a invalidoperationexception once in a while. This is the suggested workaround, see
http://connect.microsoft.com/VisualStudio/feedback/details/145633/invalidoperationexception-thrown-when-a-form-with-a-bound-datagridview-with-auto-sizing-columns-is-shown
Earlier, I used Show on the deleteSelector, and the workaround worked perfectly. Now, with showdialog it seems that the error is not catched anymore (I get an uncatched error message). Why is the error not catched?
ShowDialog runs the dialog on a separate thread, so the exception is being thrown in a different stack to your exception handler.
I suggest you try to find a different workaround - just catching InvalidOperationException is pretty horrible, and I wouldn't like to bet that the form would be in a sensible state afterwards.
Related
I've recently run into this problem, and it doesn't make sense.
the following snippet is real:
try
{
File.Create(targetFile);
//File.WriteAllText(targetFile, $"test {DateTime.Now.ToString()}");
}
catch (UnauthorizedAccessException uaex)
{
}
I have checked it step by step, as soon as i get with the debugger to the "File.Create()" method, the exception rises, and it doesn't enter the catch block, also, if i remove the try-catch, it doesn't bubble up to the calling of the method which contains this.
Anyone got any idea why the try-catch and the bubbling doesn't work?
ps. The location where I am trying to create the file is write protected on purpose, this is just a way to check if it is.
I've made a mistake.
The exception is actually being caught, if you put anything in the catch block, it does execute.
To be fair the debugger confused me, by showing the exception pop-up right at the calling of the method, but that was solved by restarting the IDE
I thought I could catch an exception thrown by another class in my project, but I must be doing it wrong. In the first class, I'm surrounding my call to the other class with a try/catch block:
try
{
ImportPowerPoint.CreateTitle(textBoxPpt.Text, textBoxPkg.Text);
}
catch (FormatException ex)
{
MessageBox.Show(ex.Message, "ERROR",
MessageBoxButtons.OK,
MessageBoxIcon.Warning);
}
In the second class, this is where I'm throwing the exception:
if (!_layoutMap[(int)Layouts.A].ContainsValue(Fields.Title))
throw new FormatException("Standard (A) Layout does not contain a title.");
if (!_layoutMap[(int)Layouts.A].ContainsValue(Fields.Txt1))
throw new FormatException("Standard (A) Layout does not contain a txt1.");
if (!_layoutMap[(int)Layouts.A].ContainsValue(Fields.Prompt))
throw new FormatException("Standard (A) Layout does not contain a prompt.");
When I run the program, it breaks immediately where the exception is thrown, instead of displaying the error window that I defined in the try/catch block. Am I not handling this try/catch correctly?
For clarification, I'm forcing the exception to occur by removing certain pieces from the PowerPoint that I'm parsing. When the program fails, for instance, because I removed the Title Field, the exception thrown is of type FormatException. Shouldn't my catch in the calling class handle this?
EDIT: I think I may have found out something that is causing this to happen. The exception being thrown is in another thread. So, since it's in another thread, could this mean that this is why my try/catch isn't catching the exception?
It is breaking in the IDE to notify that the exception is thrown. If you press F5 again to continue running, your catch handler should be hit.
There is nothing class-related about exceptions. Your calling code (ImportPowerPoint.CreateTitle(...);) should be able to catch exceptions thrown by CreateTitle().
See if your Visual Studio is configured to catch all exceptions (opposed to catching only unhandled ones):
Go to the 'Debug' menu
Select 'Exceptions...'
Uncheck check boxes under the 'Thrown' column
You would typically want the IDE to catch unhandled exceptions, so I would keep the other column checked.
The problem here appears to be because this is a multithreaded program. I'm looking at using the AppDomain.UnhandledException Event to handle my exceptions.
I'm having problems working with EntityFramework. While below peace of code works fine on my PC, when it's transported to a VPS (with everything properly preinstalled), it gives me a Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object, but the message box that is supposed to catch this does not show up. Any suggestions?
Thank you in advance.
var cc = new CopierContext();
try
{
MessageBox.Show(cc.Database.Connection.ConnectionString.ToString());
var matchingProviders2 = cc.Providers.Where(prov => prov.Login == "batman");
}
catch (Exception e)
{
MessageBox.Show(e.InnerException.Message);
}
Update:
I finally got to the core of the problem. The reason is: I've had .NET 4 on VPS, while application was developed using .NET 4.5. Installing the latter one removed all problems. Thank you for all your help.
I don't know how you are running it on your VPS, but if it is not launched under the interactive user account, your message boxes will not show up.
Well from the docs on the Exception.InnerException Property
The InnerException property returns the same value as was passed into
the constructor, or a null reference
Since you're catching any old exception catch (Exception e) its quite possible that the exception that's being thrown isn't the exception you were expecting and doesn't have a InnerException. This means your catch block may be raising an exception.
There are several actions you could take.
Do not catch System.Exception exception in anything but a top level exception handler. Only catch exceptions you know what to do with. Which leads to...
Set up a top level exception handler
Finally when logging or displaying exception messages at least make sure you have an inner exception before you try and use it.
MessageBox.Show( (e.InnerException != null ? e.InnerException : e).Message );
Don't use a MessageBox as it is a service; log them to disk instead such that you can recall them, or perhaps automatically mail them to you such that you are up to date on problems occuring.
You will also want to add e.InnerException.StackTrace to the log.
My bet is that you did not configure something (or did not configure it correctly) and cc.Database.Connection.ConnectionString is null. Calling .ToString() causes the NullReferenceException. That's why you don't see the message box. As other people said - using MessageBox for this kind of debugging is not a good idea. In the catch you should print not message but e.ToString() it will show the stack trace that should point to the place where the problem is.
I'm trying to handle exceptions in a dialog so that if any exception occurs, the dialog will be closed and the application will not crash. As you can see, I use a simple try-catch block:
IDialogView dialog = null;
try
{
if (_dialogViewModel == null)
{
dialog = ViewFactory.SomeDialog();
_dialogViewModel = new DialogViewModel(dialog);
_dialogViewModel.LoadData();
}
_dialogViewModel.ShowDialog();
}
catch (Exception ex)
{
if (dialog != null)
dialog.Close();
_dialogViewModel = null;
MessageBox.Show("Sorry, there was an error in the dialog.", "Error",
MessageBoxButton.OK, MessageBoxImage.Error);
return;
}
The problem happens when an error occurs in button's CanExecute() event handler. Error is successfully caught, but when I show the MessageBox to the user, CanExecute() executes again, and so the error happens again. In the end it results in application crash.
I've googled some info, and it were said to make sure that there is no exceptions in CanExecute() event handler. But something like this can happen somewhere else, and that's why I want to simply catch ALL exceptions in the dialog entry point without working with every method.
So, my question is: how to destroy the dialog so that after exception catch it won't show again anymore? Close() didn't work, because before closing it still calls CanExecute().
As you found when you googled, you should make sure that a CanExecute handler is a) lightweight and b) never throws an exception. You are running into the main reason for this: a CanExecute will be run repeatedly, and automatically, by the framework. It will run when focus changes, on input events, when databindings change, and in response to a number of other reasons that you have little to no control over.
The problem is: you do have an error, and that error is occurring repeatedly. That means you can choose between crashing, or showing the dialog repeatedly. Or, you can do something about the error.
Your answer: fix the error.
(Your handler as it stands is fine for your other errors. Leave it there. But this particular error, you need to fix right away.)
I am trying to catch exceptions for my form client not being able to establish a connection to a server with this in the Connect callback:
try
{
client.EndConnect(async);
}
catch (Exception e)
{
client.Close();
return;
}
This works fine but this behavior is encapsulated in to a class so I want to call throw; instead of return; so that the client class can handle it instead, like so:
try
{
client.Connect(host, port);
}
catch
{
Console.WriteLine("Could not connect to: " + host + ":" + port.ToString());
}
So why not just call throw; then? Well, for some reason if I call throw;, throw new Exception();, or basically anything other than return; the program failsfast. I'm really not sure what's causing this. I tried removing client.Close(); to see if it was the problem but nothing. If I don't call return; the program just immediately exits with no error.
Anyone know what's going on here?
Edit: I do not understand why I am getting downvoted so much. I showed how I am attempting to catch these exceptions and am asking why they are not working properly. I think the problem may be (not sure, just came up with this) because within the asynchronous callback, because it is a new thread in the ThreadPool, calling throw; does not do anything because, because it is not synchronous, there is nothing to throw back to and the application dies. Even with this knowledge, I am not sure how to solve this problem unless I put some sort of try-catch on the entire program.
I suppose a solution could be just sticking with return; because there is nothing to throw back to (due to the asynchronous callback nature of the method) and instead raise an event indicating a failure of connection. Regardless, many thanks for the downvotes and helping me solve this problem. Oh wait...
What's happening is that the EndConnect is not happening on the same thread as your BeginConnect. When EndConnect throws an exception, it is caught by the worker thread's unhandled exception handler, which fails fast (the other option is that it gets ignored and you never find out that your code isn't working).
You have to come up with a way to tell your main form thread that the connect failed.
As others have pointed out, you'll need to catch your exception one way or another to avoid program termination.
For some ideas on how you can do that "globally", see How to catch ALL exceptions/crashes in a .NET app. Whether this is actually a good idea depends on the specific needs of your program...
Relevant for WinForms:
Can't tell based on your question alone, but in case this is actually a WinForms application, you may need to be cognizant of the difference in behavior of modal forms that throw exceptions, depending on whether the debugger is active or not. Let's say we have two forms - the second one is shown as a modal child of the first one:
If application was started through debugger, second form is closed and and stack unwinding goes all the way to the first form's catch block (if any).
If application is started outside debugger, stack unwinding stops before second form is closed and generic exception message is displayed. The second form stays open and catch block in the first form is never reached.