BY- C.M.SRI SIVAGAMA SUNDARI
Exception handling in c++
EXCEPTION HANDLING  Introduction  Basic of Exception Handling  Its Mechanism  Throwing and Catching Mechanism  Rethrowing an Exception  Specifying Exception
Program:1 #include<iostream> using namespace std; int main() { int a=50,b=0,c; c=a/b; cout<<c<<endl; return 0; } Output: Process returned -1073741676 NOTE: Error occur because of Logical Errors and Syntactic Errors. But exception is because of RUNTIME ERROR, INVALID INPUT ETC..
INTRODUCTION The process of responding to unwanted or unexpected events when a computer program runs. Exceptions are run time anomalies or unusual conditions that a program may encounter while executing detect and handle exceptions which are basically run time errors It provides a type-safe, integrated approach, for coping with the unusual predictable problems that arise while executing a program Exceptions are of two kinds, namely, synchronous exceptions and asynchronous exceptions.
BASIC EXCEPTION HANDLING AND ITS MECHANISMS: 1. FIND THE PROBLEM (HIT THE EXCEPTION) 2. INFROM THAT AN ERROR HAS OCCURRED (THROW THE EXCEPTION) 3. RECEIVE THE ERROR INFORMATION (CATCH THE EXCEPTION) 4. TAKE CORRECTION ACTIONS (HANDLE THE EXCEPTION) Exception handling mechanisms are of three keywords namely try , throw and catch TRY - GENERATE EXCEPTIONS THROW - DECTECT AND THROW THE EXCEPTIONS CATCH - CATCH THE EXCEPTION AND HANDLE THE ERROR
CATCH BLOCK CATCH AND HANDLING THE EXCEPTIONS Exception objects
try { // Block of code to try throw exception; // Throw an exception when a problem arise } catch () { // Block of code to handle errors } SYNTAX:
Program 2: #include<iostream> Using namespace std; int main() { int nume,deno,div; cout<<“N Enter numerator and denominator:” cin>>nume>>deno; try { if(deno==0) throw 0; else { div=nume/deno; cout<<“n BY DIVIDING”<<nume<<“and”<<deno<<“answer is:”<<div; } } catch(int num_exception) { cout<<“ n ERROR:CANNOT DIVIDE BY”<<num_exception<<endl; } return 0; }
OUTPUT: Enter numerator and denominator: 70 0 ERROR : CANNOT DIVIDE BY 0 OUTPUT: Enter numerator and denominator: 70 7 BY DIVIDING 70 AND 7 ANSWER IS 10 throw point function that causes an exception Try block Invokes a functions that contain an exception cCatch block Catches and handles the exception FUNCTION INVOKED BYTRY BLOCKTHROWING EXCEPTIONS:
THROWING AND CATCHING MECHANISM: throw(exception); throw exceptions; throw; // used for rethrowing an exception Catch(type arg) { //statement block for managing exception } MULTIPLE CATCHING STATEMENTS: Try { //try block } catch(type1 arg) { //catchblock1 } catch(type2 arg) { //catchblock2 } ---------- ----------
Program 4: #include<iostream> Using namespace std; Void test(int x) { try { if(x==1) throw x; else { if(x==0) throw ‘x’; else { if(x== -1) throw 2.5; } } } Catch(char c) { cout<<“caught a character “; } catch(int x) { cout<<“caught a integer”; } catch(float d) { cout<<“caught a floating value”; } }
Special case: Try { throw; } catch(…) { cout<<“error”; } int main() { cout<<“n testing multiple catches”; test(1); test(0); test(-1); return 0; } OUTPUT: Testing multiple catches Caught an integer Caught a character Caught a floating value
Specifying exceptions : Syntax: Data_type fun_name(arg list) throw (type list) { -------- -------- fn. Body -------- } Program 5: #include<iostream> Using namespace std; Void test(int x) throw(int, float, char) { try { if(x==1) throw x; else { if(x==0) throw ‘x’; else { if(x== -1) throw 2.5; } } }
Rethrowing an exception: Program: #include<iostream> Using namespace std; Void func(int x,int y) { cout<<“INSIDE Fn.”; try { if(y==0) throw y; else cout<<“divide:”<<x/ y<<“n”; } catch(int a) { cout<<“caught inside fn”; throw; } } int main() { cout<<“inside main”; try { func(10,2); func(10,0); } catch(int) { cout<<“caught integer inside main”; } cout<<“end of main”; return 0; }
Project on c++ programming. Bsc.cs students

Project on c++ programming. Bsc.cs students

  • 1.
  • 2.
  • 3.
    EXCEPTION HANDLING  Introduction Basic of Exception Handling  Its Mechanism  Throwing and Catching Mechanism  Rethrowing an Exception  Specifying Exception
  • 4.
    Program:1 #include<iostream> using namespace std; intmain() { int a=50,b=0,c; c=a/b; cout<<c<<endl; return 0; } Output: Process returned -1073741676 NOTE: Error occur because of Logical Errors and Syntactic Errors. But exception is because of RUNTIME ERROR, INVALID INPUT ETC..
  • 5.
    INTRODUCTION The process ofresponding to unwanted or unexpected events when a computer program runs. Exceptions are run time anomalies or unusual conditions that a program may encounter while executing detect and handle exceptions which are basically run time errors It provides a type-safe, integrated approach, for coping with the unusual predictable problems that arise while executing a program Exceptions are of two kinds, namely, synchronous exceptions and asynchronous exceptions.
  • 6.
    BASIC EXCEPTION HANDLINGAND ITS MECHANISMS: 1. FIND THE PROBLEM (HIT THE EXCEPTION) 2. INFROM THAT AN ERROR HAS OCCURRED (THROW THE EXCEPTION) 3. RECEIVE THE ERROR INFORMATION (CATCH THE EXCEPTION) 4. TAKE CORRECTION ACTIONS (HANDLE THE EXCEPTION) Exception handling mechanisms are of three keywords namely try , throw and catch TRY - GENERATE EXCEPTIONS THROW - DECTECT AND THROW THE EXCEPTIONS CATCH - CATCH THE EXCEPTION AND HANDLE THE ERROR
  • 7.
    CATCH BLOCK CATCH ANDHANDLING THE EXCEPTIONS Exception objects
  • 8.
    try { // Blockof code to try throw exception; // Throw an exception when a problem arise } catch () { // Block of code to handle errors } SYNTAX:
  • 9.
    Program 2: #include<iostream> Using namespacestd; int main() { int nume,deno,div; cout<<“N Enter numerator and denominator:” cin>>nume>>deno; try { if(deno==0) throw 0; else { div=nume/deno; cout<<“n BY DIVIDING”<<nume<<“and”<<deno<<“answer is:”<<div; } } catch(int num_exception) { cout<<“ n ERROR:CANNOT DIVIDE BY”<<num_exception<<endl; } return 0; }
  • 10.
    OUTPUT: Enter numerator anddenominator: 70 0 ERROR : CANNOT DIVIDE BY 0 OUTPUT: Enter numerator and denominator: 70 7 BY DIVIDING 70 AND 7 ANSWER IS 10 throw point function that causes an exception Try block Invokes a functions that contain an exception cCatch block Catches and handles the exception FUNCTION INVOKED BYTRY BLOCKTHROWING EXCEPTIONS:
  • 11.
    THROWING AND CATCHINGMECHANISM: throw(exception); throw exceptions; throw; // used for rethrowing an exception Catch(type arg) { //statement block for managing exception } MULTIPLE CATCHING STATEMENTS: Try { //try block } catch(type1 arg) { //catchblock1 } catch(type2 arg) { //catchblock2 } ---------- ----------
  • 12.
    Program 4: #include<iostream> Using namespacestd; Void test(int x) { try { if(x==1) throw x; else { if(x==0) throw ‘x’; else { if(x== -1) throw 2.5; } } } Catch(char c) { cout<<“caught a character “; } catch(int x) { cout<<“caught a integer”; } catch(float d) { cout<<“caught a floating value”; } }
  • 13.
    Special case: Try { throw; } catch(…) { cout<<“error”; } int main() { cout<<“ntesting multiple catches”; test(1); test(0); test(-1); return 0; } OUTPUT: Testing multiple catches Caught an integer Caught a character Caught a floating value
  • 14.
    Specifying exceptions : Syntax: Data_typefun_name(arg list) throw (type list) { -------- -------- fn. Body -------- } Program 5: #include<iostream> Using namespace std; Void test(int x) throw(int, float, char) { try { if(x==1) throw x; else { if(x==0) throw ‘x’; else { if(x== -1) throw 2.5; } } }
  • 15.
    Rethrowing an exception: Program: #include<iostream> Usingnamespace std; Void func(int x,int y) { cout<<“INSIDE Fn.”; try { if(y==0) throw y; else cout<<“divide:”<<x/ y<<“n”; } catch(int a) { cout<<“caught inside fn”; throw; } } int main() { cout<<“inside main”; try { func(10,2); func(10,0); } catch(int) { cout<<“caught integer inside main”; } cout<<“end of main”; return 0; }