0% found this document useful (0 votes)
2 views21 pages

Unit 9 Exception Handling

Students that study C+ language

Uploaded by

Hasan Saleh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views21 pages

Unit 9 Exception Handling

Students that study C+ language

Uploaded by

Hasan Saleh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 21

 Explain debugging

 Differentiate between compile time errors and


run-time errors.
 Exception handling in C# using try, catch and
finally blocks.
 All syntax errors will be detected and displayed by
the C# compiler and therefore these errors are as
compile-time errors.
 Whenever the compiler displays an error, it will not
create the .cs file.
 It is therefore necessary that we fix all the errors
before we can successfully compile and run the
program.
• Sometimes, a program may compile successfully creating the .exe file
but may not run properly.
• Such programs may produce wrong results due to wrong logic or may
terminate due to errors such as stack overflow.
• Most common run-time errors are:

• Dividing an integer by zero.

• Accessing an element that is out of the bounds of an array.

• Trying to store a value into an array of an incompatible class or type.

• Passing a parameter that is not in a valid range or value for a method.


• Attempting to use a negative size for an array.
• Using a null object reference as a legitimate object reference to access a
method or a variable
• Converting an invalid string to a number or vice versa.
• Accessing a character that is out of bounds of a string, and so on.
• When such errors are encountered, C# typically generates an error
message and aborts the program.
The Program displays the following output:

Division by zero
y=1

Note that the program did not stop at the point of exceptional
condition. It catches the error condition, prints the error
message and then continues the execution as if nothing has
happened.
Out Put
• Note that the array element a[2] does not exist because array a is

defined to have only two elements. a[0] and a[l].

• Therefore, the index 2 is outside the array boundary thus causing

the block catch(lndexOutOfRangeException e) to catch and

handle the error.


(1) (2)

try try
{ {
…….. …….
…… ……..
} }
finally catch (…..)
{ {
……. ……
……. …….
} }
.
.
finally
{
……..
……….
}
 This chapter includes:-

›Debugging.
›Types of errors such as compile time errors and
run-time errors.
›Exception handling in C#.
›Try and catch statements.
›Multiple catch statements.
›Finally block

You might also like