New Posts  All Forums:Forum Nav:

C# try catch question

post #1 of 10
Thread Starter 
What up my OC programming peeps, I was just wondering what everyone's thoughts are( everyone = you have actually made a working C# WindowsWebsForm(Windows Application) about the try/ catch statement for error handling on users input.
I am about to write a couple network programs for work and plan on using try/ catch statements for error handling. From what i've read, as long as the program isnt big, ( big meaning big program using a lot of try/catch statements for handling, OR meaning a lot of traffic, in this particular situation atleast) then using try/catch for error handling would not degragate program performance.
Once again, just wanted others opinions as it sucks that I have no real programming pals, OC is all i got haha. And insight would be appreciated and thanks for reading.
Goliath
(13 items)
 
  
CPUMotherboardGraphicsRAM
Q6600 @ 3.0 Ghz EVGA 750I SLI EVGA GTX 260 Corsair Dominator 4GB @ 1066 
Hard DriveOSMonitorKeyboard
2X-W.D. 1 TB Windows 7 Ultimate x64 Asus 21 inch 1080 p Pro 2000 Wireless Keyboard 
PowerCaseMouse
OCZ 700W GamXtreme CM Storm Scout Logitech G500 
  hide details  
Reply
Goliath
(13 items)
 
  
CPUMotherboardGraphicsRAM
Q6600 @ 3.0 Ghz EVGA 750I SLI EVGA GTX 260 Corsair Dominator 4GB @ 1066 
Hard DriveOSMonitorKeyboard
2X-W.D. 1 TB Windows 7 Ultimate x64 Asus 21 inch 1080 p Pro 2000 Wireless Keyboard 
PowerCaseMouse
OCZ 700W GamXtreme CM Storm Scout Logitech G500 
  hide details  
Reply
post #2 of 10
You mean as opposed to the other exception handling techniques
confused.gif

Your user input should not be throwing any exceptions that you need to catch.

To be more specific you should write code that checks the values and validates them, exceptions are just that, exceptions and unforseen/unavoidable occasional circumstances.
Edited by mykah89 - 4/11/11 at 6:13pm
My System
(13 items)
 
  
CPUMotherboardGraphicsRAM
I5 760 GA-H55N GTX 470 PNY Optima 8GB 
Hard DriveMonitorPowerCase
Spinpoint F4 LG227WTG sg07 PSU Sugo sg07 
Mouse
CM Sentinel 
  hide details  
Reply
My System
(13 items)
 
  
CPUMotherboardGraphicsRAM
I5 760 GA-H55N GTX 470 PNY Optima 8GB 
Hard DriveMonitorPowerCase
Spinpoint F4 LG227WTG sg07 PSU Sugo sg07 
Mouse
CM Sentinel 
  hide details  
Reply
post #3 of 10
Thread Starter 
theres try catch and finally, and try/catch/finally/else. No one way of doing something in programming, but i digress lol Tell me how you would solve and error if a user put in a letter instead of a number in a text box that was only for ints.
Goliath
(13 items)
 
  
CPUMotherboardGraphicsRAM
Q6600 @ 3.0 Ghz EVGA 750I SLI EVGA GTX 260 Corsair Dominator 4GB @ 1066 
Hard DriveOSMonitorKeyboard
2X-W.D. 1 TB Windows 7 Ultimate x64 Asus 21 inch 1080 p Pro 2000 Wireless Keyboard 
PowerCaseMouse
OCZ 700W GamXtreme CM Storm Scout Logitech G500 
  hide details  
Reply
Goliath
(13 items)
 
  
CPUMotherboardGraphicsRAM
Q6600 @ 3.0 Ghz EVGA 750I SLI EVGA GTX 260 Corsair Dominator 4GB @ 1066 
Hard DriveOSMonitorKeyboard
2X-W.D. 1 TB Windows 7 Ultimate x64 Asus 21 inch 1080 p Pro 2000 Wireless Keyboard 
PowerCaseMouse
OCZ 700W GamXtreme CM Storm Scout Logitech G500 
  hide details  
