title: Handle Exceptions from inside Hangfire
body: <p>I have a scenario, I am scheduling a BackgroundJob to run after some random seconds on application start. After the job is performed then I am again scheduling the same BackgroundJob to run after some random seconds and it goes on and never stops.</p>
  
  <p>What I want is if any exception occurs in Hangfire during this continuous process I want to shut-down my asp.net core app.</p>
  
  <p>for example, while this process is happening I stop the SQL Server. So hangfire giving this exception that is obvious</p>
  
  <blockquote>
    <p>System.Data.SqlClient.SqlException: A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)</p>
    
    <p>System.ComponentModel.Win32Exception: The system cannot find the file specified<br>
    --- End of inner exception stack trace ---<br>
    at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean onlyOneCheckConnection, DbConnectionOptions userOptions, DbConnectionInternal&amp; connection)
        at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, TaskCompletionSource1 retry, DbConnectionOptions userOptions, DbConnectionInternal&amp; connection)
        at System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal&amp; connection)
        at System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource1 retry, DbConnectionOptions userOptions)
        at System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource1 retry)
        at System.Data.SqlClient.SqlConnection.Open()
        at Hangfire.SqlServer.SqlServerStorage.CreateAndOpenConnection()
        at Hangfire.SqlServer.SqlServerStorage.UseConnection[T](Func`2 func)
        at Hangfire.SqlServer.SqlServerConnection.RemoveTimedOutServers(TimeSpan timeOut)
        at Hangfire.Server.ServerWatchdog.Execute(BackgroundProcessContext context)
        at Hangfire.Server.AutomaticRetryProcess.Execute(BackgroundProcessContext context)</p>
  </blockquote>
  
  <p>So I don't know where to handle this exception, so that I can stop my application if this exception occurs.</p>