What is Exception Handling?

SyedBahadurShah 397 views 11 slides Nov 17, 2015
Slide 1
Slide 1 of 11
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11

About This Presentation

What is Exception Handling?


Slide Content

Exceptions Handling Prepared by : - S yed B ahadur S hah

What is Exceptions Handling? Exceptions provide a way to react to exceptional circumstances (like runtime errors) in programs by transferring control to special functions called  handlers . An  exception  is an event, which occurs during the execution of a program, that disrupts the normal flow of the program's instructions. Exception handling provides a standard mechanism for coding responses to run-time errors or  exceptions .

Types of Exceptions Handling? Some common types : Divide by zero errors. Accessing the elements of an array beyond its range. Invalid input. Hard disk crash. Opening a non-existing file. Heap memory exhaustion.

Exceptions Handling C++ exception handling is built upon three keywords:  try, catch, finally  and throw . 1. 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. 2. 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. 3. finally: The finally block allows you to run any cleanup-type statements that you want to execute, no matter what happens in the protected code. 4. throw : A program throws an exception when a problem shows up. This is done using a throw keyword.

Try, Catch, Finally and Throw Try, Catch and Throw Blocks : try  { throw (________); } catch  (________) {    } finally { }

How to Throw Exception? The  throw  statement requires a single argument: a throwable object. Throwable objects are instances of any subclass of the Throwable class. Here's an example of a  throw  statement. Example: try  { < Connecting to server > if (< Not Connecting to Server >) { throw(“Server not responding”); } }

How to Catch Exception? try  { < Connecting to server > if (< Not connected to server >) { throw(“ Server not responding ”); } } catch  ( string ErrorMessage ) {      cout << ErrorMessage ; }

Finally Block try  { < Connecting to server > if (< Not connected to server >) { throw(“ Server not responding ”); } } catch  ( string ErrorMessage ) {      cout << ErrorMessage ; } finally { < Connecting to server > }

Advantages of Exceptions Handling? Exception handling separates error-handling code from normal code. It clarifies the code and enhances readability. It makes for clear, robust and fault-tolerant programs.

Golden Sayings of HAZRAT ALI (A.S) Insaan Ka Apnay Dushman Sey Inteqaam Ka Sub Sey Acha Tariqa Ye He Key Wo Apni Khoobiyo’n Mein Izafa Karey “ Hazrat Ali (A.S)”

Thank you 