Reply
post #4 of 10
Thread Starter 
I totally disagree on what you said about exceptions. They are avoidable. By testing your program before releasing it you are essentially getting all the bugs out. Bear in mind that I am not writing a big network program. Just under 500 lines i'd say. So its not too complex.
Goliath
(13 items)
 
  
CPUMotherboardGraphicsRAM
Q6600 @ 3.0 Ghz EVGA 750I SLI EVGA GTX 260 Corsair Dominator 4GB @ 1066 
Hard DriveOSMonitorKeyboard
2X-W.D. 1 TB Windows 7 Ultimate x64 Asus 21 inch 1080 p Pro 2000 Wireless Keyboard 
PowerCaseMouse
OCZ 700W GamXtreme CM Storm Scout Logitech G500 
  hide details  
Reply
Goliath
(13 items)
 
  
CPUMotherboardGraphicsRAM
Q6600 @ 3.0 Ghz EVGA 750I SLI EVGA GTX 260 Corsair Dominator 4GB @ 1066 
Hard DriveOSMonitorKeyboard
2X-W.D. 1 TB Windows 7 Ultimate x64 Asus 21 inch 1080 p Pro 2000 Wireless Keyboard 
PowerCaseMouse
OCZ 700W GamXtreme CM Storm Scout Logitech G500 
  hide details  
Reply
post #5 of 10
Code:

private static Boolean CheckIfNumeric(string chkNumeric)
        {
            int intOutVal;
            Boolean isNumeric = false;
            isNumeric = int.TryParse(chkNumeric, out intOutVal);
            return isNumeric;
        }

My System
(13 items)
 
  
CPUMotherboardGraphicsRAM
I5 760 GA-H55N GTX 470 PNY Optima 8GB 
Hard DriveMonitorPowerCase
Spinpoint F4 LG227WTG sg07 PSU Sugo sg07 
Mouse
CM Sentinel 
  hide details  
Reply
My System
(13 items)
 
  
CPUMotherboardGraphicsRAM
I5 760 GA-H55N GTX 470 PNY Optima 8GB 
Hard DriveMonitorPowerCase
Spinpoint F4 LG227WTG sg07 PSU Sugo sg07 
Mouse
CM Sentinel 
  hide details  
Reply
post #6 of 10
Thread Starter 
Finally, somebody that knows their **** haha tooshay mykah. But what does "out intOutVal" do?
Goliath
(13 items)
 
  
CPUMotherboardGraphicsRAM
Q6600 @ 3.0 Ghz EVGA 750I SLI EVGA GTX 260 Corsair Dominator 4GB @ 1066 
Hard DriveOSMonitorKeyboard
2X-W.D. 1 TB Windows 7 Ultimate x64 Asus 21 inch 1080 p Pro 2000 Wireless Keyboard 
PowerCaseMouse
OCZ 700W GamXtreme CM Storm Scout Logitech G500 
  hide details  
Reply
Goliath
(13 items)
 
  
CPUMotherboardGraphicsRAM
Q6600 @ 3.0 Ghz EVGA 750I SLI EVGA GTX 260 Corsair Dominator 4GB @ 1066 
Hard DriveOSMonitorKeyboard
2X-W.D. 1 TB Windows 7 Ultimate x64 Asus 21 inch 1080 p Pro 2000 Wireless Keyboard 
PowerCaseMouse
OCZ 700W GamXtreme CM Storm Scout Logitech G500 
  hide details  
Reply
post #7 of 10
I by no means claim to know a lot, which is the problem here. I suggest some more reading on your part if you are unfamiliar with out parameters.

Simply put an out parameter is a parameter that is expected to be defined within the method being called.

