SlideShare a Scribd company logo
1 of 17
Hi friends
C# Tutorial
Part 41:
ERROR
www.siri-kt.blogspot.com
• Errors 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-
• 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.
• bool flag=true;
• WHILE (flag) //syntax error, since c# is case sensitive
• {
• //TO DO:
• }
• 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.
– int a = 5, b = 0;
– int result = a / b; // DivideByZeroException
• Exceptions can be handled by using try-catch
blocks.
• 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
• int a = 5, b = 6;
• 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.
• Try block
• The try block encloses the statements that might throw
an exception.
– try
– {
– // Statements that can cause exception.
– }
• the microsoft given 4 keywords for handling Runtime errors
• 1.try
• 2.catch
• 3.finally
• 4.Throw
• if we are not expect any errors in an application then we can't work with exception
handling.
• syntax:
• try
• { expected error information }
• catch
• { exception information }
• finally
• { Result(o/p) }
• throw
• { user defined exception }
• 1.try:
• in the try block we have to include all expected
exceptions statements. try block followed with cacth
&finally,catch (or)finally one try block may having
multiple catch blocks but having only one finally block.
one try block raising only one exception at a time.one try
block can executes one catch block at time
• 2.catch:
• using catch block we can provide exception
information.one catch block can handles only one
exception
• 3.finally block:
• the finally block executes which statements doesn't
• 4.throw:
• throw bloack can handle only one userdefined exception at a time. this block can handle
multiple userdefined exceptions at a time.
• example exception Handling:
• static void Main(string[] args)
• { try
• { int a, b, c;
• Console.WriteLine("enter a value");
• a=int.Parse(Console.ReadLine());
• Console.WriteLine("enter b value");
• b=int.Parse(Console.ReadLine());
• c = a / b;
• Console.WriteLine(c.ToString());
• Console.ReadLine(); }
• catch (Exception ex)
• { Console.WriteLine(ex.ToString());
• Console.ReadLine(); }
• ex2:
• 1.take a form
• 2.add 3 label,3 textboxes&one button
• source code:
• private void button1_Click(object sender, EventArgs e)
• { int a, b, res = 0;
• try
• { a = Convert.ToInt16(textBox1.Text);
• b = Convert.ToInt16(textBox2.Text);
• res = a / b; }
• catch (Exception ex)
• { MessageBox.Show(ex.Message); }
• finally
• { textBox3.Text = res.ToString(); } }
• Catch block
• Catch block handles any exception if one exists.
– catch(ExceptionType e)
– {
– // Statements to handle exception.
– }
• 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.
– finally
– {
– // Statement to clean up.
– }
– Throw keyword
– This keyword is used to throw an exception explicitly.
– catch (Exception e)
– {
– throw (e);
– }
• Key points about exceptions handling
• Exceptions are types that all directly or indirectly
derive from System.Exception class.
• Exception objects contain detailed information about
the error, such as the state of the call stack and a text
description of the error.
• A try block is used to throw multiple exceptions that can
handle by using multiple catch blocks.
• More specialized catch block should come before a
generalized one. Otherwise specialized catch block will
never be executed.
• Exceptions can be explicitly generated by a program by
using the throw keyword.
• If no exception handling mechanism is used, the program
stops executing with an error message.
• By providing a catch block without a brackets or arguments,
we can catch all exceptions occurred inside a try block.
– Catch
– { Console.WriteLine("oException" ); }
• By providing a catch block with an exception object, you can
obtain more information about the type of exception that
occurred.
– catch(Exception e)
– { Console.WriteLine(e.Message); }
• The statements inside the finally block are always executed
whether exception occurs or not in the program.
• 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.
• try
• { // statements that can cause exception. }
• catch (MoreSpecificExceptionType e1)
• { // error handling code }
• catch (SpecificExceptionType e2)
• { // error handling code }
• catch (GeneralExceptionType eN)
• { // error handling code }
• finally
• { // statement to clean up. }
You can also skip finally block if
required.
• try
• { // statements that can cause exception. }
• catch (MoreSpecificExceptionType e1)
• { // error handling code }
• catch (SpecificExceptionType e2)
• { // error handling code }
• catch (GeneralExceptionType eN)
• { // error handling code }
• You can also skip catch block(s) if required.
• try
• { // statements that can cause exception. }
• finally
• { // statement to clean up. }
• Hence a combination of try-catch-finally or try-catch or try-finally blocks is valid.
For more visit our website www.siri-kt.blogspot.com
Thanks for
Watching
More Angular JS TutorialsMore C sharp (c#) tutorials

More Related Content

What's hot

Exception handling in c
Exception handling in cException handling in c
Exception handling in cMemo Yekem
 
Presentation on-exception-handling
Presentation on-exception-handlingPresentation on-exception-handling
Presentation on-exception-handlingNahian Ahmed
 
Exception Handling In Python | Exceptions In Python | Python Programming Tuto...
Exception Handling In Python | Exceptions In Python | Python Programming Tuto...Exception Handling In Python | Exceptions In Python | Python Programming Tuto...
Exception Handling In Python | Exceptions In Python | Python Programming Tuto...Edureka!
 
Exception Handling
Exception HandlingException Handling
Exception Handlingbackdoor
 
Exceptions handling in java
Exceptions handling in javaExceptions handling in java
Exceptions handling in javajunnubabu
 
Python exception handling
Python   exception handlingPython   exception handling
Python exception handlingMohammed Sikander
 
Python Programming - X. Exception Handling and Assertions
Python Programming - X. Exception Handling and AssertionsPython Programming - X. Exception Handling and Assertions
Python Programming - X. Exception Handling and AssertionsRanel Padon
 
Error and exception in python
Error and exception in pythonError and exception in python
Error and exception in pythonjunnubabu
 
Exception Handling in object oriented programming using C++
Exception Handling in object oriented programming using C++Exception Handling in object oriented programming using C++
Exception Handling in object oriented programming using C++Janki Shah
 
What is Exception Handling?
What is Exception Handling?What is Exception Handling?
What is Exception Handling?Syed Bahadur Shah
 
Exception Handling in Python - Rik van Achterberg & Tim Muller
Exception Handling in Python - Rik van Achterberg & Tim MullerException Handling in Python - Rik van Achterberg & Tim Muller
Exception Handling in Python - Rik van Achterberg & Tim MullerByte
 
Understanding Exception Handling in .Net
Understanding Exception Handling in .NetUnderstanding Exception Handling in .Net
Understanding Exception Handling in .NetMindfire Solutions
 
Types of exceptions
Types of exceptionsTypes of exceptions
Types of exceptionsmyrajendra
 
Chapter v(error)
Chapter v(error)Chapter v(error)
Chapter v(error)Chhom Karath
 

What's hot (20)

Exception handling in c
Exception handling in cException handling in c
Exception handling in c
 
Exceptions in python
Exceptions in pythonExceptions in python
Exceptions in python
 
Presentation on-exception-handling
Presentation on-exception-handlingPresentation on-exception-handling
Presentation on-exception-handling
 
Exception handling in python
Exception handling in pythonException handling in python
Exception handling in python
 
Exception Handling In Python | Exceptions In Python | Python Programming Tuto...
Exception Handling In Python | Exceptions In Python | Python Programming Tuto...Exception Handling In Python | Exceptions In Python | Python Programming Tuto...
Exception Handling In Python | Exceptions In Python | Python Programming Tuto...
 
Exception handling
Exception handlingException handling
Exception handling
 
Exception Handling
Exception HandlingException Handling
Exception Handling
 
Exceptions handling in java
Exceptions handling in javaExceptions handling in java
Exceptions handling in java
 
Python exception handling
Python   exception handlingPython   exception handling
Python exception handling
 
Presentation1
Presentation1Presentation1
Presentation1
 
Python Programming - X. Exception Handling and Assertions
Python Programming - X. Exception Handling and AssertionsPython Programming - X. Exception Handling and Assertions
Python Programming - X. Exception Handling and Assertions
 
Error and exception in python
Error and exception in pythonError and exception in python
Error and exception in python
 
Exception Handling
Exception HandlingException Handling
Exception Handling
 
Exception Handling in object oriented programming using C++
Exception Handling in object oriented programming using C++Exception Handling in object oriented programming using C++
Exception Handling in object oriented programming using C++
 
What is Exception Handling?
What is Exception Handling?What is Exception Handling?
What is Exception Handling?
 
Exception Handling in Python - Rik van Achterberg & Tim Muller
Exception Handling in Python - Rik van Achterberg & Tim MullerException Handling in Python - Rik van Achterberg & Tim Muller
Exception Handling in Python - Rik van Achterberg & Tim Muller
 
Understanding Exception Handling in .Net
Understanding Exception Handling in .NetUnderstanding Exception Handling in .Net
Understanding Exception Handling in .Net
 
Types of exceptions
Types of exceptionsTypes of exceptions
Types of exceptions
 
C++ ala
C++ alaC++ ala
C++ ala
 
Chapter v(error)
Chapter v(error)Chapter v(error)
Chapter v(error)
 

Similar to 41c

Ch-1_5.pdf this is java tutorials for all
Ch-1_5.pdf this is java tutorials for allCh-1_5.pdf this is java tutorials for all
Ch-1_5.pdf this is java tutorials for allHayomeTakele
 
Multi catch statement
Multi catch statementMulti catch statement
Multi catch statementmyrajendra
 
UNIT III 2021R.pptx
UNIT III 2021R.pptxUNIT III 2021R.pptx
UNIT III 2021R.pptxRDeepa9
 
UNIT III 2021R.pptx
UNIT III 2021R.pptxUNIT III 2021R.pptx
UNIT III 2021R.pptxRDeepa9
 
Java class 7
Java class 7Java class 7
Java class 7Edureka!
 
Exception handling in java-PPT.pptx
Exception handling in java-PPT.pptxException handling in java-PPT.pptx
Exception handling in java-PPT.pptxsonalipatil225940
 
Exception Handling on 22nd March 2022.ppt
Exception Handling on 22nd March 2022.pptException Handling on 22nd March 2022.ppt
Exception Handling on 22nd March 2022.pptRaja Ram Dutta
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in javapriyankazope
 
Chap2 exception handling
Chap2 exception handlingChap2 exception handling
Chap2 exception handlingraksharao
 
Java-Unit 3- Chap2 exception handling
Java-Unit 3- Chap2 exception handlingJava-Unit 3- Chap2 exception handling
Java-Unit 3- Chap2 exception handlingraksharao
 
Exception handling chapter15
Exception handling chapter15Exception handling chapter15
Exception handling chapter15Kumar
 
OOPs & C++(UNIT 5)
OOPs & C++(UNIT 5)OOPs & C++(UNIT 5)
OOPs & C++(UNIT 5)SURBHI SAROHA
 
Java Exceptions and Exception Handling
 Java  Exceptions and Exception Handling Java  Exceptions and Exception Handling
Java Exceptions and Exception HandlingMaqdamYasir
 
Exception handling in java
Exception handling in java Exception handling in java
Exception handling in java yugandhar vadlamudi
 
Exceptions overview
Exceptions overviewExceptions overview
Exceptions overviewBharath K
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in javaKavitha713564
 
PROGRAMMING IN JAVA
PROGRAMMING IN JAVAPROGRAMMING IN JAVA
PROGRAMMING IN JAVASivaSankari36
 

Similar to 41c (20)

Ch-1_5.pdf this is java tutorials for all
Ch-1_5.pdf this is java tutorials for allCh-1_5.pdf this is java tutorials for all
Ch-1_5.pdf this is java tutorials for all
 
Multi catch statement
Multi catch statementMulti catch statement
Multi catch statement
 
UNIT III 2021R.pptx
UNIT III 2021R.pptxUNIT III 2021R.pptx
UNIT III 2021R.pptx
 
UNIT III 2021R.pptx
UNIT III 2021R.pptxUNIT III 2021R.pptx
UNIT III 2021R.pptx
 
Java class 7
Java class 7Java class 7
Java class 7
 
ACP - Week - 9.pptx
ACP - Week - 9.pptxACP - Week - 9.pptx
ACP - Week - 9.pptx
 
Exception handling in java-PPT.pptx
Exception handling in java-PPT.pptxException handling in java-PPT.pptx
Exception handling in java-PPT.pptx
 
Exception Handling on 22nd March 2022.ppt
Exception Handling on 22nd March 2022.pptException Handling on 22nd March 2022.ppt
Exception Handling on 22nd March 2022.ppt
 
Exception
ExceptionException
Exception
 
java.pptx
java.pptxjava.pptx
java.pptx
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in java
 
Chap2 exception handling
Chap2 exception handlingChap2 exception handling
Chap2 exception handling
 
Java-Unit 3- Chap2 exception handling
Java-Unit 3- Chap2 exception handlingJava-Unit 3- Chap2 exception handling
Java-Unit 3- Chap2 exception handling
 
Exception handling chapter15
Exception handling chapter15Exception handling chapter15
Exception handling chapter15
 
OOPs & C++(UNIT 5)
OOPs & C++(UNIT 5)OOPs & C++(UNIT 5)
OOPs & C++(UNIT 5)
 
Java Exceptions and Exception Handling
 Java  Exceptions and Exception Handling Java  Exceptions and Exception Handling
Java Exceptions and Exception Handling
 
Exception handling in java
Exception handling in java Exception handling in java
Exception handling in java
 
Exceptions overview
Exceptions overviewExceptions overview
Exceptions overview
 
Exception handling in java
Exception handling in javaException handling in java
Exception handling in java
 
PROGRAMMING IN JAVA
PROGRAMMING IN JAVAPROGRAMMING IN JAVA
PROGRAMMING IN JAVA
 

More from Sireesh K

More from Sireesh K (20)

Cn10
Cn10Cn10
Cn10
 
chanakya neeti
chanakya neetichanakya neeti
chanakya neeti
 
chanakya neeti
chanakya neetichanakya neeti
chanakya neeti
 
What is mvc
What is mvcWhat is mvc
What is mvc
 
31c
31c31c
31c
 
31cs
31cs31cs
31cs
 
45c
45c45c
45c
 
44c
44c44c
44c
 
42c
42c42c
42c
 
40c
40c40c
40c
 
39c
39c39c
39c
 
38c
38c38c
38c
 
37c
37c37c
37c
 
35c
35c35c
35c
 
34c
34c34c
34c
 
33c
33c33c
33c
 
30c
30c30c
30c
 
29c
29c29c
29c
 
27c
27c27c
27c
 
28c
28c28c
28c
 

Recently uploaded

Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceSamikshaHamane
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Jisc
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for BeginnersSabitha Banu
 
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITYISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITYKayeClaireEstoconing
 
ACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfSpandanaRallapalli
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfMr Bounab Samir
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxHumphrey A Beña
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...JhezDiaz1
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptxmary850239
 
Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parentsnavabharathschool99
 
Science 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptxScience 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptxMaryGraceBautista27
 
Q4 English4 Week3 PPT Melcnmg-based.pptx
Q4 English4 Week3 PPT Melcnmg-based.pptxQ4 English4 Week3 PPT Melcnmg-based.pptx
Q4 English4 Week3 PPT Melcnmg-based.pptxnelietumpap1
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONHumphrey A Beña
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxAnupkumar Sharma
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPCeline George
 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxDr.Ibrahim Hassaan
 

Recently uploaded (20)

Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in Pharmacovigilance
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for Beginners
 
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITYISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
 
ACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdf
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx
 
Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parents
 
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptxYOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
 
Science 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptxScience 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptx
 
Q4 English4 Week3 PPT Melcnmg-based.pptx
Q4 English4 Week3 PPT Melcnmg-based.pptxQ4 English4 Week3 PPT Melcnmg-based.pptx
Q4 English4 Week3 PPT Melcnmg-based.pptx
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERP
 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptx
 

41c

  • 1. Hi friends C# Tutorial Part 41: ERROR www.siri-kt.blogspot.com
  • 2. • Errors 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-
  • 3. • 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. • bool flag=true; • WHILE (flag) //syntax error, since c# is case sensitive • { • //TO DO: • }
  • 4. • 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. – int a = 5, b = 0; – int result = a / b; // DivideByZeroException • Exceptions can be handled by using try-catch blocks.
  • 5. • 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 • int a = 5, b = 6; • double avg = a + b / 2.0; // logical error, it should be (a + b) / 2.0
  • 6. • 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. • Try block • The try block encloses the statements that might throw an exception. – try – { – // Statements that can cause exception. – }
  • 7. • the microsoft given 4 keywords for handling Runtime errors • 1.try • 2.catch • 3.finally • 4.Throw • if we are not expect any errors in an application then we can't work with exception handling. • syntax: • try • { expected error information } • catch • { exception information } • finally • { Result(o/p) } • throw • { user defined exception }
  • 8. • 1.try: • in the try block we have to include all expected exceptions statements. try block followed with cacth &finally,catch (or)finally one try block may having multiple catch blocks but having only one finally block. one try block raising only one exception at a time.one try block can executes one catch block at time • 2.catch: • using catch block we can provide exception information.one catch block can handles only one exception • 3.finally block: • the finally block executes which statements doesn't
  • 9. • 4.throw: • throw bloack can handle only one userdefined exception at a time. this block can handle multiple userdefined exceptions at a time. • example exception Handling: • static void Main(string[] args) • { try • { int a, b, c; • Console.WriteLine("enter a value"); • a=int.Parse(Console.ReadLine()); • Console.WriteLine("enter b value"); • b=int.Parse(Console.ReadLine()); • c = a / b; • Console.WriteLine(c.ToString()); • Console.ReadLine(); } • catch (Exception ex) • { Console.WriteLine(ex.ToString()); • Console.ReadLine(); }
  • 10. • ex2: • 1.take a form • 2.add 3 label,3 textboxes&one button • source code: • private void button1_Click(object sender, EventArgs e) • { int a, b, res = 0; • try • { a = Convert.ToInt16(textBox1.Text); • b = Convert.ToInt16(textBox2.Text); • res = a / b; } • catch (Exception ex) • { MessageBox.Show(ex.Message); } • finally • { textBox3.Text = res.ToString(); } }
  • 11. • Catch block • Catch block handles any exception if one exists. – catch(ExceptionType e) – { – // Statements to handle exception. – } • 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. – finally – { – // Statement to clean up. – }
  • 12. – Throw keyword – This keyword is used to throw an exception explicitly. – catch (Exception e) – { – throw (e); – }
  • 13. • Key points about exceptions handling • Exceptions are types that all directly or indirectly derive from System.Exception class. • Exception objects contain detailed information about the error, such as the state of the call stack and a text description of the error. • A try block is used to throw multiple exceptions that can handle by using multiple catch blocks. • More specialized catch block should come before a generalized one. Otherwise specialized catch block will never be executed. • Exceptions can be explicitly generated by a program by using the throw keyword.
  • 14. • If no exception handling mechanism is used, the program stops executing with an error message. • By providing a catch block without a brackets or arguments, we can catch all exceptions occurred inside a try block. – Catch – { Console.WriteLine("oException" ); } • By providing a catch block with an exception object, you can obtain more information about the type of exception that occurred. – catch(Exception e) – { Console.WriteLine(e.Message); } • The statements inside the finally block are always executed whether exception occurs or not in the program. • Also, before the termination of the program finally block is always executed.
  • 15. 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. • try • { // statements that can cause exception. } • catch (MoreSpecificExceptionType e1) • { // error handling code } • catch (SpecificExceptionType e2) • { // error handling code } • catch (GeneralExceptionType eN) • { // error handling code } • finally • { // statement to clean up. }
  • 16. You can also skip finally block if required. • try • { // statements that can cause exception. } • catch (MoreSpecificExceptionType e1) • { // error handling code } • catch (SpecificExceptionType e2) • { // error handling code } • catch (GeneralExceptionType eN) • { // error handling code } • You can also skip catch block(s) if required. • try • { // statements that can cause exception. } • finally • { // statement to clean up. } • Hence a combination of try-catch-finally or try-catch or try-finally blocks is valid.
  • 17. For more visit our website www.siri-kt.blogspot.com Thanks for Watching More Angular JS TutorialsMore C sharp (c#) tutorials