├── .gitattributes ├── .gitignore ├── LICENSE ├── README.md ├── SimpleExceptionHandling-Logo.pdn ├── SimpleExceptionHandling-Logo.png ├── SimpleExceptionHandling.sln ├── global.json ├── src ├── SimpleExceptionHandling.Examples │ ├── Program.cs │ ├── Properties │ │ └── AssemblyInfo.cs │ ├── SimpleExceptionHandling.Examples.xproj │ └── project.json └── SimpleExceptionHandling │ ├── Delegates.cs │ ├── Handling.cs │ ├── HandlingConfiguration.cs │ ├── HandlingInput.cs │ ├── HandlingResult.cs │ ├── IHandlingConfiguration.cs │ ├── IHandlingInput.cs │ ├── IHandlingResult.cs │ ├── NoHandlerMatchedException.cs │ ├── Properties │ └── AssemblyInfo.cs │ ├── SimpleExceptionHandling.xproj │ └── project.json └── tools ├── SimpleExceptionHandling.PublicKey └── SimpleExceptionHandling.snk /.gitattributes: -------------------------------------------------------------------------------- 1 | ############################################################################### 2 | # Set default behavior to automatically normalize line endings. 3 | ############################################################################### 4 | * text=auto 5 | 6 | ############################################################################### 7 | # Set default behavior for command prompt diff. 8 | # 9 | # This is need for earlier builds of msysgit that does not have it on by 10 | # default for csharp files. 11 | # Note: This is only used by command line 12 | ############################################################################### 13 | #*.cs diff=csharp 14 | 15 | ############################################################################### 16 | # Set the merge driver for project and solution files 17 | # 18 | # Merging from the command prompt will add diff markers to the files if there 19 | # are conflicts (Merging from VS is not affected by the settings below, in VS 20 | # the diff markers are never inserted). Diff markers may cause the following 21 | # file extensions to fail to load in VS. An alternative would be to treat 22 | # these files as binary and thus will always conflict and require user 23 | # intervention with every merge. To do so, just uncomment the entries below 24 | ############################################################################### 25 | #*.sln merge=binary 26 | #*.csproj merge=binary 27 | #*.vbproj merge=binary 28 | #*.vcxproj merge=binary 29 | #*.vcproj merge=binary 30 | #*.dbproj merge=binary 31 | #*.fsproj merge=binary 32 | #*.lsproj merge=binary 33 | #*.wixproj merge=binary 34 | #*.modelproj merge=binary 35 | #*.sqlproj merge=binary 36 | #*.wwaproj merge=binary 37 | 38 | ############################################################################### 39 | # behavior for image files 40 | # 41 | # image files are treated as binary by default. 42 | ############################################################################### 43 | #*.jpg binary 44 | #*.png binary 45 | #*.gif binary 46 | 47 | ############################################################################### 48 | # diff behavior for common document formats 49 | # 50 | # Convert binary document formats to text before diffing them. This feature 51 | # is only available from the command line. Turn it on by uncommenting the 52 | # entries below. 53 | ############################################################################### 54 | #*.doc diff=astextplain 55 | #*.DOC diff=astextplain 56 | #*.docx diff=astextplain 57 | #*.DOCX diff=astextplain 58 | #*.dot diff=astextplain 59 | #*.DOT diff=astextplain 60 | #*.pdf diff=astextplain 61 | #*.PDF diff=astextplain 62 | #*.rtf diff=astextplain 63 | #*.RTF diff=astextplain 64 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | 4 | # User-specific files 5 | *.suo 6 | *.user 7 | *.userosscache 8 | *.sln.docstates 9 | 10 | # User-specific files (MonoDevelop/Xamarin Studio) 11 | *.userprefs 12 | 13 | # Build results 14 | [Dd]ebug/ 15 | [Dd]ebugPublic/ 16 | [Rr]elease/ 17 | [Rr]eleases/ 18 | [Xx]64/ 19 | [Xx]86/ 20 | [Bb]uild/ 21 | bld/ 22 | [Bb]in/ 23 | [Oo]bj/ 24 | 25 | # Visual Studio 2015 cache/options directory 26 | .vs/ 27 | # Uncomment if you have tasks that create the project's static files in wwwroot 28 | #wwwroot/ 29 | 30 | # MSTest test Results 31 | [Tt]est[Rr]esult*/ 32 | [Bb]uild[Ll]og.* 33 | 34 | # NUNIT 35 | *.VisualState.xml 36 | TestResult.xml 37 | 38 | # Build Results of an ATL Project 39 | [Dd]ebugPS/ 40 | [Rr]eleasePS/ 41 | dlldata.c 42 | 43 | # DNX 44 | project.lock.json 45 | artifacts/ 46 | 47 | *_i.c 48 | *_p.c 49 | *_i.h 50 | *.ilk 51 | *.meta 52 | *.obj 53 | *.pch 54 | *.pdb 55 | *.pgc 56 | *.pgd 57 | *.rsp 58 | *.sbr 59 | *.tlb 60 | *.tli 61 | *.tlh 62 | *.tmp 63 | *.tmp_proj 64 | *.log 65 | *.vspscc 66 | *.vssscc 67 | .builds 68 | *.pidb 69 | *.svclog 70 | *.scc 71 | 72 | # Chutzpah Test files 73 | _Chutzpah* 74 | 75 | # Visual C++ cache files 76 | ipch/ 77 | *.aps 78 | *.ncb 79 | *.opendb 80 | *.opensdf 81 | *.sdf 82 | *.cachefile 83 | *.VC.db 84 | 85 | # Visual Studio profiler 86 | *.psess 87 | *.vsp 88 | *.vspx 89 | *.sap 90 | 91 | # TFS 2012 Local Workspace 92 | $tf/ 93 | 94 | # Guidance Automation Toolkit 95 | *.gpState 96 | 97 | # ReSharper is a .NET coding add-in 98 | _ReSharper*/ 99 | *.[Rr]e[Ss]harper 100 | *.DotSettings.user 101 | 102 | # JustCode is a .NET coding add-in 103 | .JustCode 104 | 105 | # TeamCity is a build add-in 106 | _TeamCity* 107 | 108 | # DotCover is a Code Coverage Tool 109 | *.dotCover 110 | 111 | # NCrunch 112 | _NCrunch_* 113 | .*crunch*.local.xml 114 | nCrunchTemp_* 115 | 116 | # MightyMoose 117 | *.mm.* 118 | AutoTest.Net/ 119 | 120 | # Web workbench (sass) 121 | .sass-cache/ 122 | 123 | # Installshield output folder 124 | [Ee]xpress/ 125 | 126 | # DocProject is a documentation generator add-in 127 | DocProject/buildhelp/ 128 | DocProject/Help/*.HxT 129 | DocProject/Help/*.HxC 130 | DocProject/Help/*.hhc 131 | DocProject/Help/*.hhk 132 | DocProject/Help/*.hhp 133 | DocProject/Help/Html2 134 | DocProject/Help/html 135 | 136 | # Click-Once directory 137 | publish/ 138 | 139 | # Publish Web Output 140 | *.[Pp]ublish.xml 141 | *.azurePubxml 142 | 143 | # TODO: Un-comment the next line if you do not want to checkin 144 | # your web deploy settings because they may include unencrypted 145 | # passwords 146 | #*.pubxml 147 | *.publishproj 148 | 149 | # NuGet Packages 150 | *.nupkg 151 | # The packages folder can be ignored because of Package Restore 152 | **/packages/* 153 | # except build/, which is used as an MSBuild target. 154 | !**/packages/build/ 155 | # Uncomment if necessary however generally it will be regenerated when needed 156 | #!**/packages/repositories.config 157 | # NuGet v3's project.json files produces more ignoreable files 158 | *.nuget.props 159 | *.nuget.targets 160 | 161 | # Microsoft Azure Build Output 162 | csx/ 163 | *.build.csdef 164 | 165 | # Microsoft Azure Emulator 166 | ecf/ 167 | rcf/ 168 | 169 | # Microsoft Azure ApplicationInsights config file 170 | ApplicationInsights.config 171 | 172 | # Windows Store app package directory 173 | AppPackages/ 174 | BundleArtifacts/ 175 | 176 | # Visual Studio cache files 177 | # files ending in .cache can be ignored 178 | *.[Cc]ache 179 | # but keep track of directories ending in .cache 180 | !*.[Cc]ache/ 181 | 182 | # Others 183 | ClientBin/ 184 | [Ss]tyle[Cc]op.* 185 | ~$* 186 | *~ 187 | *.dbmdl 188 | *.dbproj.schemaview 189 | *.pfx 190 | *.publishsettings 191 | node_modules/ 192 | orleans.codegen.cs 193 | 194 | # RIA/Silverlight projects 195 | Generated_Code/ 196 | 197 | # Backup & report files from converting an old project file 198 | # to a newer Visual Studio version. Backup files are not needed, 199 | # because we have git ;-) 200 | _UpgradeReport_Files/ 201 | Backup*/ 202 | UpgradeLog*.XML 203 | UpgradeLog*.htm 204 | 205 | # SQL Server files 206 | *.mdf 207 | *.ldf 208 | 209 | # Business Intelligence projects 210 | *.rdl.data 211 | *.bim.layout 212 | *.bim_*.settings 213 | 214 | # Microsoft Fakes 215 | FakesAssemblies/ 216 | 217 | # GhostDoc plugin setting file 218 | *.GhostDoc.xml 219 | 220 | # Node.js Tools for Visual Studio 221 | .ntvs_analysis.dat 222 | 223 | # Visual Studio 6 build log 224 | *.plg 225 | 226 | # Visual Studio 6 workspace options file 227 | *.opt 228 | 229 | # Visual Studio LightSwitch build output 230 | **/*.HTMLClient/GeneratedArtifacts 231 | **/*.DesktopClient/GeneratedArtifacts 232 | **/*.DesktopClient/ModelManifest.xml 233 | **/*.Server/GeneratedArtifacts 234 | **/*.Server/ModelManifest.xml 235 | _Pvt_Extensions 236 | 237 | # LightSwitch generated files 238 | GeneratedArtifacts/ 239 | ModelManifest.xml 240 | 241 | # Paket dependency manager 242 | .paket/paket.exe 243 | 244 | # FAKE - F# Make 245 | .fake/ -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 João Simões 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Simple Exception Handling 2 | Library that helps developers to handle exceptions outside catch blocks. 3 | Typical usages are global exception handlers. 4 | 5 | ## Installation 6 | This library can be installed via [NuGet](https://www.nuget.org/packages/SimpleExceptionHandling/) package. Just run the following command: 7 | 8 | ```powershell 9 | Install-Package SimpleExceptionHandling 10 | ``` 11 | 12 | ## Compatibility 13 | 14 | This library is compatible with the folowing frameworks: 15 | 16 | * MonoAndroid 1.0; 17 | * MonoTouch 1.0; 18 | * .NET Framework 2.0 19 | * .NET Framework 3.5 20 | * .NET Framework 4.0 21 | * .NET Framework 4.5 22 | * .NET Core 5.0; 23 | * .NET Standard 1.0; 24 | * Portable Class Library (.NETFramework 4.0, Silverlight 5.0, Windows 8.0, WindowsPhone 8.0, WindowsPhoneApp 8.1); 25 | * Portable Class Library (.NETFramework 4.5, Windows 8.0, WindowsPhoneApp 8.1); 26 | * WindowsPhoneApp 8.1; 27 | * Xamarin.iOS 1.0; 28 | * Xamarin.TVOS 1.0; 29 | 30 | ## Tipical usage 31 | 32 | This is a usage example for Web API 2: 33 | 34 | ```csharp 35 | using SimpleExceptionHandling; 36 | 37 | public class GlobalExceptionHandler : ExceptionHandler 38 | { 39 | private static readonly IHandlingConfiguration 40 | HandlingConfiguration = 41 | Handling.Prepare() 42 | .On((ex, i) => 43 | { 44 | return Handling.Handled( 45 | i.Parameter.Request.CreateBadRequestResult( 46 | ex.ValidationErrors.Select( 47 | e => new KeyValuePair( 48 | e.Key, e.Messages.ToArray())))); 49 | }) 50 | .On((ex, i) => 51 | { 52 | return Handling.Handled( 53 | i.Parameter.Request.CreateConflictResult(ex.Message)); 54 | }) 55 | .On((ex, i) => 56 | { 57 | return Handling.Handled( 58 | i.Parameter.Request.CreateInternalServerErrorResult(ex.Message)); 59 | }) 60 | .On((ex, i) => 61 | { 62 | return Handling.Handled( 63 | i.Parameter.Request.CreateBadGatewayResult()); 64 | }) 65 | .On((ex, i) => 66 | { 67 | return Handling.Handled( 68 | i.Parameter.Request.CreateGatewayTimeoutResult()); 69 | }) 70 | .On((ex, i) => 71 | { 72 | return Handling.Handled( 73 | i.Parameter.Request.CreateNotImplementedResult()); 74 | }); 75 | 76 | public override bool ShouldHandle(ExceptionHandlerContext context) 77 | { 78 | return true; 79 | } 80 | 81 | public override void Handle(ExceptionHandlerContext context) 82 | { 83 | var result = HandlingConfiguration.Catch(context.Exception, context, false); 84 | if (result.Handled) 85 | { 86 | context.Result = result.Result; 87 | } 88 | else 89 | { 90 | base.Handle(context); 91 | } 92 | } 93 | } 94 | ``` 95 | 96 | ### Basic usage 97 | 98 | Here is a simple example of handling exceptions by their types: 99 | 100 | ```csharp 101 | public static void BasicExceptionHandling(string param01) 102 | { 103 | string handlerName = null; 104 | var configuration = 105 | Handling.Prepare() 106 | .On(ex => 107 | { 108 | handlerName = $"ArgumentNullException[ParamName={ex.ParamName}]"; 109 | }) 110 | .On(ex => 111 | { 112 | handlerName = $"ArgumentException[ParamName={ex.ParamName}]"; 113 | }); 114 | 115 | var result = configuration.Catch(new ArgumentNullException(nameof(param01))); 116 | Console.WriteLine($"Handler[Handled={result.Handled}] -> '{handlerName}'"); 117 | // Handler[Handled=True] -> 'ArgumentNullException[ParamName=param01]' 118 | 119 | handlerName = null; 120 | result = configuration.Catch(new ArgumentOutOfRangeException(nameof(param01))); 121 | Console.WriteLine($"Handler[Handled={result.Handled}] -> '{handlerName}'"); 122 | // Handler[Handled=True] -> 'ArgumentException[ParamName=param01]' 123 | 124 | handlerName = null; 125 | result = configuration.Catch(new Exception(), throwIfNotHandled: false); 126 | Console.WriteLine($"Handler[Handled={result.Handled}] -> '{handlerName}'"); 127 | // Handler[Handled=False] -> '' 128 | } 129 | ``` 130 | 131 | ### Input Parameter and Result 132 | 133 | In this example, a parameter is passed as an argument of the `Catch` method, and a result is returned by handlers. One of the handlers will be invoked but won't be considered to handle the exception: 134 | 135 | ```csharp 136 | public static void InputAndResultExceptionHandling(string param01) 137 | { 138 | var configuration = 139 | Handling.Prepare() 140 | .On((ex, i) => 141 | { 142 | // this handler will be invoked, but says to be ignored 143 | 144 | //return new HandlingResult(false); 145 | //return Handling.Ignore(); 146 | return false; 147 | }) 148 | .On((ex, i) => 149 | { 150 | var ret = $"ArgumentException[ParamName={ex.ParamName}, InputParameter={i.Parameter}]"; 151 | 152 | //return new HandlingResult(true, ret); 153 | return Handling.Handled(ret); 154 | }) 155 | .On((ex, i) => 156 | { 157 | var ret = $"Exception[InputParameter={i.Parameter}]"; 158 | 159 | //return new HandlingResult(true, ret); 160 | return Handling.Handled(ret); 161 | }); 162 | 163 | var result = 164 | configuration.Catch( 165 | new ArgumentNullException(nameof(param01)), 987987); 166 | Console.WriteLine($"Handler[Handled={result.Handled}] -> '{result.Result}'"); 167 | // Handler[Handled=True] -> 'ArgumentException[ParamName=param01, InputParameter=987987]' 168 | 169 | result = 170 | configuration.Catch( 171 | new ArgumentOutOfRangeException(nameof(param01)), 123123); 172 | Console.WriteLine($"Handler[Handled={result.Handled}] -> '{result.Result}'"); 173 | // Handler[Handled=True] -> 'ArgumentException[ParamName=param01, InputParameter=123123]' 174 | 175 | result = 176 | configuration.Catch(new Exception(), 54321); 177 | Console.WriteLine($"Handler[Handled={result.Handled}] -> '{result.Result}'"); 178 | // Handler[Handled=True] -> 'Exception[InputParameter=54321]' 179 | } 180 | ``` 181 | 182 | ## Conditions 183 | 184 | In this example, some handlers are conditionally invoked, even if the exception type match: 185 | 186 | ```csharp 187 | public static void ConditionalExceptionHandling(string param01, string param02, string param03) 188 | { 189 | string handlerName = null; 190 | var configuration = 191 | Handling.Prepare() 192 | .On(ex => 193 | { 194 | handlerName = "ArgumentNullException[ParamName=param01]"; 195 | }, (ex, i) => ex.ParamName == nameof(param01)) 196 | .On(ex => 197 | { 198 | handlerName = "ArgumentNullException[ParamName=param02]"; 199 | }, (ex, i) => ex.ParamName == nameof(param02)) 200 | .On(ex => 201 | { 202 | handlerName = $"ArgumentNullException[ParamName={ex.ParamName}]"; 203 | }); 204 | 205 | var result = configuration.Catch(new ArgumentNullException(nameof(param01))); 206 | Console.WriteLine($"Handler[Handled={result.Handled}] -> '{handlerName}'"); 207 | // Handler[Handled=True] -> 'ArgumentException[ParamName=param01]' 208 | 209 | result = configuration.Catch(new ArgumentNullException(nameof(param03))); 210 | Console.WriteLine($"Handler[Handled={result.Handled}] -> '{handlerName}'"); 211 | // Handler[Handled=True] -> 'ArgumentException[ParamName=param03]' 212 | } 213 | ``` 214 | -------------------------------------------------------------------------------- /SimpleExceptionHandling-Logo.pdn: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gravity00/SimpleExceptionHandling/9912ea2c4793d14bd4beb8895ec5d8206dc65694/SimpleExceptionHandling-Logo.pdn -------------------------------------------------------------------------------- /SimpleExceptionHandling-Logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gravity00/SimpleExceptionHandling/9912ea2c4793d14bd4beb8895ec5d8206dc65694/SimpleExceptionHandling-Logo.png -------------------------------------------------------------------------------- /SimpleExceptionHandling.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 14 4 | VisualStudioVersion = 14.0.25420.1 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{F8FF3B4B-266E-45C6-ABD1-5B68FFF7733C}" 7 | EndProject 8 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{493EEE14-9FB8-4D53-A0CF-78BD1A0BDBF4}" 9 | ProjectSection(SolutionItems) = preProject 10 | global.json = global.json 11 | EndProjectSection 12 | EndProject 13 | Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "SimpleExceptionHandling", "src\SimpleExceptionHandling\SimpleExceptionHandling.xproj", "{7C547964-762F-4E2A-9473-E3FB5D1ADCCB}" 14 | EndProject 15 | Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "SimpleExceptionHandling.Examples", "src\SimpleExceptionHandling.Examples\SimpleExceptionHandling.Examples.xproj", "{012218D6-2181-4885-BB01-4173152F1C41}" 16 | EndProject 17 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tools", "tools", "{5737C83B-3ECB-4AFC-9D5C-D1EB6B50272A}" 18 | ProjectSection(SolutionItems) = preProject 19 | tools\SimpleExceptionHandling.PublicKey = tools\SimpleExceptionHandling.PublicKey 20 | tools\SimpleExceptionHandling.snk = tools\SimpleExceptionHandling.snk 21 | EndProjectSection 22 | EndProject 23 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "git", "git", "{98F6CA57-3C03-4D78-9C0B-75806E6515D4}" 24 | ProjectSection(SolutionItems) = preProject 25 | .gitattributes = .gitattributes 26 | .gitignore = .gitignore 27 | EndProjectSection 28 | EndProject 29 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "other", "other", "{A66BA278-D0C6-4373-909B-6D375AAFBF40}" 30 | ProjectSection(SolutionItems) = preProject 31 | LICENSE = LICENSE 32 | README.md = README.md 33 | SimpleExceptionHandling-Logo.pdn = SimpleExceptionHandling-Logo.pdn 34 | SimpleExceptionHandling-Logo.png = SimpleExceptionHandling-Logo.png 35 | EndProjectSection 36 | EndProject 37 | Global 38 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 39 | Debug|Any CPU = Debug|Any CPU 40 | Release|Any CPU = Release|Any CPU 41 | EndGlobalSection 42 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 43 | {7C547964-762F-4E2A-9473-E3FB5D1ADCCB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 44 | {7C547964-762F-4E2A-9473-E3FB5D1ADCCB}.Debug|Any CPU.Build.0 = Debug|Any CPU 45 | {7C547964-762F-4E2A-9473-E3FB5D1ADCCB}.Release|Any CPU.ActiveCfg = Release|Any CPU 46 | {7C547964-762F-4E2A-9473-E3FB5D1ADCCB}.Release|Any CPU.Build.0 = Release|Any CPU 47 | {012218D6-2181-4885-BB01-4173152F1C41}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 48 | {012218D6-2181-4885-BB01-4173152F1C41}.Debug|Any CPU.Build.0 = Debug|Any CPU 49 | {012218D6-2181-4885-BB01-4173152F1C41}.Release|Any CPU.ActiveCfg = Release|Any CPU 50 | {012218D6-2181-4885-BB01-4173152F1C41}.Release|Any CPU.Build.0 = Release|Any CPU 51 | EndGlobalSection 52 | GlobalSection(SolutionProperties) = preSolution 53 | HideSolutionNode = FALSE 54 | EndGlobalSection 55 | GlobalSection(NestedProjects) = preSolution 56 | {7C547964-762F-4E2A-9473-E3FB5D1ADCCB} = {F8FF3B4B-266E-45C6-ABD1-5B68FFF7733C} 57 | {012218D6-2181-4885-BB01-4173152F1C41} = {F8FF3B4B-266E-45C6-ABD1-5B68FFF7733C} 58 | {5737C83B-3ECB-4AFC-9D5C-D1EB6B50272A} = {493EEE14-9FB8-4D53-A0CF-78BD1A0BDBF4} 59 | {98F6CA57-3C03-4D78-9C0B-75806E6515D4} = {493EEE14-9FB8-4D53-A0CF-78BD1A0BDBF4} 60 | {A66BA278-D0C6-4373-909B-6D375AAFBF40} = {493EEE14-9FB8-4D53-A0CF-78BD1A0BDBF4} 61 | EndGlobalSection 62 | EndGlobal 63 | -------------------------------------------------------------------------------- /global.json: -------------------------------------------------------------------------------- 1 | { 2 | "projects": [ "src", "test" ], 3 | "sdk": { 4 | "version": "1.0.0-preview2-003121" 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /src/SimpleExceptionHandling.Examples/Program.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | // The MIT License (MIT) 3 | // 4 | // Copyright (c) 2016 João Simões 5 | // 6 | // Permission is hereby granted, free of charge, to any person obtaining a copy 7 | // of this software and associated documentation files (the "Software"), to deal 8 | // in the Software without restriction, including without limitation the rights 9 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | // copies of the Software, and to permit persons to whom the Software is 11 | // furnished to do so, subject to the following conditions: 12 | // 13 | // The above copyright notice and this permission notice shall be included in all 14 | // copies or substantial portions of the Software. 15 | // 16 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | // SOFTWARE. 23 | #endregion 24 | namespace SimpleExceptionHandling.Examples 25 | { 26 | using System; 27 | 28 | public class Program 29 | { 30 | public static void Main(string[] args) 31 | { 32 | Console.WriteLine(); 33 | BasicExceptionHandling(null); 34 | 35 | Console.WriteLine(); 36 | InputAndResultExceptionHandling(null); 37 | 38 | Console.WriteLine(); 39 | ConditionalExceptionHandling(null, null, null); 40 | 41 | Console.WriteLine(); 42 | Console.WriteLine("Application terminated. Press to exit..."); 43 | Console.ReadLine(); 44 | } 45 | 46 | public static void BasicExceptionHandling(string param01) 47 | { 48 | string handlerName = null; 49 | var configuration = 50 | Handling.Prepare() 51 | .On(ex => 52 | { 53 | handlerName = $"ArgumentNullException[ParamName={ex.ParamName}]"; 54 | }) 55 | .On(ex => 56 | { 57 | handlerName = $"ArgumentException[ParamName={ex.ParamName}]"; 58 | }); 59 | 60 | var result = configuration.Catch(new ArgumentNullException(nameof(param01))); 61 | Console.WriteLine($"Handler[Handled={result.Handled}] -> '{handlerName}'"); 62 | // Handler[Handled=True] -> 'ArgumentNullException[ParamName=param01]' 63 | 64 | handlerName = null; 65 | result = configuration.Catch(new ArgumentOutOfRangeException(nameof(param01))); 66 | Console.WriteLine($"Handler[Handled={result.Handled}] -> '{handlerName}'"); 67 | // Handler[Handled=True] -> 'ArgumentException[ParamName=param01]' 68 | 69 | handlerName = null; 70 | result = configuration.Catch(new Exception(), throwIfNotHandled: false); 71 | Console.WriteLine($"Handler[Handled={result.Handled}] -> '{handlerName}'"); 72 | // Handler[Handled=False] -> '' 73 | } 74 | 75 | public static void InputAndResultExceptionHandling(string param01) 76 | { 77 | var configuration = 78 | Handling.Prepare() 79 | .On((ex, i) => 80 | { 81 | // this handler will be invoked, but says to be ignored 82 | 83 | //return new HandlingResult(false); 84 | //return Handling.Ignore(); 85 | return false; 86 | }) 87 | .On((ex, i) => 88 | { 89 | var ret = $"ArgumentException[ParamName={ex.ParamName}, InputParameter={i.Parameter}]"; 90 | 91 | //return new HandlingResult(true, ret); 92 | return Handling.Handled(ret); 93 | }) 94 | .On((ex, i) => 95 | { 96 | var ret = $"Exception[InputParameter={i.Parameter}]"; 97 | 98 | //return new HandlingResult(true, ret); 99 | return Handling.Handled(ret); 100 | }); 101 | 102 | var result = 103 | configuration.Catch( 104 | new ArgumentNullException(nameof(param01)), 987987); 105 | Console.WriteLine($"Handler[Handled={result.Handled}] -> '{result.Result}'"); 106 | // Handler[Handled=True] -> 'ArgumentException[ParamName=param01, InputParameter=987987]' 107 | 108 | result = 109 | configuration.Catch( 110 | new ArgumentOutOfRangeException(nameof(param01)), 123123); 111 | Console.WriteLine($"Handler[Handled={result.Handled}] -> '{result.Result}'"); 112 | // Handler[Handled=True] -> 'ArgumentException[ParamName=param01, InputParameter=123123]' 113 | 114 | result = 115 | configuration.Catch(new Exception(), 54321); 116 | Console.WriteLine($"Handler[Handled={result.Handled}] -> '{result.Result}'"); 117 | // Handler[Handled=True] -> 'Exception[InputParameter=54321]' 118 | } 119 | 120 | public static void ConditionalExceptionHandling(string param01, string param02, string param03) 121 | { 122 | string handlerName = null; 123 | var configuration = 124 | Handling.Prepare() 125 | .On(ex => 126 | { 127 | handlerName = "ArgumentNullException[ParamName=param01]"; 128 | }, (ex, i) => ex.ParamName == nameof(param01)) 129 | .On(ex => 130 | { 131 | handlerName = "ArgumentNullException[ParamName=param02]"; 132 | }, (ex, i) => ex.ParamName == nameof(param02)) 133 | .On(ex => 134 | { 135 | handlerName = $"ArgumentNullException[ParamName={ex.ParamName}]"; 136 | }); 137 | 138 | var result = configuration.Catch(new ArgumentNullException(nameof(param01))); 139 | Console.WriteLine($"Handler[Handled={result.Handled}] -> '{handlerName}'"); 140 | // Handler[Handled=True] -> 'ArgumentException[ParamName=param01]' 141 | 142 | result = configuration.Catch(new ArgumentNullException(nameof(param03))); 143 | Console.WriteLine($"Handler[Handled={result.Handled}] -> '{handlerName}'"); 144 | // Handler[Handled=True] -> 'ArgumentException[ParamName=param03]' 145 | } 146 | } 147 | } 148 | -------------------------------------------------------------------------------- /src/SimpleExceptionHandling.Examples/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | // The MIT License (MIT) 3 | // 4 | // Copyright (c) 2016 João Simões 5 | // 6 | // Permission is hereby granted, free of charge, to any person obtaining a copy 7 | // of this software and associated documentation files (the "Software"), to deal 8 | // in the Software without restriction, including without limitation the rights 9 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | // copies of the Software, and to permit persons to whom the Software is 11 | // furnished to do so, subject to the following conditions: 12 | // 13 | // The above copyright notice and this permission notice shall be included in all 14 | // copies or substantial portions of the Software. 15 | // 16 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | // SOFTWARE. 23 | #endregion 24 | using System; 25 | using System.Reflection; 26 | 27 | #if NET40 || NET45 28 | using System.Runtime.InteropServices; 29 | #endif 30 | 31 | [assembly: AssemblyTitle("SimpleExceptionHandling.Examples")] 32 | [assembly: AssemblyDescription("Library that helps developers to handle exceptions outside catch blocks.")] 33 | [assembly: AssemblyConfiguration("")] 34 | [assembly: AssemblyCompany("Net.JoaoSimoes")] 35 | [assembly: AssemblyProduct("SimpleExceptionHandling")] 36 | [assembly: AssemblyCopyright("Copyright © 2016 João Simões")] 37 | [assembly: AssemblyTrademark("")] 38 | [assembly: AssemblyCulture("")] 39 | 40 | #if NET40 || NET45 41 | 42 | [assembly: ComVisible(false)] 43 | 44 | [assembly: Guid("012218d6-2181-4885-bb01-4173152f1c41")] 45 | 46 | #endif 47 | 48 | [assembly: CLSCompliant(true)] 49 | 50 | [assembly: AssemblyVersion("1.0.*")] 51 | -------------------------------------------------------------------------------- /src/SimpleExceptionHandling.Examples/SimpleExceptionHandling.Examples.xproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 14.0 5 | $(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion) 6 | 7 | 8 | 9 | 10 | 012218d6-2181-4885-bb01-4173152f1c41 11 | SimpleExceptionHandling.Examples 12 | .\obj 13 | .\bin\ 14 | v4.6.1 15 | 16 | 17 | 18 | 2.0 19 | 20 | 21 | -------------------------------------------------------------------------------- /src/SimpleExceptionHandling.Examples/project.json: -------------------------------------------------------------------------------- 1 | { 2 | "version": "1.0.0-*", 3 | "title": "Simple Exception Handling Examples", 4 | "description": "Example code for SimpleExceptionHandling library.", 5 | "authors": [ "João Simões" ], 6 | "language": "en-US", 7 | "copyright": "Copyright © 2016 João Simões", 8 | "packOptions": { 9 | "summary": "Example code for SimpleExceptionHandling library.", 10 | "tags": [ "exception", "handler", "examples" ], 11 | "projectUrl": "https://github.com/gravity00/SimpleExceptionHandling", 12 | "licenseUrl": "https://raw.githubusercontent.com/gravity00/SimpleExceptionHandling/master/LICENSE", 13 | "iconUrl": "https://raw.githubusercontent.com/gravity00/SimpleExceptionHandling/master/SimpleExceptionHandling-Logo.png", 14 | "releaseNotes": "", 15 | "owners": [ "João Simões" ], 16 | "repository": { 17 | "type": "git", 18 | "url": "https://github.com/gravity00/SimpleExceptionHandling" 19 | } 20 | }, 21 | "buildOptions": { 22 | "xmlDoc": false, 23 | "optimize": false, 24 | "emitEntryPoint": true 25 | }, 26 | 27 | "dependencies": { 28 | "Microsoft.NETCore.App": { 29 | "type": "platform", 30 | "version": "1.0.0" 31 | }, 32 | "SimpleExceptionHandling": { "target": "project" } 33 | }, 34 | 35 | "frameworks": { 36 | "netcoreapp1.0": { } 37 | } 38 | 39 | } 40 | -------------------------------------------------------------------------------- /src/SimpleExceptionHandling/Delegates.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | // The MIT License (MIT) 3 | // 4 | // Copyright (c) 2016 João Simões 5 | // 6 | // Permission is hereby granted, free of charge, to any person obtaining a copy 7 | // of this software and associated documentation files (the "Software"), to deal 8 | // in the Software without restriction, including without limitation the rights 9 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | // copies of the Software, and to permit persons to whom the Software is 11 | // furnished to do so, subject to the following conditions: 12 | // 13 | // The above copyright notice and this permission notice shall be included in all 14 | // copies or substantial portions of the Software. 15 | // 16 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | // SOFTWARE. 23 | #endregion 24 | namespace SimpleExceptionHandling 25 | { 26 | #if NET20 27 | 28 | /// 29 | /// Encapsulates a method that has no parameters and does not return a value. 30 | /// 31 | public delegate void Action(); 32 | 33 | /// 34 | /// Encapsulates a method that has two parameters and does not return a value. 35 | /// 36 | /// The first parameter type 37 | /// The second parameter type 38 | /// The first parameter 39 | /// The second parameter 40 | public delegate void Action(T01 arg01, T02 arg02); 41 | 42 | /// 43 | /// Encapsulates a method that has three parameters and does not return a value. 44 | /// 45 | /// The first parameter type 46 | /// The second parameter type 47 | /// The third parameter type 48 | /// The first parameter 49 | /// The second parameter 50 | /// The third parameter 51 | public delegate void Action(T01 arg01, T02 arg02, T03 arg03); 52 | 53 | /// 54 | /// Encapsulates a method that has a single parameter and returns a value. 55 | /// 56 | /// The parameter type 57 | /// The result type 58 | /// The parameter 59 | /// The result value 60 | public delegate TResult Func(T arg); 61 | 62 | /// 63 | /// Encapsulates a method that has two parameters and returns a value. 64 | /// 65 | /// The first parameter type 66 | /// The second parameter type 67 | /// The result type 68 | /// The first parameter 69 | /// The second parameter 70 | /// The result value 71 | public delegate TResult Func(T01 arg01, T02 arg02); 72 | 73 | #endif 74 | } -------------------------------------------------------------------------------- /src/SimpleExceptionHandling/Handling.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | // The MIT License (MIT) 3 | // 4 | // Copyright (c) 2016 João Simões 5 | // 6 | // Permission is hereby granted, free of charge, to any person obtaining a copy 7 | // of this software and associated documentation files (the "Software"), to deal 8 | // in the Software without restriction, including without limitation the rights 9 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | // copies of the Software, and to permit persons to whom the Software is 11 | // furnished to do so, subject to the following conditions: 12 | // 13 | // The above copyright notice and this permission notice shall be included in all 14 | // copies or substantial portions of the Software. 15 | // 16 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | // SOFTWARE. 23 | #endregion 24 | namespace SimpleExceptionHandling 25 | { 26 | using System; 27 | 28 | /// 29 | /// Exposes static methods to fluently configure instances. 30 | /// 31 | public static class Handling 32 | { 33 | private static readonly IHandlingResult HandledResult = new HandlingResult(true); 34 | 35 | private static readonly IHandlingResult IgnoredResult = new HandlingResult(false); 36 | 37 | #region Prepare 38 | 39 | /// 40 | /// Prepares a new instance to be configured. 41 | /// 42 | /// The parameter type 43 | /// The result type 44 | /// The handling configuration instance 45 | public static IHandlingConfiguration Prepare() 46 | { 47 | return new HandlingConfiguration(); 48 | } 49 | 50 | /// 51 | /// Prepares a new instance to be configured. 52 | /// 53 | /// The handling configuration instance 54 | public static IHandlingConfiguration Prepare() 55 | { 56 | return new HandlingConfiguration(); 57 | } 58 | 59 | #endregion 60 | 61 | #region IHandlingResult.Result 62 | 63 | /// 64 | /// Gets the as an extected type. 65 | /// 66 | /// The state type 67 | /// The handling result to use 68 | /// The result object 69 | /// 70 | /// 71 | #if NET20 72 | public static TResult Result(IHandlingResult result) 73 | #else 74 | public static TResult Result(this IHandlingResult result) 75 | #endif 76 | { 77 | if (result == null) throw new ArgumentNullException(nameof(result)); 78 | 79 | return (TResult)result.Result; 80 | } 81 | 82 | #endregion 83 | 84 | #region IHandlingInput.Parameter 85 | 86 | /// 87 | /// Gets the as an extected type. 88 | /// 89 | /// The state type 90 | /// The handling input to use 91 | /// The parameter object 92 | /// 93 | /// 94 | #if NET20 95 | public static TResult Parameter(IHandlingInput input) 96 | #else 97 | public static TResult Parameter(this IHandlingInput input) 98 | #endif 99 | { 100 | if (input == null) throw new ArgumentNullException(nameof(input)); 101 | 102 | return (TResult)input.Parameter; 103 | } 104 | 105 | #endregion 106 | 107 | #region Handled 108 | 109 | /// 110 | /// Returns an handling result with the flag 111 | /// set to true. 112 | /// 113 | /// The result type 114 | /// The handling result 115 | public static IHandlingResult Handled() 116 | { 117 | return new HandlingResult(true); 118 | } 119 | 120 | /// 121 | /// Returns an handling result with the flag 122 | /// set to true. 123 | /// 124 | /// The result type 125 | /// The result value 126 | /// The handling result 127 | public static IHandlingResult Handled(TResult result) 128 | { 129 | return new HandlingResult(true, result); 130 | } 131 | 132 | /// 133 | /// Returns an handling result with the flag 134 | /// set to true. 135 | /// 136 | /// The handling result 137 | public static IHandlingResult Handled() 138 | { 139 | return HandledResult; 140 | } 141 | 142 | /// 143 | /// Returns an handling result with the flag 144 | /// set to true. 145 | /// 146 | /// The result value 147 | /// The handling result 148 | public static IHandlingResult Handled(object result) 149 | { 150 | return new HandlingResult(true, result); 151 | } 152 | 153 | #endregion 154 | 155 | #region Ignore 156 | 157 | /// 158 | /// Returns an handling result with the flag 159 | /// set to false. 160 | /// 161 | /// The result type 162 | /// The handling result 163 | public static IHandlingResult Ignore() 164 | { 165 | return new HandlingResult(false); 166 | } 167 | 168 | /// 169 | /// Returns an handling result with the flag 170 | /// set to false. 171 | /// 172 | /// The handling result 173 | public static IHandlingResult Ignore() 174 | { 175 | return IgnoredResult; 176 | } 177 | 178 | #endregion 179 | } 180 | } 181 | -------------------------------------------------------------------------------- /src/SimpleExceptionHandling/HandlingConfiguration.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | // The MIT License (MIT) 3 | // 4 | // Copyright (c) 2016 João Simões 5 | // 6 | // Permission is hereby granted, free of charge, to any person obtaining a copy 7 | // of this software and associated documentation files (the "Software"), to deal 8 | // in the Software without restriction, including without limitation the rights 9 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | // copies of the Software, and to permit persons to whom the Software is 11 | // furnished to do so, subject to the following conditions: 12 | // 13 | // The above copyright notice and this permission notice shall be included in all 14 | // copies or substantial portions of the Software. 15 | // 16 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | // SOFTWARE. 23 | #endregion 24 | namespace SimpleExceptionHandling 25 | { 26 | using System; 27 | 28 | /// 29 | /// The exception handling configuration. 30 | /// 31 | /// The parameter type 32 | /// The result type 33 | public class HandlingConfiguration : IHandlingConfiguration 34 | { 35 | private Func, IHandlingResult>[] _handlers = 36 | new Func, IHandlingResult>[10]; 37 | private int _handlerCount; 38 | private Action, IHandlingResult> _finalizationHandler; 39 | 40 | #region On 41 | 42 | /// 43 | /// Adds the given exception handler to this configuration. If this handler matches 44 | /// a given exception on , 45 | /// it will be considered successfully handled. 46 | /// 47 | /// The exception type 48 | /// The handler to be added 49 | /// An optional condition to be checked if the handler must be used 50 | /// The configuration after changes 51 | /// 52 | public IHandlingConfiguration On( 53 | Action> handler, 54 | Func, bool> condition = null) 55 | where TException : Exception 56 | { 57 | if (handler == null) throw new ArgumentNullException(nameof(handler)); 58 | 59 | AddHandler((ex, i) => 60 | { 61 | var castedException = ex as TException; 62 | if (castedException == null || (condition != null && !condition(castedException, i))) 63 | return new HandlingResult(false); 64 | 65 | handler(castedException, i); 66 | return new HandlingResult(true); 67 | }); 68 | 69 | return this; 70 | } 71 | 72 | /// 73 | /// Adds the given exception handler to this configuration. If this handler matches 74 | /// a given exception on and 75 | /// true is returned, it will be considered successfully handled. 76 | /// 77 | /// The exception type 78 | /// The handler to be added 79 | /// An optional condition to be checked if the handler must be used 80 | /// The configuration after changes 81 | /// 82 | public IHandlingConfiguration On( 83 | Func, bool> handler, 84 | Func, bool> condition = null) 85 | where TException : Exception 86 | { 87 | if (handler == null) throw new ArgumentNullException(nameof(handler)); 88 | 89 | AddHandler((ex, i) => 90 | { 91 | var castedException = ex as TException; 92 | if (castedException == null || (condition != null && !condition(castedException, i))) 93 | return new HandlingResult(false); 94 | 95 | return new HandlingResult(handler(castedException, i)); 96 | }); 97 | 98 | return this; 99 | } 100 | 101 | /// 102 | /// Adds the given exception handler to this configuration. If this handler matches 103 | /// a given exception on 104 | /// and is true, 105 | /// it will be considered successfully handled. 106 | /// 107 | /// The exception type 108 | /// The handler to be added 109 | /// An optional condition to be checked if the handler must be used 110 | /// The configuration after changes 111 | /// 112 | public IHandlingConfiguration On( 113 | Func, IHandlingResult> handler, 114 | Func, bool> condition = null) 115 | where TException : Exception 116 | { 117 | if (handler == null) throw new ArgumentNullException(nameof(handler)); 118 | 119 | AddHandler((ex, i) => 120 | { 121 | var castedException = ex as TException; 122 | if (castedException == null || (condition != null && !condition(castedException, i))) 123 | return new HandlingResult(false); 124 | 125 | return handler(castedException, i); 126 | }); 127 | 128 | return this; 129 | } 130 | 131 | /// 132 | /// Adds the given exception handler to this configuration. If this handler matches 133 | /// a given exception on , 134 | /// it will be considered successfully handled. 135 | /// 136 | /// The exception type 137 | /// The handler to be added 138 | /// An optional condition to be checked if the handler must be used 139 | /// The configuration after changes 140 | /// 141 | public IHandlingConfiguration On( 142 | Action handler, 143 | Func, bool> condition = null) 144 | where TException : Exception 145 | { 146 | if (handler == null) throw new ArgumentNullException(nameof(handler)); 147 | 148 | AddHandler((ex, i) => 149 | { 150 | var castedException = ex as TException; 151 | if (castedException == null || (condition != null && !condition(castedException, i))) 152 | return new HandlingResult(false); 153 | 154 | handler(castedException); 155 | return new HandlingResult(true); 156 | }); 157 | 158 | return this; 159 | } 160 | 161 | /// 162 | /// Adds the given exception handler to this configuration. If this handler matches 163 | /// a given exception on 164 | /// and true is returned, it will be considered successfully handled. 165 | /// 166 | /// The exception type 167 | /// The handler to be added 168 | /// An optional condition to be checked if the handler must be used 169 | /// The configuration after changes 170 | /// 171 | public IHandlingConfiguration On( 172 | Func handler, 173 | Func, bool> condition = null) 174 | where TException : Exception 175 | { 176 | if (handler == null) throw new ArgumentNullException(nameof(handler)); 177 | 178 | AddHandler((ex, i) => 179 | { 180 | var castedException = ex as TException; 181 | if (castedException == null || (condition != null && !condition(castedException, i))) 182 | return new HandlingResult(false); 183 | 184 | return new HandlingResult(handler(castedException)); 185 | }); 186 | 187 | return this; 188 | } 189 | 190 | /// 191 | /// Adds the given exception handler to this configuration. If this handler matches 192 | /// a given exception on 193 | /// and is true, 194 | /// it will be considered successfully handled. 195 | /// 196 | /// The exception type 197 | /// The handler to be added 198 | /// An optional condition to be checked if the handler must be used 199 | /// The configuration after changes 200 | /// 201 | public IHandlingConfiguration On( 202 | Func> handler, 203 | Func, bool> condition = null) 204 | where TException : Exception 205 | { 206 | if (handler == null) throw new ArgumentNullException(nameof(handler)); 207 | 208 | AddHandler((ex, i) => 209 | { 210 | var castedException = ex as TException; 211 | if (castedException == null || (condition != null && !condition(castedException, i))) 212 | return new HandlingResult(false); 213 | 214 | return handler(castedException); 215 | }); 216 | 217 | return this; 218 | } 219 | 220 | #endregion 221 | 222 | #region FinalizeWith 223 | 224 | /// 225 | /// Sets the given handler as the finalization handler that will be executed even if not handlers 226 | /// matched the given exception. 227 | /// 228 | /// The handler to use 229 | /// The configuration after changes 230 | /// 231 | public IHandlingConfiguration FinalizeWith( 232 | Action, IHandlingResult> finalizationHandler) 233 | { 234 | if (finalizationHandler == null) throw new ArgumentNullException(nameof(finalizationHandler)); 235 | 236 | _finalizationHandler = finalizationHandler; 237 | return this; 238 | } 239 | 240 | /// 241 | /// Sets the given handler as the finalization handler that will be executed even if not handlers 242 | /// matched the given exception. 243 | /// 244 | /// The handler to use 245 | /// The configuration after changes 246 | /// 247 | public IHandlingConfiguration FinalizeWith( 248 | Action> finalizationHandler) 249 | { 250 | if (finalizationHandler == null) throw new ArgumentNullException(nameof(finalizationHandler)); 251 | 252 | _finalizationHandler = (e, input, result) => 253 | { 254 | finalizationHandler(e, input); 255 | }; 256 | return this; 257 | } 258 | 259 | /// 260 | /// Sets the given handler as the finalization handler that will be executed even if not handlers 261 | /// matched the given exception. 262 | /// 263 | /// The handler to use 264 | /// The configuration after changes 265 | /// 266 | public IHandlingConfiguration FinalizeWith( 267 | Action finalizationHandler) 268 | { 269 | if (finalizationHandler == null) throw new ArgumentNullException(nameof(finalizationHandler)); 270 | 271 | _finalizationHandler = (e, input, result) => 272 | { 273 | finalizationHandler(e); 274 | }; 275 | return this; 276 | } 277 | 278 | /// 279 | /// Sets the given handler as the finalization handler that will be executed even if not handlers 280 | /// matched the given exception. 281 | /// 282 | /// The handler to use 283 | /// The configuration after changes 284 | /// 285 | public IHandlingConfiguration FinalizeWith(Action finalizationHandler) 286 | { 287 | if (finalizationHandler == null) throw new ArgumentNullException(nameof(finalizationHandler)); 288 | 289 | _finalizationHandler = (e, input, result) => 290 | { 291 | finalizationHandler(); 292 | }; 293 | return this; 294 | } 295 | 296 | #endregion 297 | 298 | /// 299 | /// Passes the given exception through all the handlers until beeing successfully handled. 300 | /// 301 | /// 302 | /// Order will be preserved when running the handlers. 303 | /// 304 | /// The exception to be catched 305 | /// An optional handling parameter 306 | /// If not handled, should the exception be thrown 307 | /// The handling result 308 | /// 309 | /// 310 | public IHandlingResult Catch( 311 | Exception exception, TParameter parameter = default(TParameter), bool throwIfNotHandled = true) 312 | { 313 | if (exception == null) throw new ArgumentNullException(nameof(exception)); 314 | 315 | var input = new HandlingInput(exception, parameter); 316 | IHandlingResult result = null; 317 | try 318 | { 319 | for (var i = 0; i < _handlerCount; i++) 320 | { 321 | result = _handlers[i](exception, input); 322 | if (result.Handled) 323 | return result; 324 | } 325 | 326 | result = Handling.Ignore(); 327 | if (throwIfNotHandled) 328 | throw new NoHandlerMatchedException(exception); 329 | return result; 330 | } 331 | finally 332 | { 333 | _finalizationHandler?.Invoke(exception, input, result ?? Handling.Ignore()); 334 | } 335 | } 336 | 337 | #region Private 338 | 339 | private void AddHandler(Func, IHandlingResult> handler) 340 | { 341 | if (_handlerCount == _handlers.Length) 342 | { 343 | var newHandlerCollection = 344 | new Func, IHandlingResult>[_handlers.Length * 2]; 345 | _handlers.CopyTo(newHandlerCollection, 0); 346 | _handlers = newHandlerCollection; 347 | } 348 | 349 | _handlers[_handlerCount] = handler; 350 | ++_handlerCount; 351 | } 352 | 353 | #endregion 354 | } 355 | 356 | /// 357 | /// The exception handling configuration. 358 | /// 359 | public class HandlingConfiguration : HandlingConfiguration, IHandlingConfiguration 360 | { 361 | 362 | } 363 | } -------------------------------------------------------------------------------- /src/SimpleExceptionHandling/HandlingInput.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | // The MIT License (MIT) 3 | // 4 | // Copyright (c) 2016 João Simões 5 | // 6 | // Permission is hereby granted, free of charge, to any person obtaining a copy 7 | // of this software and associated documentation files (the "Software"), to deal 8 | // in the Software without restriction, including without limitation the rights 9 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | // copies of the Software, and to permit persons to whom the Software is 11 | // furnished to do so, subject to the following conditions: 12 | // 13 | // The above copyright notice and this permission notice shall be included in all 14 | // copies or substantial portions of the Software. 15 | // 16 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | // SOFTWARE. 23 | #endregion 24 | namespace SimpleExceptionHandling 25 | { 26 | using System; 27 | 28 | /// 29 | /// The handling input information. 30 | /// 31 | /// The parameter type 32 | public class HandlingInput : IHandlingInput 33 | { 34 | /// 35 | /// The exception to be handled 36 | /// 37 | public Exception Exception { get; } 38 | 39 | /// 40 | /// The parameter object for the handling operation 41 | /// 42 | public TParameter Parameter { get; } 43 | 44 | /// 45 | /// Creates a new instance. 46 | /// 47 | /// The exception to be handled 48 | /// An optional input parameter 49 | /// 50 | public HandlingInput(Exception exception, TParameter parameter = default(TParameter)) 51 | { 52 | if (exception == null) throw new ArgumentNullException(nameof(exception)); 53 | 54 | Exception = exception; 55 | Parameter = parameter; 56 | } 57 | } 58 | 59 | /// 60 | /// The handling input information. 61 | /// 62 | public class HandlingInput : HandlingInput, IHandlingInput 63 | { 64 | /// 65 | /// Creates a new instance. 66 | /// 67 | /// The exception to be handled 68 | /// An optional input parameter 69 | /// 70 | public HandlingInput(Exception exception, object parameter = null) : base(exception, parameter) 71 | { 72 | 73 | } 74 | } 75 | } -------------------------------------------------------------------------------- /src/SimpleExceptionHandling/HandlingResult.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | // The MIT License (MIT) 3 | // 4 | // Copyright (c) 2016 João Simões 5 | // 6 | // Permission is hereby granted, free of charge, to any person obtaining a copy 7 | // of this software and associated documentation files (the "Software"), to deal 8 | // in the Software without restriction, including without limitation the rights 9 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | // copies of the Software, and to permit persons to whom the Software is 11 | // furnished to do so, subject to the following conditions: 12 | // 13 | // The above copyright notice and this permission notice shall be included in all 14 | // copies or substantial portions of the Software. 15 | // 16 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | // SOFTWARE. 23 | #endregion 24 | namespace SimpleExceptionHandling 25 | { 26 | /// 27 | /// The handling result. 28 | /// 29 | public class HandlingResult : IHandlingResult 30 | { 31 | #region Constructors 32 | 33 | /// 34 | /// Creates a new instance. 35 | /// The property will be set to true. 36 | /// 37 | public HandlingResult() 38 | { 39 | Handled = true; 40 | } 41 | 42 | /// 43 | /// Creates a new instance. 44 | /// 45 | /// Was the exception handled? 46 | /// An optional result object 47 | public HandlingResult(bool handled, TResult result = default(TResult)) 48 | { 49 | Handled = handled; 50 | Result = result; 51 | } 52 | 53 | #endregion 54 | 55 | /// 56 | /// Was the exception handled? 57 | /// 58 | public bool Handled { get; } 59 | 60 | /// 61 | /// The result object for the handling operation 62 | /// 63 | public TResult Result { get; } 64 | } 65 | 66 | /// 67 | /// The handling result. 68 | /// 69 | public class HandlingResult : HandlingResult, IHandlingResult 70 | { 71 | #region Constructors 72 | 73 | /// 74 | /// Creates a new instance. 75 | /// The property will be set to true. 76 | /// 77 | public HandlingResult() 78 | { 79 | 80 | } 81 | 82 | /// 83 | /// Creates a new instance. 84 | /// 85 | /// Was the exception handled? 86 | /// An optional result object 87 | public HandlingResult(bool handled, object result = null) : base(handled, result) 88 | { 89 | 90 | } 91 | 92 | #endregion 93 | } 94 | } -------------------------------------------------------------------------------- /src/SimpleExceptionHandling/IHandlingConfiguration.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | // The MIT License (MIT) 3 | // 4 | // Copyright (c) 2016 João Simões 5 | // 6 | // Permission is hereby granted, free of charge, to any person obtaining a copy 7 | // of this software and associated documentation files (the "Software"), to deal 8 | // in the Software without restriction, including without limitation the rights 9 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | // copies of the Software, and to permit persons to whom the Software is 11 | // furnished to do so, subject to the following conditions: 12 | // 13 | // The above copyright notice and this permission notice shall be included in all 14 | // copies or substantial portions of the Software. 15 | // 16 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | // SOFTWARE. 23 | #endregion 24 | namespace SimpleExceptionHandling 25 | { 26 | using System; 27 | 28 | /// 29 | /// The exception handling configuration. 30 | /// 31 | /// The parameter type 32 | /// The result type 33 | public interface IHandlingConfiguration 34 | { 35 | #region On 36 | 37 | /// 38 | /// Adds the given exception handler to this configuration. If this handler matches 39 | /// a given exception on , it will be considered successfully handled. 40 | /// 41 | /// The exception type 42 | /// The handler to be added 43 | /// An optional condition to be checked if the handler must be used 44 | /// The configuration after changes 45 | /// 46 | IHandlingConfiguration On( 47 | Action> handler, 48 | Func, bool> condition = null) 49 | where TException : Exception; 50 | 51 | /// 52 | /// Adds the given exception handler to this configuration. If this handler matches 53 | /// a given exception on and true is returned, it will be considered successfully handled. 54 | /// 55 | /// The exception type 56 | /// The handler to be added 57 | /// An optional condition to be checked if the handler must be used 58 | /// The configuration after changes 59 | /// 60 | IHandlingConfiguration On( 61 | Func, bool> handler, 62 | Func, bool> condition = null) 63 | where TException : Exception; 64 | 65 | /// 66 | /// Adds the given exception handler to this configuration. If this handler matches 67 | /// a given exception on and is true, 68 | /// it will be considered successfully handled. 69 | /// 70 | /// The exception type 71 | /// The handler to be added 72 | /// An optional condition to be checked if the handler must be used 73 | /// The configuration after changes 74 | /// 75 | IHandlingConfiguration On( 76 | Func, IHandlingResult> handler, 77 | Func, bool> condition = null) 78 | where TException : Exception; 79 | 80 | /// 81 | /// Adds the given exception handler to this configuration. If this handler matches 82 | /// a given exception on , it will be considered successfully handled. 83 | /// 84 | /// The exception type 85 | /// The handler to be added 86 | /// An optional condition to be checked if the handler must be used 87 | /// The configuration after changes 88 | /// 89 | IHandlingConfiguration On( 90 | Action handler, Func, bool> condition = null) 91 | where TException : Exception; 92 | 93 | /// 94 | /// Adds the given exception handler to this configuration. If this handler matches 95 | /// a given exception on and true is returned, it will be considered successfully handled. 96 | /// 97 | /// The exception type 98 | /// The handler to be added 99 | /// An optional condition to be checked if the handler must be used 100 | /// The configuration after changes 101 | /// 102 | IHandlingConfiguration On( 103 | Func handler, Func, bool> condition = null) 104 | where TException : Exception; 105 | 106 | /// 107 | /// Adds the given exception handler to this configuration. If this handler matches 108 | /// a given exception on and is true, 109 | /// it will be considered successfully handled. 110 | /// 111 | /// The exception type 112 | /// The handler to be added 113 | /// An optional condition to be checked if the handler must be used 114 | /// The configuration after changes 115 | /// 116 | IHandlingConfiguration On( 117 | Func> handler, 118 | Func, bool> condition = null) 119 | where TException : Exception; 120 | 121 | #endregion 122 | 123 | #region FinalizeWith 124 | 125 | /// 126 | /// Sets the given handler as the finalization handler that will be executed even if not handlers 127 | /// matched the given exception. 128 | /// 129 | /// The handler to use 130 | /// The configuration after changes 131 | /// 132 | IHandlingConfiguration FinalizeWith(Action, IHandlingResult> finalizationHandler); 133 | 134 | /// 135 | /// Sets the given handler as the finalization handler that will be executed even if not handlers 136 | /// matched the given exception. 137 | /// 138 | /// The handler to use 139 | /// The configuration after changes 140 | /// 141 | IHandlingConfiguration FinalizeWith(Action> finalizationHandler); 142 | 143 | /// 144 | /// Sets the given handler as the finalization handler that will be executed even if not handlers 145 | /// matched the given exception. 146 | /// 147 | /// The handler to use 148 | /// The configuration after changes 149 | /// 150 | IHandlingConfiguration FinalizeWith(Action finalizationHandler); 151 | 152 | /// 153 | /// Sets the given handler as the finalization handler that will be executed even if not handlers 154 | /// matched the given exception. 155 | /// 156 | /// The handler to use 157 | /// The configuration after changes 158 | /// 159 | IHandlingConfiguration FinalizeWith(Action finalizationHandler); 160 | 161 | #endregion 162 | 163 | /// 164 | /// Passes the given exception through all the handlers until beeing successfully handled. 165 | /// 166 | /// 167 | /// Order will be preserved when running the handlers. 168 | /// 169 | /// The exception to be catched 170 | /// An optional handling parameter 171 | /// If not handled, should the exception be thrown 172 | /// The handling result 173 | /// 174 | IHandlingResult Catch( 175 | Exception exception, TParameter parameter = default(TParameter), bool throwIfNotHandled = true); 176 | } 177 | 178 | /// 179 | /// The exception handling configuration. 180 | /// 181 | public interface IHandlingConfiguration : IHandlingConfiguration 182 | { 183 | 184 | } 185 | } 186 | -------------------------------------------------------------------------------- /src/SimpleExceptionHandling/IHandlingInput.cs: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gravity00/SimpleExceptionHandling/9912ea2c4793d14bd4beb8895ec5d8206dc65694/src/SimpleExceptionHandling/IHandlingInput.cs -------------------------------------------------------------------------------- /src/SimpleExceptionHandling/IHandlingResult.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | // The MIT License (MIT) 3 | // 4 | // Copyright (c) 2016 João Simões 5 | // 6 | // Permission is hereby granted, free of charge, to any person obtaining a copy 7 | // of this software and associated documentation files (the "Software"), to deal 8 | // in the Software without restriction, including without limitation the rights 9 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | // copies of the Software, and to permit persons to whom the Software is 11 | // furnished to do so, subject to the following conditions: 12 | // 13 | // The above copyright notice and this permission notice shall be included in all 14 | // copies or substantial portions of the Software. 15 | // 16 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | // SOFTWARE. 23 | #endregion 24 | namespace SimpleExceptionHandling 25 | { 26 | /// 27 | /// The handling result. 28 | /// 29 | /// The result type 30 | public interface IHandlingResult 31 | { 32 | /// 33 | /// Was the exception handled? 34 | /// 35 | bool Handled { get; } 36 | 37 | /// 38 | /// The result object for the handling operation 39 | /// 40 | TResult Result { get; } 41 | } 42 | 43 | /// 44 | /// The handling result. 45 | /// 46 | public interface IHandlingResult : IHandlingResult 47 | { 48 | 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /src/SimpleExceptionHandling/NoHandlerMatchedException.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | // The MIT License (MIT) 3 | // 4 | // Copyright (c) 2016 João Simões 5 | // 6 | // Permission is hereby granted, free of charge, to any person obtaining a copy 7 | // of this software and associated documentation files (the "Software"), to deal 8 | // in the Software without restriction, including without limitation the rights 9 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | // copies of the Software, and to permit persons to whom the Software is 11 | // furnished to do so, subject to the following conditions: 12 | // 13 | // The above copyright notice and this permission notice shall be included in all 14 | // copies or substantial portions of the Software. 15 | // 16 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | // SOFTWARE. 23 | #endregion 24 | namespace SimpleExceptionHandling 25 | { 26 | using System; 27 | 28 | /// 29 | /// Exception thrown when no handlers matched 30 | /// the given exception 31 | /// 32 | public class NoHandlerMatchedException : Exception 33 | { 34 | private const string DefaultMessage = "No handler matched the exception to catch"; 35 | 36 | /// 37 | /// Creates a new instance 38 | /// 39 | /// The exception without handler to match 40 | public NoHandlerMatchedException(Exception innerException) 41 | : base(DefaultMessage, innerException) 42 | { 43 | 44 | } 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/SimpleExceptionHandling/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | #region License 2 | // The MIT License (MIT) 3 | // 4 | // Copyright (c) 2016 João Simões 5 | // 6 | // Permission is hereby granted, free of charge, to any person obtaining a copy 7 | // of this software and associated documentation files (the "Software"), to deal 8 | // in the Software without restriction, including without limitation the rights 9 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | // copies of the Software, and to permit persons to whom the Software is 11 | // furnished to do so, subject to the following conditions: 12 | // 13 | // The above copyright notice and this permission notice shall be included in all 14 | // copies or substantial portions of the Software. 15 | // 16 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | // SOFTWARE. 23 | #endregion 24 | 25 | using System; 26 | using System.Reflection; 27 | 28 | #if NET20 || NET35 || NET40 || NET45 29 | using System.Runtime.InteropServices; 30 | #endif 31 | 32 | [assembly: AssemblyTitle("SimpleExceptionHandling")] 33 | [assembly: AssemblyDescription("Library that helps developers to handle exceptions outside catch blocks.")] 34 | [assembly: AssemblyConfiguration("")] 35 | [assembly: AssemblyCompany("Net.JoaoSimoes")] 36 | [assembly: AssemblyProduct("SimpleExceptionHandling")] 37 | [assembly: AssemblyCopyright("Copyright © 2016 João Simões")] 38 | [assembly: AssemblyTrademark("")] 39 | [assembly: AssemblyCulture("")] 40 | 41 | #if NET20 || NET35 || NET40 || NET45 42 | 43 | [assembly: ComVisible(false)] 44 | [assembly: Guid("7c547964-762f-4e2a-9473-e3fb5d1adccb")] 45 | 46 | #endif 47 | 48 | [assembly: CLSCompliant(true)] 49 | 50 | [assembly: AssemblyVersion("1.1.0")] 51 | [assembly: AssemblyInformationalVersion("1.1.0")] 52 | -------------------------------------------------------------------------------- /src/SimpleExceptionHandling/SimpleExceptionHandling.xproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 14.0 5 | $(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion) 6 | 7 | 8 | 9 | 10 | 7c547964-762f-4e2a-9473-e3fb5d1adccb 11 | SimpleExceptionHandling 12 | .\obj 13 | .\bin\ 14 | v4.6.1 15 | 16 | 17 | 18 | 2.0 19 | 20 | 21 | -------------------------------------------------------------------------------- /src/SimpleExceptionHandling/project.json: -------------------------------------------------------------------------------- 1 | { 2 | "version": "1.1.0", 3 | "title": "Simple Exception Handling", 4 | "description": "Library that helps developers to handle exceptions outside catch blocks. Typical usages are global exception handlers.", 5 | "authors": [ "João Simões" ], 6 | "copyright": "Copyright © 2016 João Simões", 7 | "packOptions": { 8 | "summary": "Library that helps developers to handle exceptions outside catch blocks.", 9 | "tags": [ "exception", "handler" ], 10 | "projectUrl": "https://github.com/gravity00/SimpleExceptionHandling", 11 | "licenseUrl": "https://raw.githubusercontent.com/gravity00/SimpleExceptionHandling/master/LICENSE", 12 | "iconUrl": "https://raw.githubusercontent.com/gravity00/SimpleExceptionHandling/master/SimpleExceptionHandling-Logo.png", 13 | "releaseNotes": "Added support for more frameworks\nImplemented a finalization handler with a behaviour similar to try-finally", 14 | "owners": [ "João Simões" ], 15 | "repository": { 16 | "type": "git", 17 | "url": "https://github.com/gravity00/SimpleExceptionHandling" 18 | } 19 | }, 20 | "buildOptions": { 21 | "xmlDoc": true, 22 | "optimize": true, 23 | "keyFile": "../../tools/SimpleExceptionHandling.snk" 24 | }, 25 | 26 | "frameworks": { 27 | ".NETPortable,Version=v4.0,Profile=Profile328": { 28 | "buildOptions": { 29 | "define": [ "PORTABLE40" ] 30 | }, 31 | "frameworkAssemblies": { 32 | "mscorlib": "", 33 | "System": { "type": "build" }, 34 | "System.Core": { "type": "build" } 35 | } 36 | }, 37 | ".NETPortable,Version=v4.5,Profile=Profile111": { 38 | "buildOptions": { 39 | "define": [ "PORTABLE45" ] 40 | }, 41 | "frameworkAssemblies": { 42 | "mscorlib": "", 43 | "System": { "type": "build" }, 44 | "System.Core": { "type": "build" }, 45 | "System.Runtime": { "type": "build" } 46 | } 47 | }, 48 | "monoandroid1.0": { 49 | "frameworkAssemblies": { 50 | "mscorlib": "" 51 | } 52 | }, 53 | "monotouch1.0": { 54 | "frameworkAssemblies": { 55 | "mscorlib": "" 56 | } 57 | }, 58 | "net2.0": { 59 | "frameworkAssemblies": { 60 | "mscorlib": "" 61 | } 62 | }, 63 | "net4.0": { 64 | "frameworkAssemblies": { 65 | "mscorlib": "" 66 | } 67 | }, 68 | "net4.5": { 69 | "frameworkAssemblies": { 70 | "mscorlib": "" 71 | } 72 | }, 73 | "netcore5.0": { 74 | "dependencies": { 75 | "System.Runtime": "4.1.0" 76 | } 77 | }, 78 | "netstandard1.0": { 79 | "dependencies": { 80 | "System.Runtime": "4.1.0" 81 | } 82 | }, 83 | "wpa8.1": { 84 | "frameworkAssemblies": { 85 | "mscorlib": "", 86 | "System": { "type": "build" }, 87 | "System.Core": { "type": "build" }, 88 | "System.Runtime": { "type": "build" } 89 | } 90 | }, 91 | "xamarinios1.0": { 92 | "frameworkAssemblies": { 93 | "mscorlib": "" 94 | } 95 | }, 96 | "xamarintvos1.0": { 97 | "frameworkAssemblies": { 98 | "mscorlib": "" 99 | } 100 | } 101 | } 102 | } -------------------------------------------------------------------------------- /tools/SimpleExceptionHandling.PublicKey: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gravity00/SimpleExceptionHandling/9912ea2c4793d14bd4beb8895ec5d8206dc65694/tools/SimpleExceptionHandling.PublicKey -------------------------------------------------------------------------------- /tools/SimpleExceptionHandling.snk: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gravity00/SimpleExceptionHandling/9912ea2c4793d14bd4beb8895ec5d8206dc65694/tools/SimpleExceptionHandling.snk --------------------------------------------------------------------------------