Exceptions are just that, exceptions. They are to be thrown in exceptional situations and are mainly used to close streams/database connections and prevent corruption of the programs state such as in the case of an overflow exception. I find the best exception handling method is prevention, your right exceptions are unavoidable.. but you shouldn't write code you expect to fail.
My System
(13 items)
 
  
CPUMotherboardGraphicsRAM
I5 760 GA-H55N GTX 470 PNY Optima 8GB 
Hard DriveMonitorPowerCase
Spinpoint F4 LG227WTG sg07 PSU Sugo sg07 
Mouse
CM Sentinel 
  hide details  
Reply
My System
(13 items)
 
  
CPUMotherboardGraphicsRAM
I5 760 GA-H55N GTX 470 PNY Optima 8GB 
Hard DriveMonitorPowerCase
Spinpoint F4 LG227WTG sg07 PSU Sugo sg07 
Mouse
CM Sentinel 
  hide details  
Reply
post #8 of 10
Thread Starter 
I agree, but I dont write code to fail, I expect the user to mess up. Because 98% of the time the problem lies within the user. But you have opened up another perspective in my eyes. Programming isnt even my main career but I like to program, and have had a couple years experience with college classes.Something relaxin about it. It's like reading a book. But you def. gave me another dimension to my skills so i appreciate the chit chat and just gave you rep.
Goliath
(13 items)
 
  
CPUMotherboardGraphicsRAM
Q6600 @ 3.0 Ghz EVGA 750I SLI EVGA GTX 260 Corsair Dominator 4GB @ 1066 
Hard DriveOSMonitorKeyboard
2X-W.D. 1 TB Windows 7 Ultimate x64 Asus 21 inch 1080 p Pro 2000 Wireless Keyboard 
PowerCaseMouse
OCZ 700W GamXtreme CM Storm Scout Logitech G500 
  hide details  
Reply
Goliath
(13 items)
 
  
CPUMotherboardGraphicsRAM
Q6600 @ 3.0 Ghz EVGA 750I SLI EVGA GTX 260 Corsair Dominator 4GB @ 1066 
Hard DriveOSMonitorKeyboard
2X-W.D. 1 TB Windows 7 Ultimate x64 Asus 21 inch 1080 p Pro 2000 Wireless Keyboard 
PowerCaseMouse
OCZ 700W GamXtreme CM Storm Scout Logitech G500 
  hide details  
Reply
post #9 of 10
Quote:
Originally Posted by hartleaa;13091017 
Finally, somebody that knows their **** haha tooshay mykah. But what does "out intOutVal" do?

The 'out' keyword means that the function is required to do work with that variable. If you're familiar with C++ it is similar to passing a variable into a function by reference, the difference here is that C# will enforce that the variable is accessed within the function in some way.
Code:
private static Boolean CheckIfNumeric(string chkNumeric)
{
    int intOutVal;
    Boolean isNumeric = false;
    isNumeric = int.TryParse(chkNumeric, out intOutVal);
    return isNumeric;
}

In the case above he's not actually accessing the value in intOutVal. The code could be changed to this:
Code:
private static bool CheckIfNumeric(string chkNumeric, out int intOutVal)
{
    return int.TryParse(chkNumeric, out intOutVal);
}

// Example of it in use
int returnVal;
string testNum = "25";
bool isANum = CheckIfNumeric(testNum, out returnVal);

Of course, at this point there's little use for a CheckIfNumeric if it has the same parameters as and output as the TryParse function itself. The original CheckIfNumeric makes more sense because it's sole purpose was to simply say whether or not a string represents a number.
Foldatron
(17 items)
 
Mat
(10 items)
 
Work iMac
(9 items)
 
