views:

49

answers:

1

We have a Try/Catch block of code in our web application to trap a certain exception. What we want to do with this exception depends on the current time. If it is between 4:30PM and 3:00AM we will want to send an email, otherwise we will throw the exception. What is the best way to accomplish this?

Try
    'Yada
Catch ex as WebException
    Dim Time As DateTime = DateTime.Now

    'Not sure where to go from here!
End Try
+1  A: 

Since it is between those times every day the easiest thing would be to check to see if now is before 3:00 AM on the current day, or if now is after 4:30 PM on the current day. That would then cover both the 4:30 PM to 12 midnight and midnight to 3 am time spans.

Edited by Josh Stodola to add code:

  Dim Time As DateTime = DateTime.Now
  Dim Low As New Date(Time.Year, Time.Month, Time.Day, 16, 30, 0)
  Dim High As New Date(Time.Year, Time.Month, Time.Day, 3, 0, 0)

  If Time > Low OrElse Time < High Then
      'Send Email
  Else
      Throw Ex
  End If
Solmead
Shouldn't 4:30 be 16,30,0?
Matt Hamsmith
Yes but I decided to remove the exact code on how, I started wondering if this was someones homework assignment
Solmead
Re added it back in.
Solmead
No, it is not homework.
Josh Stodola
I edited answer to include my tested code. Thank you sir!!
Josh Stodola