C# try/catch help
- Tanis Half Elf
- Is Kind Of A Big Deal
- Posts:944
- Joined:Tue Jul 03, 2007 11:00 pm
- Location:Greensburg,La
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.
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.
- Tanis Half Elf
- Is Kind Of A Big Deal
- Posts:944
- Joined:Tue Jul 03, 2007 11:00 pm
- Location:Greensburg,La
- Tanis Half Elf
- Is Kind Of A Big Deal
- Posts:944
- Joined:Tue Jul 03, 2007 11:00 pm
- Location:Greensburg,La
- Elric of the void
- Site Admin
- Posts:774
- Joined:Mon Jul 02, 2007 11:21 pm
- Tanis Half Elf
- Is Kind Of A Big Deal
- Posts:944
- Joined:Tue Jul 03, 2007 11:00 pm
- Location:Greensburg,La
- Elric of the void
- Site Admin
- Posts:774
- Joined:Mon Jul 02, 2007 11:21 pm
- dun dun dun... chips
- Papes
- Posts:3287
- Joined:Sat Oct 11, 2008 2:08 pm
- Location:WOOOOOO
- Contact:
- Tanis Half Elf
- Is Kind Of A Big Deal
- Posts:944
- Joined:Tue Jul 03, 2007 11:00 pm
- Location:Greensburg,La
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.
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.
- dun dun dun... chips
- Papes
- Posts:3287
- Joined:Sat Oct 11, 2008 2:08 pm
- Location:WOOOOOO
- Contact:
- Tanis Half Elf
- Is Kind Of A Big Deal
- Posts:944
- Joined:Tue Jul 03, 2007 11:00 pm
- Location:Greensburg,La
- dun dun dun... chips
- Papes
- Posts:3287
- Joined:Sat Oct 11, 2008 2:08 pm
- Location:WOOOOOO
- Contact:
- Tanis Half Elf
- Is Kind Of A Big Deal
- Posts:944
- Joined:Tue Jul 03, 2007 11:00 pm
- Location:Greensburg,La
- dun dun dun... chips
- Papes
- Posts:3287
- Joined:Sat Oct 11, 2008 2:08 pm
- Location:WOOOOOO
- Contact:
- Tanis Half Elf
- Is Kind Of A Big Deal
- Posts:944
- Joined:Tue Jul 03, 2007 11:00 pm
- Location:Greensburg,La
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.
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.
Who is online
Users browsing this forum: No registered users and 5 guests