exception

Catching exit(1);

I have a MFC SDI application which during startup loads a DLL. I am only able to view the source code and use the DLL but not changing & recompiling it. The situation now is that, whenever the DLL encouner an error it will call exit() such as below. bool Func() { // .. do something here if (error) { exit(999); } } In my MFC appl...

Catching exception in code

I was trying this piece of code to check whether the divide by zero exception is being caught: int main(int argc, char* argv[]) { try { //Divide by zero int k = 0; int j = 8/k; } catch (...) { std::cout<<"Caught exception\n"; } return 0; } When I complied this using VC6, the catch handl...

Throw/do-not-throw an exception based on a parameter - why is this not a good idea?

I was digging around in MSDN and found this article which had one interesting bit of advice: Do not have public members that can either throw or not throw exceptions based on some option. For example: Uri ParseUri(string uriValue, bool throwOnError) Now of course I can see that in 99% of cases this would be horrible, but is its occas...

why can't I create new instance of form after calling myForm.close()?

[Edit] See this post as to why I'm declaring form elements globally. I chose to rewire my code to get rid of the global definitions. [End Edit] I'm creating a from dynamically at runtime. the form, all buttons, and the combobox are all declared globally to the parent form. dim myForm as new form I put some buttons on it and a combo ...

How to recover gracefully from TargetInvocationException in multi thread?

I got TargetInvocationException while doing long process in another thread caused by a windows control on UI thread (Progress Bar). This exception leads my app to crash (goes to main method in debugging) and could not be caught by try catch. I figured out what made this exception, and fix it (that was trying to assign “Value” property by...

Custom Exception Messages: Best practices

Wondering how much effort I should go to forcing useful debugging information when creating exception messages, or should I just trust the user to supply the right info, or defer the information gathering to an exception handler? I see a lot of people people doing their exceptions like: throw new RuntimeException('MyObject is not an ar...

How do you customize exception handling behavior in JUnit 3?

I want to implement exception checking (like in JUnit 4) using JUnit 3. For example, I would like to be able to write tests like this: public void testMyExceptionThrown() throws Exception { shouldThrow(MyException.class); doSomethingThatMightThrowMyException(); } This should succeed if and only if a MyException is thrown. Th...

Dealing with constraint violations from the DB in NHibernate

I have what I think is a rather common use case for NHibernate. I create an entity and try to save it using ISession.Save() followed by a Transaction.Commit(). At this point, I expect violations of things like unique/primary key constraints to come up to me as exceptions. What I'm seeing, however, is just a GenericADOException. This does...

Strange "IOException was unhandled"

Hi, (VB.NET, .NET 3.5) I wrote the following function to read some text from txt file. It was working fine but now it's not. It keeps giving me this error message "IOException was unhandled" and " The process cannot access the file 'F:\kh_matt\ch1.txt' because it is being used by another process." The ch1.txt is not even opened or...

Why do I sometimes get "Could not load file or assembly AjaxControlToolkit version 3.0"?

We recently published a new version of an asp.net website where we changed from AjaxControlToolkit version 3.0 to 3.5. I checked the web site and found that it was running alright. The following day I recieved the following two similar exception reports, that seems to relate to the change in version: Could not load file or assembly...

What exception to throw from a property setter?

I have a string property that has a maximum length requirement because the data is linked to a database. What exception should I throw if the caller tries to set a string exceeding this length? For example, this C# code: public string MyProperty { get { return _MyBackingField; } set { if (value.Lengt...

WCF Protocol Exception

We are getting some weird behavior from a service we have deployed on a remote system which we are using to access and post data to. Retrieving information from the service doesn't seem to be an issue. However, whenever we try to execute insert methods on the service we get an System.ServiceModel.ProtocolException. The weird thing is, ...

Mailing Exception logs in a live Grails webapp

I'd like my Grails web-app to send an e-mail for each exception that reaches the end-user. Basically I'm looking for a elegant way to achieve something equivalent to: try { // ... all logic/db-access/etc required to render the page is executed here ... } catch (Exception e) { sendmail("[email protected]", "An exce...

How do I create a detailed error page?

I want to create a custom error page to handle all unhandled exceptions. I want to redirect to it from the Application_Error(object sender, EventArgs e) method located in Global.asax.cs. How can I show in it some detailed information from the exception which throws the application error? ...

What's the best way to handle exceptions over the lifetime of your code?

When I'm writing a function in a utility module to be used again, I tend to leave lots of comments at the top of functions and some simple input checks to throw up a message in the debugger if a function is used inappropriately, w/o simply using a throw command. What's the best methodology of handling such situations? What functionalit...

ExpectedException not catching exception, but I can catch it with try catch

Any ideas on this one? I'm trying to write a unit test that will delete an item and confirm that item is no longer in a repository by trying to retrieve the item by its ID which should throw a DataAccessException. However, the test keeps failing. I added a try catch block and sure enough I caught the exception I was expecting. I'm using ...

After hitting expectedexception test ends

I'm testing the delete method of an abstract base repository class which is inherited by several other repository classes. MyTestRepository inherits the base so I can run tests against the base's methods without restricting my test to using a concrete class. When I run my unit test it passes, but I noticed afterwards I have several Order...

IndexOutOfRangeException on accessing attributes through String array?

I have a treeView displayed on a winform.Now when i click on an xmlnode in the treeview its attributes get displayed in the listbox.Now i have divided the whole logic in UI and back end part. Now what i want that my back end class contains the method to display atrributes(name and value) of the xml node clicked and these are stored in an...

Is there a reason to NOT use .NET Framework-defined exception classes in your own code?

So, we are using the Enterprise Library 4.1 Exception Handling Application Block to deal with logging/handling our exceptions in a multiple-project application. We have a few custom exceptions and are throwing some exceptions whose classes are defined in the .NET framework's standard class libraries (e.g. ArgumentException, InvalidOperat...

Is it ok to rely on a try-catch in a CreateOrUpdate method for the Entity Framework?

Is it acceptable to do this? First try to the add the entity. If the add fails, it doesn't matter because that means the entity already exists? Or is there a more elegant/easy solution? EntityFrameworkEntities dal = EntityDataModelHelper.GetEntityDataModel(); try { dal.AddToXXXXXX(xxxxxxx); } catch { } try { dal.SaveChange...