error handling
-
In a C#.net 2010 application that I am suppose to maintain (since I am new to working with this language), I find code that looks like the following: [^]try { //do something} catch (ex){} [^] In the visual studio ide, I see a a curly green under the ex. I also do not see any using statement that I would think applies. Thus can you tell me if this kind of code really catches any errors? if so can you explain it to me? I put this kind of code around some of my new code and everything falls into the catch block. Thus can you tell me what a "good" try catch block should look like aand what namespace should be included in the using statement?
-
In a C#.net 2010 application that I am suppose to maintain (since I am new to working with this language), I find code that looks like the following: [^]try { //do something} catch (ex){} [^] In the visual studio ide, I see a a curly green under the ex. I also do not see any using statement that I would think applies. Thus can you tell me if this kind of code really catches any errors? if so can you explain it to me? I put this kind of code around some of my new code and everything falls into the catch block. Thus can you tell me what a "good" try catch block should look like aand what namespace should be included in the using statement?
0. your example is pretty bad, e.g. it is missing the type of the exception you're catching. 1. the green curly indicates the variable "ex" isn't used anywhere. If you aren't going to use it, leave out the variable's name, so write:
...
catch(FileNotFoundException) {
log("Saving the customer data failed because the necessary file wasn't there");
}2. a good try-catch construct would have one or more regular statements in the try-block, and at least one in the catch-block. 3. an empty catch block most often is very bad (it "swallows" the error information without leaving a trace). One should only catch things one can deal with, and it is always wise to log the exception. 4. There is no connection between
try-catch
andusing
. 5. And of course the C# reference material has told you you can have 0 or more catch-blocks and 0 or 1 finally-block; all with good examples in the doc. :)Luc Pattyn [My Articles] Nil Volentibus Arduum
-
In a C#.net 2010 application that I am suppose to maintain (since I am new to working with this language), I find code that looks like the following: [^]try { //do something} catch (ex){} [^] In the visual studio ide, I see a a curly green under the ex. I also do not see any using statement that I would think applies. Thus can you tell me if this kind of code really catches any errors? if so can you explain it to me? I put this kind of code around some of my new code and everything falls into the catch block. Thus can you tell me what a "good" try catch block should look like aand what namespace should be included in the using statement?
dcof wrote:
catch (ex){}
You have to specify wich type of exception may handle by this try-catch. List of Exception
catch (Exception ex){}
dcof wrote:
curly green under the ex.
Curly green indicate that variable is declared but not used inside catch block.So throw exception.
catch (Exception ex)
{
throw ex;
} -
0. your example is pretty bad, e.g. it is missing the type of the exception you're catching. 1. the green curly indicates the variable "ex" isn't used anywhere. If you aren't going to use it, leave out the variable's name, so write:
...
catch(FileNotFoundException) {
log("Saving the customer data failed because the necessary file wasn't there");
}2. a good try-catch construct would have one or more regular statements in the try-block, and at least one in the catch-block. 3. an empty catch block most often is very bad (it "swallows" the error information without leaving a trace). One should only catch things one can deal with, and it is always wise to log the exception. 4. There is no connection between
try-catch
andusing
. 5. And of course the C# reference material has told you you can have 0 or more catch-blocks and 0 or 1 finally-block; all with good examples in the doc. :)Luc Pattyn [My Articles] Nil Volentibus Arduum
Luc Pattyn wrote:
And of course the C# reference material has told you
But of course... :laugh:
When I was a coder, we worked on algorithms. Today, we memorize APIs for countless libraries — those libraries have the algorithms - Eric Allman
-
dcof wrote:
catch (ex){}
You have to specify wich type of exception may handle by this try-catch. List of Exception
catch (Exception ex){}
dcof wrote:
curly green under the ex.
Curly green indicate that variable is declared but not used inside catch block.So throw exception.
catch (Exception ex)
{
throw ex;
}Your advice to just throw the exception isn't the best thing you can do. You should never catch an exception and rethrow it without doing something else with the exception. The reason for this is because all you have done is recreate what would happen if you didn't have any exception handling at all.
Forgive your enemies - it messes with their heads
"Mind bleach! Send me mind bleach!" - Nagy Vilmos
My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility
-
Your advice to just throw the exception isn't the best thing you can do. You should never catch an exception and rethrow it without doing something else with the exception. The reason for this is because all you have done is recreate what would happen if you didn't have any exception handling at all.
Forgive your enemies - it messes with their heads
"Mind bleach! Send me mind bleach!" - Nagy Vilmos
My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility
-
In a C#.net 2010 application that I am suppose to maintain (since I am new to working with this language), I find code that looks like the following: [^]try { //do something} catch (ex){} [^] In the visual studio ide, I see a a curly green under the ex. I also do not see any using statement that I would think applies. Thus can you tell me if this kind of code really catches any errors? if so can you explain it to me? I put this kind of code around some of my new code and everything falls into the catch block. Thus can you tell me what a "good" try catch block should look like aand what namespace should be included in the using statement?
Luc gave you a very good answer. Here's a quick simple example, but try the MSDN site or maybe the articles here on CP for proper exception handling.
try{
// code here
}
catch(Exception ex){
//Note that ex, in contrary of your example is initialized as the Exception type.
//Your code would work in javascript for example, but in C# you need to always define the type of a variable
// the very least you need to do is LOG the error somewhere. Note that Exception is a general class,
//you can also catch specific Exception like IndexOutOfBoundsException, OutOfMemoryException, ...
//You can use multiple catch blocks if you wich and handle logging, user information, ... accordingly
//But I believe that if you catch you should at the very least catch the Exception
}
finally{
//Mostly the finally block is used the handle things that must be done even if the catch block was called.
//eg. A rollback against the database, close a database connection or close a file handle.
}tip: Do not show technical Exception messages to the user. Instead, depending on the catch, form a nice message explaining what happened and if they need to take action. Hope this helps.
V.
-
0. your example is pretty bad, e.g. it is missing the type of the exception you're catching. 1. the green curly indicates the variable "ex" isn't used anywhere. If you aren't going to use it, leave out the variable's name, so write:
...
catch(FileNotFoundException) {
log("Saving the customer data failed because the necessary file wasn't there");
}2. a good try-catch construct would have one or more regular statements in the try-block, and at least one in the catch-block. 3. an empty catch block most often is very bad (it "swallows" the error information without leaving a trace). One should only catch things one can deal with, and it is always wise to log the exception. 4. There is no connection between
try-catch
andusing
. 5. And of course the C# reference material has told you you can have 0 or more catch-blocks and 0 or 1 finally-block; all with good examples in the doc. :)Luc Pattyn [My Articles] Nil Volentibus Arduum
-
When I was talking about the using statement, I was wanting to know what namespace should be used.
"Exception" is part of the System namespace. (ie. System.Exception)
I wasn't, now I am, then I won't be anymore.
-
When I was talking about the using statement, I was wanting to know what namespace should be used.
Assuming you have a typical set of usings (Visual always gives you using.System to start with), there is nothing you have to add. :)
Luc Pattyn [My Articles] Nil Volentibus Arduum