A Deep Dive Into C# Errors or Exceptions Handling

5
7/2/2014 A Deep Dive into C# Errors or Exceptions Handling http://www.dotnet-tricks.com/Tutorial/csharp/EJFQ291113-A-Deep-Dive-into-C 1/5 E A Deep Dive into C# Errors or Exceptions Handling Keywords : exception handling in c#, types of errors in c#, try-catch-finally in c#, proper use of try catch and finally rrors refer to the mistake or faults which occur during program development or execution. If you don't find them and correct them, they cause a program to produce wrong results. Types of Errors In programming language errors can be divided into three categories as given below- 01. Syntax Errors Syntax errors occur during development, when you make type mistake in code. For example, instead of writing while, you write WHILE then it will be a syntax error since C# is a case sensitive language. 1. bool flag= true ; 2. 3. WHILE (flag) //syntax error, since c# is case sensitive 4. { 5. //TO DO: 6. } 02. Runtime Errors (Exceptions) Runtime errors occur during execution of the program. These are also called exceptions. This can be caused due to improper user inputs, improper design logic or system errors. 1. int a = 5 , b = 0 ; 2. int result = a / b; // DivideByZeroException Exceptions can be handled by using try-catch blocks. 03. Logical Errors Logic errors occur when the program is written fine but it does not produce desired result. Logic errors are difficult to find because you need to know for sure that the result is wrong 1. int a = 5 , b = 6 ; Posted By : Shailendra Chauhan, 29 Nov 2013 Updated On : 28 Jan 2014

description

A Deep Dive Into C# Errors or Exceptions Handling

Transcript of A Deep Dive Into C# Errors or Exceptions Handling

Page 1: A Deep Dive Into C# Errors or Exceptions Handling

7/2/2014 A Deep Dive into C# Errors or Exceptions Handling

http://www.dotnet-tricks.com/Tutorial/csharp/EJFQ291113-A-Deep-Dive-into-C 1/5

E

A Deep Dive into C# Errors or Exceptions Handling

Keywords : excep t ion hand l ing in c#, t ypes o f erro rs in c#, t ry-ca t ch- fina l l y in c#,

p roper use o f t ry ca t ch and fina l l y

rrors refer to the mistake or faults which occur during program development or execution. If you don't

find them and correct them, they cause a program to produce wrong results.

Types of Errors

In programming language errors can be divided into three categories as given below-

01. Syntax Errors

Syntax errors occur during development, when you make type mistake in code. For example, instead of

writing while, you write WHILE then it will be a syntax error since C# is a case sensitive language.

1. bool flag=true;

2.

3. WHILE (flag) //syntax error, since c# is case sensitive

4. {

5. //TO DO:

6. }

02. Runtime Errors (Exceptions)

Runtime errors occur during execution of the program. These are also called exceptions. This can be

caused due to improper user inputs, improper design logic or system errors.

1. int a = 5, b = 0;

2. int result = a / b; // DivideByZeroException

Exceptions can be handled by using try-catch blocks.

03. Logical Errors

Logic errors occur when the program is written fine but it does not produce desired result. Logic errors

are difficult to find because you need to know for sure that the result is wrong

1. int a = 5, b = 6;

P o ste d By : Shaile ndra Chauhan, 29 No v 20 1 3

U pdate d On : 28 Jan 20 1 4

Page 2: A Deep Dive Into C# Errors or Exceptions Handling

7/2/2014 A Deep Dive into C# Errors or Exceptions Handling

http://www.dotnet-tricks.com/Tutorial/csharp/EJFQ291113-A-Deep-Dive-into-C 2/5

2. double avg = a + b / 2.0; // logical error, it should be (a + b) / 2.0

Exception Handling

Exception handling is a mechanism to detect and handle run time errors. It is achieved by using Try-Catch-

Finally blocks and throw keyword.

01. Try block

The try block encloses the statements that might throw an exception.

1. try

2. {

3. // Statements that can cause exception.

4. }

02. Catch block

Catch block handles any exception if one exists.

1. catch(ExceptionType e)

2. {

3. // Statements to handle exception.

4. }

03. Finally block

The finally block can be used for doing any clean-up process like releasing unused resources even if an