CPUMotherboardGraphicsGraphics
i7 950 EVGA x58 3-way SLI EVGA GTX 660ti GTX 275 
RAMHard DriveHard DriveHard Drive
3x2GB Corsair Dominator DDR3-1600 80GB Intel X25-M SSD 2TB WD Black 150GB WD Raptor 
Hard DriveOSMonitorKeyboard
2x 150GB WD V-raptor in RAID0 Win7 Home 64-bit OEM 55" LED 120hz 1080p Vizio MS Natural Ergonomic Keyboard 4000 
PowerCase
750W PC P&C Silencer CoolerMaster 690 
CPUGraphicsRAMHard Drive
Intel Core i5 2500S AMD 6770M 8GB (2x4GB) at 1333Mhz 1TB, 7200 rpm 
Optical DriveOSMonitorKeyboard
LG 8X Dual-Layer "SuperDrive" OS X Lion 27" iMac screen Mac wireless keyboard 
Mouse
Mac wireless mouse 
CPUGraphicsRAMHard Drive
i7-2600K AMD 6970M 1GB 16GB PC3-10600 DDR3 1TB 7200rpm 
Hard DriveOptical DriveOSMonitor
256GB SSD 8x DL "SuperDrive" OS X 10.7 Lion 27" 2560x1440 iMac display 
Monitor
27" Apple thunderbolt display 
  hide details  
Reply
Foldatron
(17 items)
 
Mat
(10 items)
 
Work iMac
(9 items)
 
CPUMotherboardGraphicsGraphics
i7 950 EVGA x58 3-way SLI EVGA GTX 660ti GTX 275 
RAMHard DriveHard DriveHard Drive
3x2GB Corsair Dominator DDR3-1600 80GB Intel X25-M SSD 2TB WD Black 150GB WD Raptor 
Hard DriveOSMonitorKeyboard
2x 150GB WD V-raptor in RAID0 Win7 Home 64-bit OEM 55" LED 120hz 1080p Vizio MS Natural Ergonomic Keyboard 4000 
PowerCase
750W PC P&C Silencer CoolerMaster 690 
CPUGraphicsRAMHard Drive
Intel Core i5 2500S AMD 6770M 8GB (2x4GB) at 1333Mhz 1TB, 7200 rpm 
Optical DriveOSMonitorKeyboard
LG 8X Dual-Layer "SuperDrive" OS X Lion 27" iMac screen Mac wireless keyboard 
Mouse
Mac wireless mouse 
CPUGraphicsRAMHard Drive
i7-2600K AMD 6970M 1GB 16GB PC3-10600 DDR3 1TB 7200rpm 
Hard DriveOptical DriveOSMonitor
256GB SSD 8x DL "SuperDrive" OS X 10.7 Lion 27" 2560x1440 iMac display 
Monitor
27" Apple thunderbolt display 
  hide details  
Reply
post #10 of 10
Quote:
Originally Posted by hartleaa;13090918 
I totally disagree on what you said about exceptions...

He's actually 100% correct. You need to have the error-checking occur at the input level, not the output level.

"Exceptions" (ones you would be likely to "catch" in C#) are internal errors during processing, past the input. Your code should not be "catching" errors when users enter letters instead of numbers. Your code should prevent those letters from even being executed. An example of an "exception" would be an "incorrect password," or a "network failure" (if your end-user was offline during a net request, for instance). The name of the game here is prevention. Your program needs to be idiot-proof, and that responsibility lies on the programmer. Just part of the gig.
Edited by Plex - 4/12/11 at 5:28am
Intellect v2
(9 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i7-6700K Processor ASUS ROG MAXIMUS VIII HERO LGA1151 DDR4 M.2 SAT... EVGA GTX 1080 SC ACX 3.0 Crucial Ballistix Sport 32GB DDR4 2400 MT/s (PC... 
CoolingKeyboardPowerCase
Noctua NH-D15 Das Keyboard 4 Professional (Brown) Corsair AX860 Fractal Design Define R5 
Mouse
MIONIX NAOS 7000 
  hide details  
Reply
Intellect v2
(9 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i7-6700K Processor ASUS ROG MAXIMUS VIII HERO LGA1151 DDR4 M.2 SAT... EVGA GTX 1080 SC ACX 3.0 Crucial Ballistix Sport 32GB DDR4 2400 MT/s (PC... 
CoolingKeyboardPowerCase
Noctua NH-D15 Das Keyboard 4 Professional (Brown) Corsair AX860 Fractal Design Define R5 
Mouse
MIONIX NAOS 7000 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Application Programming