Guest Fhwoarang Posted September 3, 2008 Posted September 3, 2008 Hi, I'm a VB6 regular programmer and I'm trying to move to .NET. My transition has been soft and easy because of the good habits I learn from people in forum like this one. Still there is something I don't understand about the Try and Catch thing. 1. How do you know if something (function or method) has something that we can catch? I mean, I can look at the MSDN for everyline I writte, but that wouldn't be very practical. Do all methods and properties have an exception? 2. When or why do you use the "e" as a parameter in a catch? Sometimes I see things like this: catch (RandomException e) { // Code } I know the "e" is a value that comes from mostly from events (click, open, load, etc.), but why sometimes you use it in a catch? 3. How often do I need to use the Try Catch? Since in VB6 you don't have this, I RARELY use Try and Catch. Maybe only when I debbug and the compiler says "unhanheld exception". I hope you could answer my question, since I think this is essential to know. Cheers everybody.:D Quote
Nate Bross Posted September 3, 2008 Posted September 3, 2008 You can use try catch in the same places you would use On Error in VB6. There are many things that can cause exceptions, null objects, invalid casts, etc. try { ...code that could cause an exception... } catch(Exception ex) { // your log method LogMethod(ex); } The ex is the object that the exception we caught is stored in, it contains the exception, inner exception, stack trace, and other useful information for debugging. The bit "(Exception ex)" is really just an object decleration, the use of "e" or "ex" is up to you, you could use "myCaughtException" if you wanted. Personally, I use this on all public methods. try {} catch(Exception ex) { log(ex); throw; } I use this on private methods try {} catch { throw; } The point being that I only want to log the exception once, so I log it in the public method and I use "throw;" (i DO NOT use "throw ex;") to bubble up the exception from all private methods to the public method that called it, where it is logged and then bubbled up to the presentation layer for the user. Notice on the private methods I don't even catch a type of exception, I just bubble it up. HTH Quote ~Nate� ___________________________________________ Please use the [vb]/[cs] tags on posted code. Please post solutions you find somewhere else. Follow me on Twitter here.
Guest Fhwoarang Posted September 6, 2008 Posted September 6, 2008 Thanks for your explanation. But what does "throw" do? What exactly is "log"? Please excuse my scrubness.:D About using the "e" thing, there is something I still don't understand. I have this code: try { FormHijoInqui f = FormHijoInqui.Instancia; FormHijoInqui.Instancia.MdiParent = this; FormHijoInqui.Instancia.Show(); } catch (ObjectDisposedException) { } This code prevents the ObjectDisposedException to ocurr. But if I add an "e" after the exception name, an error ocurs, saying that the "e" is in use already. I guess that's because of this: private void BarraHerrInquilinos_Click(object sender, EventArgs e) The code I posted initially is located on this BarraHerrInquilinos method. I tried using "ex" instead "e" and it didn't showed me the error. Now, even if I handled the exception, with the following. try { FormHijoInqui f = FormHijoInqui.Instancia; FormHijoInqui.Instancia.MdiParent = this; FormHijoInqui.Instancia.Show(); } catch (ObjectDisposedException ex) { MessageBox.Show("Object missing"); } The message box appears... but the program still crashes and Visual Studio still says that the exception has not been handled. If I delete the message box line, nothing happens. So how can I really do something about this exception or why the program stops only if I add a message box, but nothing happens if I do nothing with the exception? Quote
Administrators PlausiblyDamp Posted September 6, 2008 Administrators Posted September 6, 2008 (edited) The catch block does two things, firstly it says what kind of exception you are interested in handling and secondly assigns it to a variable. catch (ObjectDisposedException) { } would catch an exception of type ObjectDisposedException, however it wouldn't assign it to a variable. catch (ObjectDisposedException ex) { } does the same thing but assigns the Exception to a variable named ex, this means you can access information regarding the exception within the catch block e.g. catch (ObjectDisposedException ex) { MessageBox.Show(ex.Message); } Like all variable names you cannot reuse an existing name from the same scope (e causes an error because e is already in use as a parameter name). The throw keyword is how you signal your own errors (similar to err.raise in vb6) Edited July 11, 2009 by PlausiblyDamp Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Guest Fhwoarang Posted July 11, 2009 Posted July 11, 2009 I was doing last night a function that returns a bool. And I used a try and catch. But everytime I tried to run the application, the "not all code routes return a value" error kept showing up, meaning that I didn't consider all possibilities that in the code may occur (like an "else if" without an "else"). So I checked my code and it was really simple: private bool esNumero(string expresion) { try { bool cadenaValidada = true; foreach (char c in expresion) { if (char.IsLetter(c)) { cadenaValidada = false; } } return cadenaValidada; } catch (SystemException ex) { MessageBox.Show("Error al validar cadena de números: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return false; } } Despite this, the "not all code routes return a value" error kept showing up. So I added a "return false" in the "catch" segment and it worked fine. I really didn't know that the catch should return a value if the function is returning a value. Is this normal or did I do something wrong? Quote
Administrators PlausiblyDamp Posted July 11, 2009 Administrators Posted July 11, 2009 If you are catching the exception then the error has been dealt with, the calling function will not be aware an exception was thrown and caught so it will expect a return value. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Guest Fhwoarang Posted July 11, 2009 Posted July 11, 2009 If you are catching the exception then the error has been dealt with, the calling function will not be aware an exception was thrown and caught so it will expect a return value. Thanks, man. You are always a lifesaver.:D Is this same case for all kind of return types? Int, object, string, etc. Quote
Administrators PlausiblyDamp Posted July 11, 2009 Administrators Posted July 11, 2009 If a function is declared as returning a result, regardless of type (classes, arrays, collections, ints - anything at all) then all paths must return a value unless an unhandled exception occurs. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
robertsams23 Posted April 5, 2010 Posted April 5, 2010 try catch explined : http://csharp.net-informations.com/statements/csharp-exceptions.htm tks.robert Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.