![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programmer
Join Date: Oct 2005
Posts: 72
Rep Power: 4
![]() |
How to "expect an exception to occur" in the best code-style
I am just looking for a better writing method to improve myself. So, the situation is this (i'm writing a test script to catch errors, where the situation is that I am expecting an exception to occur. It must):
bool errorMissing = true;
try
{
// Some statement that MUST return an error
}
catch (Exception e)
{
errorMissing = false; //error occured. Good!
// Then I check the error messages, etc to see if correct
}
finally
{
if (errorMissing)
{
//Assert.Fail( )
}
}2. Is there a better and more elegant way in C#? |
|
|
|
|
|
#2 |
|
Newbie
Join Date: Feb 2008
Location: Toowoomba, Australia
Posts: 7
Rep Power: 0
![]() |
Re: How to "expect an exception to occur" in the best code-style
With exception handling in C# the try block is where you put the code that you expect may produce an exception.
The catch clauses are used to handle exceptions that are generated from within the try block. The catch clause section can contain multiple exception handlers. When an exception occurs the catch clauses are searched in order from first to last. As a result when writing your catch clauses you should put the most specific exception handlers first followed by the more general ones. In the example below if the specific handlers cannot handle the exception it is caught by the last handler. In this example I have caught the exception message. You can choose what to do with this message after the calling block. Whether or not an exception occurs the finally block is always run. It is usually where your clean up code goes. You can put your code to close database connections or files that you have open in the try statement. I hope this helps Regards Scottyb |
|
|
|
|
|
#3 | |
|
Programmer
Join Date: Oct 2005
Posts: 72
Rep Power: 4
![]() |
Re: How to "expect an exception to occur" in the best code-style
Quote:
But my real intention is the see whether my command returns an exception or not? Like i said, i am writing a test function, and that particular command must return an exception as it is no longer supported. If it did not, i render the test as "failed". My method works, but I want to learn to write elegant code. I'm trying my best. But I feel learn by example is the best. |
|
|
|
|
|
|
#4 |
|
Professional Programmer
|
Re: How to "expect an exception to occur" in the best code-style
how about throwing some code our way ?
maybe we'll have better suggestions.
__________________
Don't take life too seriously, it's not permanent ! |
|
|
|
|
|
#5 |
|
Programmer
Join Date: Nov 2007
Posts: 86
Rep Power: 2
![]() |
Re: How to "expect an exception to occur" in the best code-style
if this is a test method you should be using NUnit. are you? if so, on the test method you can place an attribute called ExpectedException. NUnit does the rest.
|
|
|
|
|
|
#6 | |
|
Programmer
Join Date: Oct 2005
Posts: 72
Rep Power: 4
![]() |
Re: How to "expect an exception to occur" in the best code-style
Quote:
Initially I thought that [ExpectedException] just makes sure if that exception occurs, the test still passed. I didn't know it does check if the exception did not occur, the test fails -- this is what I wanted !! Thanks for pointing out, else I would not have looked into the manual to check. Thanks. |
|
|
|
|
|
|
#7 | |
|
Programmer
Join Date: Oct 2005
Posts: 72
Rep Power: 4
![]() |
Re: How to "expect an exception to occur" in the best code-style
Quote:
public void testAbort()
{
bool errorMissing = true;
try
{
runner.Abort(); // This command must return expection!!
}
catch (SomeException e)
{
errorMissing = false;
Assert.AreEqual("Command obsolete", e.ErrorCode)
}
finally
{
if (errorMissing)
Assert.Fail(); // make the test fail
}
} |
|
|
|
|
|
|
#8 |
|
Professional Programmer
|
Re: How to "expect an exception to occur" in the best code-style
Well, you could use this from nunit : http://www.nunit.org/index.php?p=exception&r=2.4.3
in order to make the code smaller : csharp Syntax (Toggle Plain Text)
An improvement to that has been made in xUnit where you can do : Assert.Throws(method). Best of luck.
__________________
Don't take life too seriously, it's not permanent ! |
|
|
|
|
|
#9 |
|
Programmer
Join Date: Nov 2007
Posts: 86
Rep Power: 2
![]() |
Re: How to "expect an exception to occur" in the best code-style
beauty is in the eye of the beholder
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Incorporating javascript code into java application | csrocker101 | Java | 1 | Feb 11th, 2008 7:36 AM |
| Viewing VB's Automated code | john Wesley | Visual Basic .NET | 3 | Jun 8th, 2006 6:37 AM |
| FTP and return code fetching | Serinth | C | 2 | May 29th, 2006 12:05 AM |
| What Style Do You Program With? | Darkhack | Other Programming Languages | 1 | Feb 10th, 2006 3:07 AM |
| AhhH!!! I can't find anything on this exception... | stakeknife | ASP | 2 | Sep 26th, 2005 8:48 AM |