C# try/catch help

Need help with your code? Post it here and we'll try our best to help.
User avatar
Tanis Half Elf
Is Kind Of A Big Deal
Posts:944
Joined:Tue Jul 03, 2007 11:00 pm
Location:Greensburg,La
C# try/catch help

Postby Tanis Half Elf » Wed Apr 22, 2009 8:07 pm

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.

User avatar
Tanis Half Elf
Is Kind Of A Big Deal
Posts:944
Joined:Tue Jul 03, 2007 11:00 pm
Location:Greensburg,La

Postby Tanis Half Elf » Thu Apr 23, 2009 12:41 pm

I figured it out.

User avatar
Darkfoxx
Site Admin
Posts:9666
Joined:Mon Jul 02, 2007 7:09 pm
Location:Alexandria, VA
Contact:

Postby Darkfoxx » Thu Apr 23, 2009 12:42 pm

Sorry no one helped, but I'm not a programmer.
Gaming:

Workstation:

User avatar
Tanis Half Elf
Is Kind Of A Big Deal
Posts:944
Joined:Tue Jul 03, 2007 11:00 pm
Location:Greensburg,La

Postby Tanis Half Elf » Thu Apr 23, 2009 11:02 pm

Yea, i didnt think there were too many programmers left in the forums. But i was using every resource i had.

User avatar
Elric of the void
Site Admin
Posts:774
Joined:Mon Jul 02, 2007 11:21 pm

Postby Elric of the void » Fri Apr 24, 2009 12:29 am

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 :)


User avatar
Tanis Half Elf
Is Kind Of A Big Deal
Posts:944
Joined:Tue Jul 03, 2007 11:00 pm
Location:Greensburg,La

Postby Tanis Half Elf » Fri Apr 24, 2009 11:57 pm

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.

User avatar
Elric of the void
Site Admin
Posts:774
Joined:Mon Jul 02, 2007 11:21 pm

Postby Elric of the void » Sat Apr 25, 2009 10:31 am

he solution was to avoid doing this, he said that this action should already be automated and that this would, if anything cause conflicts.


User avatar
dun dun dun... chips
Papes
Posts:3287
Joined:Sat Oct 11, 2008 2:08 pm
Location:WOOOOOO
Contact:

Postby dun dun dun... chips » Sat Apr 25, 2009 2:33 pm

what are you using try/catch for?

edit: why are you using it for debugs?

User avatar
Tanis Half Elf
Is Kind Of A Big Deal
Posts:944
Joined:Tue Jul 03, 2007 11:00 pm
Location:Greensburg,La

Postby Tanis Half Elf » Sat Apr 25, 2009 2:58 pm

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.

User avatar
dun dun dun... chips
Papes
Posts:3287
Joined:Sat Oct 11, 2008 2:08 pm
Location:WOOOOOO
Contact:

Postby dun dun dun... chips » Sat Apr 25, 2009 3:24 pm

whats not working with it?
is there a compiler error, or is it just not doing anything?

User avatar
Tanis Half Elf
Is Kind Of A Big Deal
Posts:944
Joined:Tue Jul 03, 2007 11:00 pm
Location:Greensburg,La

Postby Tanis Half Elf » Sat Apr 25, 2009 9:49 pm

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.

User avatar
dun dun dun... chips
Papes
Posts:3287
Joined:Sat Oct 11, 2008 2:08 pm
Location:WOOOOOO
Contact:

Postby dun dun dun... chips » Sat Apr 25, 2009 10:18 pm

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?

User avatar
Tanis Half Elf
Is Kind Of A Big Deal
Posts:944
Joined:Tue Jul 03, 2007 11:00 pm
Location:Greensburg,La

Postby Tanis Half Elf » Sun Apr 26, 2009 1:43 pm

There is an exception being thrown, but it isn't being caught; therefore, crashing my program.

User avatar
dun dun dun... chips
Papes
Posts:3287
Joined:Sat Oct 11, 2008 2:08 pm
Location:WOOOOOO
Contact:

Postby dun dun dun... chips » Sun Apr 26, 2009 6:43 pm

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#

User avatar
Tanis Half Elf
Is Kind Of A Big Deal
Posts:944
Joined:Tue Jul 03, 2007 11:00 pm
Location:Greensburg,La

Postby Tanis Half Elf » Sun Apr 26, 2009 10:52 pm

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.


Return to “Code Monkeys”

Who is online

Users browsing this forum: No registered users and 8 guests