{"action":"created","comment":{"url":"https://api.github.com/repos/dotnet/core-docs/pulls/comments/68469017","id":68469017,"diff_hunk":"@@ -0,0 +1,528 @@\n+---\n+title: Handling and throwing exceptions in .NET\n+description: Understand how to use exceptions in .NET\n+keywords: .NET, .NET Core\n+author: mairaw\n+manager: wpickett\n+ms.date: 06/20/2016\n+ms.topic: article\n+ms.prod: .net-core\n+ms.technology: .net-core-technologies\n+ms.devlang: dotnet\n+ms.assetid: bf116df6-0042-46bf-be13-b69864816210\n+---\n+\n+# Handling and throwing exceptions in .NET\n+\n+Applications must be able to handle errors that occur during execution in a consistent manner. .NET provides a model for notifying applications of errors in a uniform way: .NET operations indicate failure by throwing exceptions.\n+\n+## Exceptions\n+\n+An exception is any error condition or unexpected behavior that is encountered by an executing program. Exceptions can be thrown because of a fault in your code or in code that you call (such as a shared library), unavailable operating system resources, unexpected conditions that the runtime encounters (such as code that cannot be verified), and so on. Your application can recover from some of these conditions, but not from others. Although you can recover from most application exceptions, you cannot recover from most runtime exceptions.\n+\n+In .NET, an exception is an object that inherits from the [System.Exception](https://docs.microsoft.com/dotnet/core/api/System.Exception) class. An exception is thrown from an area of code where a problem has occurred. The exception is passed up the stack until the application handles it or the program terminates.\n+\n+## Exceptions vs. traditional error-handling methods\n+\n+Traditionally, a language's error-handling model relied on either the language's unique way of detecting errors and locating handlers for them, or on the error-handling mechanism provided by the operating system. .NET implements exception handling with the following features:\n+\n+- Handles exceptions without regard for the language that generates the exception or the language that handles the exception.\n+\n+- Does not require any particular language syntax for handling exceptions, but allows each language to define its own syntax.\n+\n+- Allows exceptions to be thrown across process and even machine boundaries.\n+\n+Exceptions offer several advantages over other methods of error notification, such as return codes. Failures do not go unnoticed. Invalid values do not continue to propagate through the system. You do not have to check return codes. Finally, exception-handling code can be easily added to increase program reliability.\n+\n+## How the runtime manages exceptions\n+\n+The runtime uses an exception-handling model based on exception objects and protected blocks of code. An [Exception](https://docs.microsoft.com/dotnet/core/api/System.Exception) object is created to represent an exception when it occurs.\n+\n+The runtime creates an exception information table for each executable. Each method of the executable has an associated array of exception-handling information (which can be empty) in the exception information table. Each entry in the array describes a protected block of code, any exception filters associated with that code, and any exception handlers (**catch** statements). This exception table is extremely efficient and does not cause any performance penalty in processor time or in memory use when an exception does not occur. You use resources only when an exception occurs.\n+\n+The exception information table represents four types of exception handlers for protected blocks:\n+\n+- A **finally** handler that executes whenever the block exits, whether that occurs by normal control flow or by an unhandled exception.\n+\n+- A fault handler that must execute if an exception occurs, but does not execute on completion of normal control flow.\n+\n+- A type-filtered handler that handles any exception of a specified class or any of its derived classes.\n+\n+- A user-filtered handler that runs user-specified code to determine whether the exception should be handled by the associated handler or should be passed to the next protected block.\n+\n+Each language implements these exception handlers according to its specifications. When an exception occurs, the runtime begins a two-step process:\n+\n+1. The runtime searches the array for the first protected block that does the following:\n+\n+  - Protects a region that includes the currently executing instruction.\n+\n+  - Contains an exception handler or contains a filter that handles the exception.\n+\n+2. If a match occurs, the runtime creates an **Exception** object that describes the exception. The runtime then executes all **finally** or **fault** statements between the statement where the exception occurred and the statement that handles the exception. Note that the order of exception handlers is important; the innermost exception handler is evaluated first. Also note that exception handlers can access the local variables and local memory of the routine that catches the exception, but any intermediate values at the time the exception is thrown are lost.\n+\n+\tIf no match occurs in the current method, the runtime searches each caller of the current method, and it continues this path all the way up the stack. If no caller has a match, the runtime lets the debugger access the exception. On .NET runtimes that implement Application Domains, if the debugger does not attach to the exception, the runtime throws the [AppDomain.UnhandledException](https://msdn.microsoft.com/library/system.appdomain.unhandledexception) event. If there are no listeners for this event, the runtime dumps a stack trace and ends the application.","path":"docs/standard/exceptions.md","position":63,"original_position":63,"commit_id":"0c24b34fac37c6c9b7774d8149d6727f29c88e78","original_commit_id":"0c24b34fac37c6c9b7774d8149d6727f29c88e78","user":{"login":"richlander","id":2608468,"avatar_url":"https://avatars.githubusercontent.com/u/2608468?v=3","gravatar_id":"","url":"https://api.github.com/users/richlander","html_url":"https://github.com/richlander","followers_url":"https://api.github.com/users/richlander/followers","following_url":"https://api.github.com/users/richlander/following{/other_user}","gists_url":"https://api.github.com/users/richlander/gists{/gist_id}","starred_url":"https://api.github.com/users/richlander/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/richlander/subscriptions","organizations_url":"https://api.github.com/users/richlander/orgs","repos_url":"https://api.github.com/users/richlander/repos","events_url":"https://api.github.com/users/richlander/events{/privacy}","received_events_url":"https://api.github.com/users/richlander/received_events","type":"User","site_admin":false},"body":">  If no caller has a match, the runtime lets the debugger access the exception.\r\n\r\nIs that only if a debugger is attached?","created_at":"2016-06-24T21:57:55Z","updated_at":"2016-06-24T21:57:55Z","html_url":"https://github.com/dotnet/core-docs/pull/616#discussion_r68469017","pull_request_url":"https://api.github.com/repos/dotnet/core-docs/pulls/616","_links":{"self":{"href":"https://api.github.com/repos/dotnet/core-docs/pulls/comments/68469017"},"html":{"href":"https://github.com/dotnet/core-docs/pull/616#discussion_r68469017"},"pull_request":{"href":"https://api.github.com/repos/dotnet/core-docs/pulls/616"}}},"pull_request":{"url":"https://api.github.com/repos/dotnet/core-docs/pulls/616","id":75174680,"html_url":"https://github.com/dotnet/core-docs/pull/616","diff_url":"https://github.com/dotnet/core-docs/pull/616.diff","patch_url":"https://github.com/dotnet/core-docs/pull/616.patch","issue_url":"https://api.github.com/repos/dotnet/core-docs/issues/616","number":616,"state":"open","locked":false,"title":"Finish exception topics migration","user":{"login":"tdykstra","id":1569635,"avatar_url":"https://avatars.githubusercontent.com/u/1569635?v=3","gravatar_id":"","url":"https://api.github.com/users/tdykstra","html_url":"https://github.com/tdykstra","followers_url":"https://api.github.com/users/tdykstra/followers","following_url":"https://api.github.com/users/tdykstra/following{/other_user}","gists_url":"https://api.github.com/users/tdykstra/gists{/gist_id}","starred_url":"https://api.github.com/users/tdykstra/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tdykstra/subscriptions","organizations_url":"https://api.github.com/users/tdykstra/orgs","repos_url":"https://api.github.com/users/tdykstra/repos","events_url":"https://api.github.com/users/tdykstra/events{/privacy}","received_events_url":"https://api.github.com/users/tdykstra/received_events","type":"User","site_admin":false},"body":"","created_at":"2016-06-24T21:13:48Z","updated_at":"2016-06-24T21:57:55Z","closed_at":null,"merged_at":null,"merge_commit_sha":"8ba438f8823c6182ae33fa75da66c501013fd39c","assignee":null,"assignees":[],"milestone":null,"commits_url":"https://api.github.com/repos/dotnet/core-docs/pulls/616/commits","review_comments_url":"https://api.github.com/repos/dotnet/core-docs/pulls/616/comments","review_comment_url":"https://api.github.com/repos/dotnet/core-docs/pulls/comments{/number}","comments_url":"https://api.github.com/repos/dotnet/core-docs/issues/616/comments","statuses_url":"https://api.github.com/repos/dotnet/core-docs/statuses/0c24b34fac37c6c9b7774d8149d6727f29c88e78","head":{"label":"tdykstra:0622exc","ref":"0622exc","sha":"0c24b34fac37c6c9b7774d8149d6727f29c88e78","user":{"login":"tdykstra","id":1569635,"avatar_url":"https://avatars.githubusercontent.com/u/1569635?v=3","gravatar_id":"","url":"https://api.github.com/users/tdykstra","html_url":"https://github.com/tdykstra","followers_url":"https://api.github.com/users/tdykstra/followers","following_url":"https://api.github.com/users/tdykstra/following{/other_user}","gists_url":"https://api.github.com/users/tdykstra/gists{/gist_id}","starred_url":"https://api.github.com/users/tdykstra/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tdykstra/subscriptions","organizations_url":"https://api.github.com/users/tdykstra/orgs","repos_url":"https://api.github.com/users/tdykstra/repos","events_url":"https://api.github.com/users/tdykstra/events{/privacy}","received_events_url":"https://api.github.com/users/tdykstra/received_events","type":"User","site_admin":false},"repo":{"id":60284571,"name":"core-docs","full_name":"tdykstra/core-docs","owner":{"login":"tdykstra","id":1569635,"avatar_url":"https://avatars.githubusercontent.com/u/1569635?v=3","gravatar_id":"","url":"https://api.github.com/users/tdykstra","html_url":"https://github.com/tdykstra","followers_url":"https://api.github.com/users/tdykstra/followers","following_url":"https://api.github.com/users/tdykstra/following{/other_user}","gists_url":"https://api.github.com/users/tdykstra/gists{/gist_id}","starred_url":"https://api.github.com/users/tdykstra/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tdykstra/subscriptions","organizations_url":"https://api.github.com/users/tdykstra/orgs","repos_url":"https://api.github.com/users/tdykstra/repos","events_url":"https://api.github.com/users/tdykstra/events{/privacy}","received_events_url":"https://api.github.com/users/tdykstra/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/tdykstra/core-docs","description":".NET Core documentation (work in progress).","fork":true,"url":"https://api.github.com/repos/tdykstra/core-docs","forks_url":"https://api.github.com/repos/tdykstra/core-docs/forks","keys_url":"https://api.github.com/repos/tdykstra/core-docs/keys{/key_id}","collaborators_url":"https://api.github.com/repos/tdykstra/core-docs/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/tdykstra/core-docs/teams","hooks_url":"https://api.github.com/repos/tdykstra/core-docs/hooks","issue_events_url":"https://api.github.com/repos/tdykstra/core-docs/issues/events{/number}","events_url":"https://api.github.com/repos/tdykstra/core-docs/events","assignees_url":"https://api.github.com/repos/tdykstra/core-docs/assignees{/user}","branches_url":"https://api.github.com/repos/tdykstra/core-docs/branches{/branch}","tags_url":"https://api.github.com/repos/tdykstra/core-docs/tags","blobs_url":"https://api.github.com/repos/tdykstra/core-docs/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/tdykstra/core-docs/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/tdykstra/core-docs/git/refs{/sha}","trees_url":"https://api.github.com/repos/tdykstra/core-docs/git/trees{/sha}","statuses_url":"https://api.github.com/repos/tdykstra/core-docs/statuses/{sha}","languages_url":"https://api.github.com/repos/tdykstra/core-docs/languages","stargazers_url":"https://api.github.com/repos/tdykstra/core-docs/stargazers","contributors_url":"https://api.github.com/repos/tdykstra/core-docs/contributors","subscribers_url":"https://api.github.com/repos/tdykstra/core-docs/subscribers","subscription_url":"https://api.github.com/repos/tdykstra/core-docs/subscription","commits_url":"https://api.github.com/repos/tdykstra/core-docs/commits{/sha}","git_commits_url":"https://api.github.com/repos/tdykstra/core-docs/git/commits{/sha}","comments_url":"https://api.github.com/repos/tdykstra/core-docs/comments{/number}","issue_comment_url":"https://api.github.com/repos/tdykstra/core-docs/issues/comments{/number}","contents_url":"https://api.github.com/repos/tdykstra/core-docs/contents/{+path}","compare_url":"https://api.github.com/repos/tdykstra/core-docs/compare/{base}...{head}","merges_url":"https://api.github.com/repos/tdykstra/core-docs/merges","archive_url":"https://api.github.com/repos/tdykstra/core-docs/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/tdykstra/core-docs/downloads","issues_url":"https://api.github.com/repos/tdykstra/core-docs/issues{/number}","pulls_url":"https://api.github.com/repos/tdykstra/core-docs/pulls{/number}","milestones_url":"https://api.github.com/repos/tdykstra/core-docs/milestones{/number}","notifications_url":"https://api.github.com/repos/tdykstra/core-docs/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/tdykstra/core-docs/labels{/name}","releases_url":"https://api.github.com/repos/tdykstra/core-docs/releases{/id}","deployments_url":"https://api.github.com/repos/tdykstra/core-docs/deployments","created_at":"2016-06-02T17:40:56Z","updated_at":"2016-06-02T17:40:58Z","pushed_at":"2016-06-24T21:40:40Z","git_url":"git://github.com/tdykstra/core-docs.git","ssh_url":"git@github.com:tdykstra/core-docs.git","clone_url":"https://github.com/tdykstra/core-docs.git","svn_url":"https://github.com/tdykstra/core-docs","homepage":"http://dotnet.github.io/docs/index.html","size":15012,"stargazers_count":0,"watchers_count":0,"language":"CSS","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":1,"mirror_url":null,"open_issues_count":0,"forks":1,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"dotnet:master","ref":"master","sha":"41eaffe5704c87a593aff70ab78b239384573206","user":{"login":"dotnet","id":9141961,"avatar_url":"https://avatars.githubusercontent.com/u/9141961?v=3","gravatar_id":"","url":"https://api.github.com/users/dotnet","html_url":"https://github.com/dotnet","followers_url":"https://api.github.com/users/dotnet/followers","following_url":"https://api.github.com/users/dotnet/following{/other_user}","gists_url":"https://api.github.com/users/dotnet/gists{/gist_id}","starred_url":"https://api.github.com/users/dotnet/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dotnet/subscriptions","organizations_url":"https://api.github.com/users/dotnet/orgs","repos_url":"https://api.github.com/users/dotnet/repos","events_url":"https://api.github.com/users/dotnet/events{/privacy}","received_events_url":"https://api.github.com/users/dotnet/received_events","type":"Organization","site_admin":false},"repo":{"id":35890081,"name":"core-docs","full_name":"dotnet/core-docs","owner":{"login":"dotnet","id":9141961,"avatar_url":"https://avatars.githubusercontent.com/u/9141961?v=3","gravatar_id":"","url":"https://api.github.com/users/dotnet","html_url":"https://github.com/dotnet","followers_url":"https://api.github.com/users/dotnet/followers","following_url":"https://api.github.com/users/dotnet/following{/other_user}","gists_url":"https://api.github.com/users/dotnet/gists{/gist_id}","starred_url":"https://api.github.com/users/dotnet/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dotnet/subscriptions","organizations_url":"https://api.github.com/users/dotnet/orgs","repos_url":"https://api.github.com/users/dotnet/repos","events_url":"https://api.github.com/users/dotnet/events{/privacy}","received_events_url":"https://api.github.com/users/dotnet/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/dotnet/core-docs","description":".NET Core documentation (work in progress).","fork":false,"url":"https://api.github.com/repos/dotnet/core-docs","forks_url":"https://api.github.com/repos/dotnet/core-docs/forks","keys_url":"https://api.github.com/repos/dotnet/core-docs/keys{/key_id}","collaborators_url":"https://api.github.com/repos/dotnet/core-docs/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/dotnet/core-docs/teams","hooks_url":"https://api.github.com/repos/dotnet/core-docs/hooks","issue_events_url":"https://api.github.com/repos/dotnet/core-docs/issues/events{/number}","events_url":"https://api.github.com/repos/dotnet/core-docs/events","assignees_url":"https://api.github.com/repos/dotnet/core-docs/assignees{/user}","branches_url":"https://api.github.com/repos/dotnet/core-docs/branches{/branch}","tags_url":"https://api.github.com/repos/dotnet/core-docs/tags","blobs_url":"https://api.github.com/repos/dotnet/core-docs/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/dotnet/core-docs/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/dotnet/core-docs/git/refs{/sha}","trees_url":"https://api.github.com/repos/dotnet/core-docs/git/trees{/sha}","statuses_url":"https://api.github.com/repos/dotnet/core-docs/statuses/{sha}","languages_url":"https://api.github.com/repos/dotnet/core-docs/languages","stargazers_url":"https://api.github.com/repos/dotnet/core-docs/stargazers","contributors_url":"https://api.github.com/repos/dotnet/core-docs/contributors","subscribers_url":"https://api.github.com/repos/dotnet/core-docs/subscribers","subscription_url":"https://api.github.com/repos/dotnet/core-docs/subscription","commits_url":"https://api.github.com/repos/dotnet/core-docs/commits{/sha}","git_commits_url":"https://api.github.com/repos/dotnet/core-docs/git/commits{/sha}","comments_url":"https://api.github.com/repos/dotnet/core-docs/comments{/number}","issue_comment_url":"https://api.github.com/repos/dotnet/core-docs/issues/comments{/number}","contents_url":"https://api.github.com/repos/dotnet/core-docs/contents/{+path}","compare_url":"https://api.github.com/repos/dotnet/core-docs/compare/{base}...{head}","merges_url":"https://api.github.com/repos/dotnet/core-docs/merges","archive_url":"https://api.github.com/repos/dotnet/core-docs/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/dotnet/core-docs/downloads","issues_url":"https://api.github.com/repos/dotnet/core-docs/issues{/number}","pulls_url":"https://api.github.com/repos/dotnet/core-docs/pulls{/number}","milestones_url":"https://api.github.com/repos/dotnet/core-docs/milestones{/number}","notifications_url":"https://api.github.com/repos/dotnet/core-docs/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/dotnet/core-docs/labels{/name}","releases_url":"https://api.github.com/repos/dotnet/core-docs/releases{/id}","deployments_url":"https://api.github.com/repos/dotnet/core-docs/deployments","created_at":"2015-05-19T15:13:32Z","updated_at":"2016-06-22T07:15:15Z","pushed_at":"2016-06-24T21:54:21Z","git_url":"git://github.com/dotnet/core-docs.git","ssh_url":"git@github.com:dotnet/core-docs.git","clone_url":"https://github.com/dotnet/core-docs.git","svn_url":"https://github.com/dotnet/core-docs","homepage":"https://dotnet.github.io/docs/index.html","size":16017,"stargazers_count":113,"watchers_count":113,"language":"PowerShell","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":144,"mirror_url":null,"open_issues_count":152,"forks":144,"open_issues":152,"watchers":113,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/dotnet/core-docs/pulls/616"},"html":{"href":"https://github.com/dotnet/core-docs/pull/616"},"issue":{"href":"https://api.github.com/repos/dotnet/core-docs/issues/616"},"comments":{"href":"https://api.github.com/repos/dotnet/core-docs/issues/616/comments"},"review_comments":{"href":"https://api.github.com/repos/dotnet/core-docs/pulls/616/comments"},"review_comment":{"href":"https://api.github.com/repos/dotnet/core-docs/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/dotnet/core-docs/pulls/616/commits"},"statuses":{"href":"https://api.github.com/repos/dotnet/core-docs/statuses/0c24b34fac37c6c9b7774d8149d6727f29c88e78"}}}}
{"actor":{"display_login":"richlander"}}