Reputation: 2640
I've got a very standard-fare try / catch that isn't catching a NullReferenceException. I'm trying to return a date value from a form using the .Tag property. This tag will frequently be null, which is fine. My code below seems the simplest to me but it won't catch. Any help would be great, thanks in advance...
public void ScheduleDataRun()
{
FrmSetTimer frmSetTimer = new FrmSetTimer(DateTimeOfNextAvailableDataRun);
try
{
frmSetTimer.ShowDialog();
DateTimeOfNextScheduledDataRun = (DateTime)frmSetTimer.Tag;
SetDataRunTimer(DateTimeOfNextScheduledDataRun);
Status = DRMStatus.Scheduled;
}
catch
{
Status = DRMStatus.Inactive;
StatusChanged();
}
}
Edit: Issue solved per Diggingforfire suggestion below: "In the Debug->Exceptions menu you can choose to break on thrown and user-unhandled exceptions."
Upvotes: 1
Views: 263
Reputation: 37950
This is not really answering the question, but: Causing an exception and catching it is considered bad practice when a simple if
test would have sufficed. Edit: Furthermore, since the exception is apparently caused by attempting to cast null
to DateTime
(which is a value type, which I should have seen - thanks to the commenters, who were more awake than me), you need to check frmSetTimer.Tag
before casting. Try the following:
frmSetTimer.ShowDialog();
if (frmSetTimer.Tag == null) {
Status = DRMStatus.Inactive;
StatusChanged();
}
else {
SetDataRunTimer((DateTime)frmSetTimer.Tag);
Status = DRMStatus.Scheduled;
}
This assumes that SetDataRunTimer()
doesn't have any side effects that you'd want to trigger even if the argument is null. If it does, so that you have to call it whether the argument is null or not, you should modify that method to either handle null gracefully (and maybe return a bool
that indicates whether things went well or not) or throws an ArgumentNullException
. Also, it would need to take a DateTime?
in order to make it possible to pass null
at all. NullReferenceException
should never be thrown by bug-free code and should not be caught explicitly - that way, any NullReferenceException
is always an indication of a programming error.
Upvotes: 7
Reputation: 3399
I'm just curious why you think it's not throwing. What are you expecting? Have you set a breakpoint in the catch? In the Debug->Exceptions menu you can choose to break on thrown and user-unhandled exceptions.
Upvotes: 1
Reputation: 4804
Make sure that there aren't any catch blocks inside SetDataRunTimer that might "swallow" the exception. Here are some useful exceptions guidelines.
Upvotes: 1
Reputation: 4897
Try this:
try
{
frmSetTimer.ShowDialog();
DateTimeOfNextScheduledDataRun = (DateTime)frmSetTimer.Tag;
SetDataRunTimer(DateTimeOfNextScheduledDataRun);
Status = DRMStatus.Scheduled;
}
catch (NullReferenceException)
{
Status = DRMStatus.Inactive;
StatusChanged();
}
EDIT: If this doesn't work, try using the debugger to see if it even enters your 'catch' block in the first place.
Upvotes: 1
Reputation: 12604
Are you sure that there isn't a null reference exception being caused in the catch block?
Upvotes: 2