C# Exception Handling

Handling errors is an important part of programming which makes the program more user-friendly. In this tutorial, we’ll learn about how we can handle exceptions in C#.

The throw keyword

The throw keyword is used to throw errors and stops th…


This content originally appeared on DEV Community and was authored by Ishaan Sheikh

Handling errors is an important part of programming which makes the program more user-friendly. In this tutorial, we'll learn about how we can handle exceptions in C#.

The throw keyword

The throw keyword is used to throw errors and stops the further execution of a program.


bool authorized = false;
if(authorized)
{
    // Your business logic here
}
else
{
    throw new Exception("Access denied");
}

The above program throws an error as -

Unhandled exception. System.Exception: Access denied

The throw keyword can be used with many types of exception classes. For example -


if(email == null)
{
    throw new ArgumentNullException("Email cannot be null");
}

try-catch

The try-catch statement consist of a try block followed by one or more catch blocks.

try
{
    // Your code blocks
}
catch(Exception ex)
{
    throw new Exception("An error occured - " + ex);
}

try-finally

This statement consist of a try block followed by a finally block which runs always after the try block.

try
{
    // Your code here...
}
finally
{
    // This block will run regardless of the result
}

try-catch-finally

This statement consists of a try block followed by one or more catch blocks followed by a finally block which will run always regardless of the result. This is helpful in cases such as you have to free up the memory even if the program doesn't execute successfully.

try
{
    // Your main business logic
}
catch(Exception ex)
{
    throw new Exception("Error!");
}
catch(ArgumentNullException ex)
{
    throw new ArgumentNullException("Arg cannot be null");
}
...
// More catch blocks
...
finally
{
    // It will run regardless of the result
}


This content originally appeared on DEV Community and was authored by Ishaan Sheikh


Print Share Comment Cite Upload Translate Updates
APA

Ishaan Sheikh | Sciencx (2021-07-04T07:31:51+00:00) C# Exception Handling. Retrieved from https://www.scien.cx/2021/07/04/c-exception-handling/

MLA
" » C# Exception Handling." Ishaan Sheikh | Sciencx - Sunday July 4, 2021, https://www.scien.cx/2021/07/04/c-exception-handling/
HARVARD
Ishaan Sheikh | Sciencx Sunday July 4, 2021 » C# Exception Handling., viewed ,<https://www.scien.cx/2021/07/04/c-exception-handling/>
VANCOUVER
Ishaan Sheikh | Sciencx - » C# Exception Handling. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/07/04/c-exception-handling/
CHICAGO
" » C# Exception Handling." Ishaan Sheikh | Sciencx - Accessed . https://www.scien.cx/2021/07/04/c-exception-handling/
IEEE
" » C# Exception Handling." Ishaan Sheikh | Sciencx [Online]. Available: https://www.scien.cx/2021/07/04/c-exception-handling/. [Accessed: ]
rf:citation
» C# Exception Handling | Ishaan Sheikh | Sciencx | https://www.scien.cx/2021/07/04/c-exception-handling/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.