Page 1 of 2

C# try/catch help

Posted: Wed Apr 22, 2009 8:07 pm
by Tanis Half Elf
Not sure if any of you are familiar with C#, but here is my problem

The program I made will run fine because I am using a try/catch statement when debugging. When I am not in debug mode or after the program is installed, the try/catch statement will not work. Is there anything similar to this in C# that will work when not in debug mode?


Additional Details
Here is the method I am using it in:

public void printBridge(string chosenPrinter)
{
try
{
printPreviewDialog1.ShowDialog();
}
catch
{
MessageBox.Show("Unable to set a default printer. The printer may not exist or the WScript.Network com object may have a problem.");
}

}



If the printer isn't online and it tries to run this in debug or release mode the exception will be caught and the message box will appear. If I start the program without debugging or try to run the program after I installed it. The program will crash due to the exception not being caught.

Posted: Thu Apr 23, 2009 12:41 pm
by Tanis Half Elf
I figured it out.

Posted: Thu Apr 23, 2009 12:42 pm
by Darkfoxx
Sorry no one helped, but I'm not a programmer.

Posted: Thu Apr 23, 2009 11:02 pm
by Tanis Half Elf
Yea, i didnt think there were too many programmers left in the forums. But i was using every resource i had.

Posted: Fri Apr 24, 2009 12:29 am
by Elric of the void
I did have one of my buddies look at it, you posted you found a solution before a managed to post it >.< , next time ill post it faster :)

Posted: Fri Apr 24, 2009 11:57 pm
by Tanis Half Elf
Well, i thought i found a solution, but it worked once, and not anymore. I think it was coincidence, it is a mystery. If that other solution gets posted that would be awesome! Thanks for having one of your buddies look at the code.

Posted: Sat Apr 25, 2009 10:31 am
by Elric of the void
he solution was to avoid doing this, he said that this action should already be automated and that this would, if anything cause conflicts.

Posted: Sat Apr 25, 2009 2:33 pm
by dun dun dun... chips
what are you using try/catch for?

edit: why are you using it for debugs?

Posted: Sat Apr 25, 2009 2:58 pm
by Tanis Half Elf
The try/catch is used to catch the exception that is thrown when the printer is offline when trying to print to it.. From what I understand this only happens with some printer drivers. When the program catches the exception it puts up an error message telling the user to select a different printer.

If there is no try catch the exception will go through and crash the program. I am giving a presentation showing off our program in 2 weeks, in front of a lot of faculty and professionals in the field. If they find a bug to crash our program we will get grilled.

Posted: Sat Apr 25, 2009 3:24 pm
by dun dun dun... chips
whats not working with it?
is there a compiler error, or is it just not doing anything?

Posted: Sat Apr 25, 2009 9:49 pm
by Tanis Half Elf
Yea, it wasnt doing anything. Like it was not catching the exception. It would only catch the exception in debug mode or release mode, but not in start without debugging or when I installed the program onto my computer.

Posted: Sat Apr 25, 2009 10:18 pm
by dun dun dun... chips
when you say its not doing anything, is the exception supposed to be thrown and its not catching it, or is there no exception being thrown?

Posted: Sun Apr 26, 2009 1:43 pm
by Tanis Half Elf
There is an exception being thrown, but it isn't being caught; therefore, crashing my program.

Posted: Sun Apr 26, 2009 6:43 pm
by dun dun dun... chips
hmm... interesting.
im not good with c#, ive never decided to sit down and learn a language only for windows... :|
however, i know that with java, c, and obj-c, in the catch bit, you need to have what it is catching in parentheses or something like that.
maybe its different in c#

Posted: Sun Apr 26, 2009 10:52 pm
by Tanis Half Elf
Yea, you dont need anything in parenthesis, but it is recommended. I went about this another way.
I decided to read whether the selected printer is offline or online. eg)connected or not connected.
I actually started off trying to do this and could never figure this out. I've spent hours researching
this in the past with no luck, but I got lucky and decided to try it a different way from the general public.
It worked, so I can now tell if the printer is online or not. If so, i will have it print. If not i will throw up an
error message to the user. Funny thing is now the program wont print period! Kinda, weird since i
didnt even mess with that block of code. I think it may be my hp printer, so i am getting a group member to test it.

Ohh, and regarding languages, all the .Net language are growing very fast. Especially C# and VB in the south.