exception is thrown. For example, disposing database connection.

1. finally

2. {

3. // Statement to clean up.

4. }

04. Throw keyword

This keyword is used to throw an exception explicitly.

1. catch (Exception e)

2. {

3. throw (e);

4. }

Key points about exceptions handling

Page 3: A Deep Dive Into C# Errors or Exceptions Handling

7/2/2014 A Deep Dive into C# Errors or Exceptions Handling

http://www.dotnet-tricks.com/Tutorial/csharp/EJFQ291113-A-Deep-Dive-into-C 3/5

1. Exceptions are types that all directly or indirectly derive from System.Exception class.

2. Exception objects contain detailed information about the error, such as the state of the call stack

and a text description of the error.

3. A try block is used to throw multiple exceptions that can handle by using multiple catch blocks.

4. More specialized catch block should come before a generalized one. Otherwise specialized catch

block will never be executed.

5. Exceptions can be explicitly generated by a program by using the throw keyword.

6. If no exception handling mechanism is used, the program stops executing with an error message.

7. By providing a catch block without a brackets or arguments, we can catch all exceptions occurred

inside a try block.

1. Catch

2. {

3. Console.WriteLine("oException" );

4. }

8. By providing a catch block with an exception object, you can obtain more information about the

type of exception that occurred.

1.

2. catch(Exception e)

3. {

4. Console.WriteLine(e.Message);

5. }

9. The statements inside the finally block are always executed whether exception occurs or not in the

program.

10. Also, before the termination of the program finally block is always executed.

Order of Try-Catch-Finally blocks

In the order of exceptions handling blocks try block comes first, after that catch block(s) come and in the

last finally block come.

1. try

2. {

3. // statements that can cause exception.

4. }

5. catch (MoreSpecificExceptionType e1)

6. {

Page 4: A Deep Dive Into C# Errors or Exceptions Handling

7/2/2014 A Deep Dive into C# Errors or Exceptions Handling

http://www.dotnet-tricks.com/Tutorial/csharp/EJFQ291113-A-Deep-Dive-into-C 4/5

7. // error handling code

8. }

9. catch (SpecificExceptionType e2)

10. {

11. // error handling code

12. }

13. catch (GeneralExceptionType eN)

14. {

15. // error handling code

16. }

17. finally

18. {

19. // statement to clean up.

20. }

You can also skip finally block if required.

1. try

2. {

3. // statements that can cause exception.

4. }

5. catch (MoreSpecificExceptionType e1)

6. {

7. // error handling code

8. }

9. catch (SpecificExceptionType e2)

10. {

11. // error handling code

12. }

13. catch (GeneralExceptionType eN)

14. {

15. // error handling code

16. }

You can also skip catch block(s) if required.

1. try

2. {

3. // statements that can cause exception.

4. }

5. finally

6. {

Page 5: A Deep Dive Into C# Errors or Exceptions Handling

7/2/2014 A Deep Dive into C# Errors or Exceptions Handling

http://www.dotnet-tricks.com/Tutorial/csharp/EJFQ291113-A-Deep-Dive-into-C 5/5

Tweet 9in S h a r e 7

7. // statement to clean up.

8. }

Hence a combination of try-catch-finally or try-catch or try-finally blocks is valid.

What do you think?

I hope you will enjoy the exceptions handling in C# while programming. I would like to have feedback from

my blog readers. Your valuable feedback, question, or comments about this article are always welcome.

Print Article

Share this article with your friends!

About the Author

Shailendra Chauhan works as Sr. Software Engineer at reputed MNC and has more than 5 years of

hand over Microsoft .NET technologies. He is a .NET Consultant and is the founder & chief editor of

www.dotnet-tricks.com and www.dotnetinterviewtricks.com blogs. He is an author of books ASP.NET

MVC Interview Questions and Answers & LINQ Interview Questions and Answers.

He loves to work with web applications and mobile apps using Microsoft technology including ASP.NET,

MVC, C#, SQL Server, WCF, Web API, Entity Framework,Cloud Computing, Windows Azure, jQuery,

jQuery Mobile, Knockout.js, Angular.js and many more web technologies. More...