Red Gate forums :: View topic - Recognizing handled exceptions
Return to www.red-gate.com RSS Feed Available

Search  | Usergroups |  Profile |  Messages |  Log in  Register 
Go to product documentation
Exception Hunter 1
Exception Hunter 1 forum

Recognizing handled exceptions

Search in Exception Hunter 1 forum
Post new topic   Reply to topic
Jump to:  
Author Message
DerikWhittaker



Joined: 11 Dec 2007
Posts: 2

PostPosted: Tue Dec 11, 2007 11:48 pm    Post subject: Recognizing handled exceptions Reply with quote

I ran ExceptionHunter on some test code to see what it would do. When i was provided with the possible exceptions, I added and if statement that threw the correct exceptions the condition was met, however when I run ExcpetionHunter again, it still shows me that the exception condition exists.

Here is my code
No exception handling
public bool StringParameterMethod( string value )
{
if ( value.Equals( "ABC" ) )
{
return true;
}
return false;
}

With exception handling
public bool StringParameterMethod_Valid(string value)
{

if (value == null) { throw new NullReferenceException("The provided parameter {value} was null"); }
if (value.Length == 0) { throw new ArgumentOutOfRangeException("Value", value, "The provided parameter {value} did not contain a valid value." ); }

if (value.Equals("ABC"))
{
return true;
}

return false;
}

Am I doing something wrong?
_________________
Derik Whittaker
http://devlicio.us/
Back to top
View user's profile Send private message
James Moore



Joined: 21 Nov 2005
Posts: 245

PostPosted: Wed Dec 12, 2007 8:52 am    Post subject: Reply with quote

Derik.

With the following code Exception Hunter will tell you you throw all of the exceptions for Equals as well as NullReferenceException/ArgumentOutOfRangeException (Unless it can determine that any of them will never be thrown).

public bool StringParameterMethod_Valid(string value)
{

if (value == null) { throw new NullReferenceException("The provided parameter {value} was null"); }
if (value.Length == 0) { throw new ArgumentOutOfRangeException("Value", value, "The provided parameter {value} did not contain a valid value." ); }

if (value.Equals("ABC"))
{
return true;
}

return false;
}

To stop exceptions you would need to add a try...catch handlers to do somthing when the exceptions are thrown, generally you might want to do this in the functions which call StringParameterMethod_Valid or you might want to wrap your call to Equals with a try...catch and return false, IE:

public bool StringParameterMethod_Valid(string value)
{

try
{

if (value.Equals("ABC"))
{
return true;
}

} catch (ArgumentOutOfRangeException argumentOutOfRangeException)
{
// Log if needed
return false; // not valid
}
catch(NullReferenceException nullReferenceException)
{
//Log as needed
return false; // not valid
}

return false;
}

HTH,

James
_________________
James Moore
Head of DBA Tools
Red Gate Software Ltd
Back to top
View user's profile Send private message
DerikWhittaker



Joined: 11 Dec 2007
Posts: 2

PostPosted: Wed Dec 12, 2007 2:38 pm    Post subject: Reply with quote

James,

I understand what you are saying, I just don't agree with it. I should NOT have to put a try-catch in order for ExceptionHunter to think I have handled the possible exception case.

From my point of view, this violates the fail-fast concept. If i only have a try-catch, then I am not doing my part in checking for possible known failures before processing. I understand that I need try-catches, but try-catches should be for unknown (at development time) exceptions.

I really believe the product should notice the fact that the exception scenario has been handled show you that.
_________________
Derik Whittaker
http://devlicio.us/
Back to top
View user's profile Send private message
James Moore



Joined: 21 Nov 2005
Posts: 245

PostPosted: Wed Dec 12, 2007 2:56 pm    Post subject: Reply with quote

Derik,

When I run analysis on the following code I get told a nullreferenceexception can be thrown:

private static bool MyFunction(string p)
{

if (p.Equals("ABC"))
{
return true;
}

return false;
}

If I add

if (p == null)
{
return false;
}

Before the p.Equals I get told that no exceptions can be thrown.

With your code I would expect to be told a NullReferenceException or ArgumentOutOfRangeException can be thrown as you are throwning them explicitly (their source will be different from your original example without error handling but they are still thrown).

I am not quite sure what output you are expecting from your second example.

James
_________________
James Moore
Head of DBA Tools
Red Gate Software Ltd
Back to top
View user's profile Send private message
tdyen



Joined: 18 Dec 2008
Posts: 1

PostPosted: Thu Dec 18, 2008 12:39 am    Post subject: I have the same problem Reply with quote

If i have some code like thus

private static string GetDateString(DatePicker date)
{
try
{
if (date != null && date.SelectedDate != null)
{
return date.SelectedDate.Value.ToString("yyyy/MM/dd");
}

return DateTime.MinValue.ToString("yyyy/MM/dd");
}
catch (NullReferenceException)
{
return DateTime.MinValue.ToString("yyyy/MM/dd");
}
}

I get an error that System.Windows.Controls.DataPicker.get_SelectedDate() may raise a NullReferenceExcpetion

I have clearly dealt with it twice, I don't see why exception hunter is still showing it as an unhandled exception.
Back to top
View user's profile Send private message
Reyna85



Joined: 29 Jun 2012
Posts: 1
Location: US

PostPosted: Fri Jun 29, 2012 1:17 pm    Post subject: Reply with quote

Thanks for your comments and tips!
Actually I have the same kind of problems and somehow that should help me as well, at least I hope so. Thanks.


_______________

make a word search http://thewordsearchmaker.org/
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic All times are GMT + 1 Hour
Page 1 of 1

 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group