diff --git a/RGB.NET.Core/Devices/AbstractRGBDeviceProvider.cs b/RGB.NET.Core/Devices/AbstractRGBDeviceProvider.cs index 630bf09..5ff52a6 100644 --- a/RGB.NET.Core/Devices/AbstractRGBDeviceProvider.cs +++ b/RGB.NET.Core/Devices/AbstractRGBDeviceProvider.cs @@ -22,7 +22,7 @@ namespace RGB.NET.Core #region Events - public event EventHandler? Exception; + public event EventHandler? Exception; #endregion @@ -54,10 +54,15 @@ namespace RGB.NET.Core IsInitialized = true; } + catch (DeviceProviderException) + { + Reset(); + throw; + } catch (Exception ex) { Reset(); - Throw(ex); + Throw(ex, true); return false; } @@ -98,15 +103,16 @@ namespace RGB.NET.Core IsInitialized = false; } - protected virtual void Throw(Exception ex) + protected virtual void Throw(Exception ex, bool isCritical = false) { - try { OnException(ex); } catch { /* we don't want to throw due to bad event handlers */ } + ExceptionEventArgs args = new(ex, isCritical, ThrowsExceptions); + try { OnException(args); } catch { /* we don't want to throw due to bad event handlers */ } - if (ThrowsExceptions) - throw ex; + if (args.Throw) + throw new DeviceProviderException(ex, isCritical); } - protected virtual void OnException(Exception ex) => Exception?.Invoke(this, ex); + protected virtual void OnException(ExceptionEventArgs args) => Exception?.Invoke(this, args); public virtual void Dispose() { diff --git a/RGB.NET.Core/Devices/IRGBDeviceProvider.cs b/RGB.NET.Core/Devices/IRGBDeviceProvider.cs index afa4ab0..e923ad4 100644 --- a/RGB.NET.Core/Devices/IRGBDeviceProvider.cs +++ b/RGB.NET.Core/Devices/IRGBDeviceProvider.cs @@ -27,7 +27,7 @@ namespace RGB.NET.Core /// /// Occurs when an exception is thrown in the device provider /// - event EventHandler? Exception; + event EventHandler? Exception; #endregion diff --git a/RGB.NET.Core/Events/ExceptionEventArgs.cs b/RGB.NET.Core/Events/ExceptionEventArgs.cs index 01e58ae..c5ae6bc 100644 --- a/RGB.NET.Core/Events/ExceptionEventArgs.cs +++ b/RGB.NET.Core/Events/ExceptionEventArgs.cs @@ -18,6 +18,10 @@ namespace RGB.NET.Core /// public Exception Exception { get; } + public bool IsCritical { get; } + + public bool Throw { get; set; } + #endregion #region Constructors @@ -27,9 +31,11 @@ namespace RGB.NET.Core /// Initializes a new instance of the class. /// /// The which is responsible for the event-call. - public ExceptionEventArgs(Exception exception) + public ExceptionEventArgs(Exception exception, bool isCritical = false, bool @throw = false) { this.Exception = exception; + this.IsCritical = isCritical; + this.Throw = @throw; } #endregion diff --git a/RGB.NET.Core/Exceptions/DeviceProviderException.cs b/RGB.NET.Core/Exceptions/DeviceProviderException.cs new file mode 100644 index 0000000..f17afdb --- /dev/null +++ b/RGB.NET.Core/Exceptions/DeviceProviderException.cs @@ -0,0 +1,23 @@ +using System; + +namespace RGB.NET.Core +{ + public class DeviceProviderException : ApplicationException + { + #region Properties & Fields + + private bool IsCritical { get; } + + #endregion + + #region Constructors + + public DeviceProviderException(Exception? innerException, bool isCritical) + : base(innerException?.Message, innerException) + { + this.IsCritical = isCritical; + } + + #endregion + } +}