![Page 1: Mahmoud Rafeek Alfarra Computer Programming || Chapter 2: Exception handling](https://reader035.vdocuments.net/reader035/viewer/2022062515/56649cba5503460f94981acc/html5/thumbnails/1.jpg)
Mahmoud Rafeek Alfarra
Computer Programming ||
Chapter 2: Exception handling
![Page 2: Mahmoud Rafeek Alfarra Computer Programming || Chapter 2: Exception handling](https://reader035.vdocuments.net/reader035/viewer/2022062515/56649cba5503460f94981acc/html5/thumbnails/2.jpg)
SyllabusSyllabus
Revision of OOP Exception Handling String manipulation Regular expression Files and Streams Connect applications with DBMS Streams-Based Sockets and Datagrams
www.cst.ps/staff/mfarra www.facebook.com/mahmoudRfarra
2
![Page 3: Mahmoud Rafeek Alfarra Computer Programming || Chapter 2: Exception handling](https://reader035.vdocuments.net/reader035/viewer/2022062515/56649cba5503460f94981acc/html5/thumbnails/3.jpg)
Contents
Example
Exception Classes in C#
Syntax
Exception handling
What is an exception error?
www.cst.ps/staff/mfarra www.facebook.com/mahmoudRfarra
3
Practices
User defined Exception
![Page 4: Mahmoud Rafeek Alfarra Computer Programming || Chapter 2: Exception handling](https://reader035.vdocuments.net/reader035/viewer/2022062515/56649cba5503460f94981acc/html5/thumbnails/4.jpg)
What is an exception errorWhat is an exception error ? ?
An exception is a problem that arises
during the execution of a program.
A C# exception is a response to an
exceptional situation that arises while a
program is running, such as an attempt to
divide by zero.
www.cst.ps/staff/mfarra www.facebook.com/mahmoudRfarra
4
![Page 5: Mahmoud Rafeek Alfarra Computer Programming || Chapter 2: Exception handling](https://reader035.vdocuments.net/reader035/viewer/2022062515/56649cba5503460f94981acc/html5/thumbnails/5.jpg)
What is an exception errorWhat is an exception error ? ?
Exceptions provide a way to Exceptions provide a way to transfer transfer
controlcontrol from one part of a program to from one part of a program to
another. another.
www.cst.ps/staff/mfarra www.facebook.com/mahmoudRfarra
5
![Page 6: Mahmoud Rafeek Alfarra Computer Programming || Chapter 2: Exception handling](https://reader035.vdocuments.net/reader035/viewer/2022062515/56649cba5503460f94981acc/html5/thumbnails/6.jpg)
Exception handlingException handling
C# exception handling is built upon four keywords:
try: A try block identifies a block of code for which particular exceptions will be activated.
It's followed by one or more catch blocks. catch: A program catches an exception with
an exception handler at the place in a program where you want to handle the
problem. The catch keyword indicates the catching of an exception.
www.cst.ps/staff/mfarra www.facebook.com/mahmoudRfarra
6
![Page 7: Mahmoud Rafeek Alfarra Computer Programming || Chapter 2: Exception handling](https://reader035.vdocuments.net/reader035/viewer/2022062515/56649cba5503460f94981acc/html5/thumbnails/7.jpg)
Exception handlingException handling
C# exception handling is built upon four keywords:
finally: The finally block is used to execute a given set of statements, whether an exception
is thrown or not thrown. For example, if you open a file, it must be closed
whether an exception is raised or not. throw: A program throws an exception when a
problem shows up. This is done using a throw keyword.
www.cst.ps/staff/mfarra www.facebook.com/mahmoudRfarra
7
![Page 8: Mahmoud Rafeek Alfarra Computer Programming || Chapter 2: Exception handling](https://reader035.vdocuments.net/reader035/viewer/2022062515/56649cba5503460f94981acc/html5/thumbnails/8.jpg)
Syntax
Assuming a block will raise and exception, a method catches an exception using a
combination of the try and catch keywords.
You can list down multiple catch statements to catch different type of
exceptions in case your try block raises more than one exception in different
situations.
www.cst.ps/staff/mfarra www.facebook.com/mahmoudRfarra
8
![Page 9: Mahmoud Rafeek Alfarra Computer Programming || Chapter 2: Exception handling](https://reader035.vdocuments.net/reader035/viewer/2022062515/56649cba5503460f94981acc/html5/thumbnails/9.jpg)
Syntaxwww.cst.ps/staff/mfarra www.facebook.com/mahmoudRfarra
9
![Page 10: Mahmoud Rafeek Alfarra Computer Programming || Chapter 2: Exception handling](https://reader035.vdocuments.net/reader035/viewer/2022062515/56649cba5503460f94981acc/html5/thumbnails/10.jpg)
Syntax
C# exceptions are represented by classes. The exception classes in C# are mainly
directly or indirectly derived from the System.Exception class.
Some of the exception classes derived from the System.
Exception class are the System.ApplicationException and System.SystemException classes.
www.cst.ps/staff/mfarra www.facebook.com/mahmoudRfarra
10
![Page 11: Mahmoud Rafeek Alfarra Computer Programming || Chapter 2: Exception handling](https://reader035.vdocuments.net/reader035/viewer/2022062515/56649cba5503460f94981acc/html5/thumbnails/11.jpg)
Syntax
The System.ApplicationException class supports exceptions generated by
application programs. So the exceptions defined by the
programmers should derive from this class.
The The System.SystemExceptionSystem.SystemException class is class is the base class for all predefined system the base class for all predefined system
exception. exception.
www.cst.ps/staff/mfarra www.facebook.com/mahmoudRfarra
11
![Page 12: Mahmoud Rafeek Alfarra Computer Programming || Chapter 2: Exception handling](https://reader035.vdocuments.net/reader035/viewer/2022062515/56649cba5503460f94981acc/html5/thumbnails/12.jpg)
Exception Classes in C#www.cst.ps/staff/mfarra www.facebook.com/mahmoudRfarra
12
There are another Exception classes in C#, search about them !!There are another Exception classes in C#, search about them !!
![Page 13: Mahmoud Rafeek Alfarra Computer Programming || Chapter 2: Exception handling](https://reader035.vdocuments.net/reader035/viewer/2022062515/56649cba5503460f94981acc/html5/thumbnails/13.jpg)
Example 1
1. class Program {
2. public static void division(int num1, int num2)
3. {
4. float result=0.0f;
5. try
6. {
7. result = num1 / num2;
8. }
9. catch (DivideByZeroException e)
10. {
11. Console.WriteLine("Exception Error !! \n divid by zero !!");
12. // Console.WriteLine("Exception caught: {0}", e);
13. }
14. finally
15. {
16. Console.WriteLine("Result: {0} ", result);
17. }
18. }
19. static void Main(string[] args)
20. {
21. division(10,0);
22. Console.ReadLine();
23. } } 13
www.cst.ps/staff/mfarra www.facebook.com/mahmoudRfarra
![Page 14: Mahmoud Rafeek Alfarra Computer Programming || Chapter 2: Exception handling](https://reader035.vdocuments.net/reader035/viewer/2022062515/56649cba5503460f94981acc/html5/thumbnails/14.jpg)
Example 2
1. catch (DivideByZeroException ex2)
2. {
3. // …..
4. }
5. catch (FormatException ex1)
6. {
7. //……
8. }14
www.cst.ps/staff/mfarra www.facebook.com/mahmoudRfarra
Tryex1.MessageTryex1.Message
TryException only
Instead of DivideByZeroException
OrFormatException
TryException only
Instead of DivideByZeroException
OrFormatException
![Page 15: Mahmoud Rafeek Alfarra Computer Programming || Chapter 2: Exception handling](https://reader035.vdocuments.net/reader035/viewer/2022062515/56649cba5503460f94981acc/html5/thumbnails/15.jpg)
Practicewww.cst.ps/staff/mfarra www.facebook.com/mahmoudRfarra
15
UseIndexOutOfRangeException
&FormatException
&Exception
UseIndexOutOfRangeException
&FormatException
&Exception
![Page 16: Mahmoud Rafeek Alfarra Computer Programming || Chapter 2: Exception handling](https://reader035.vdocuments.net/reader035/viewer/2022062515/56649cba5503460f94981acc/html5/thumbnails/16.jpg)
User Defined Exceptionwww.cst.ps/staff/mfarra www.facebook.com/mahmoudRfarra
16
![Page 17: Mahmoud Rafeek Alfarra Computer Programming || Chapter 2: Exception handling](https://reader035.vdocuments.net/reader035/viewer/2022062515/56649cba5503460f94981acc/html5/thumbnails/17.jpg)
User Defined Exception
17
www.cst.ps/staff/mfarra www.facebook.com/mahmoudRfarra
using System;namespace ExceptionHandling{
class NegativeNumberException:ApplicationException { public NegativeNumberException(string message) // show message }}
if(value<0) throw new NegativeNumberException(" Use Only Positive numbers");
![Page 18: Mahmoud Rafeek Alfarra Computer Programming || Chapter 2: Exception handling](https://reader035.vdocuments.net/reader035/viewer/2022062515/56649cba5503460f94981acc/html5/thumbnails/18.jpg)
Mahmoud Rafeek Alfarra