C++ Exception Handling-||














































C++ Exception Handling-||




 In the previous article i have discuss about Exception Handling.If you have not gone throw you can go by clicking on the link Exception Handling .We will learn more about about Exception Handling in this article.

        Throwing Exceptions


Exceptions can be thrown anywhere within a code block using throw statement. The operand of the throw statement determines a type for the exception and can be any expression and the type of the result of the expression determines the type of exception thrown.

Example:-

 double divide1(int v1, int v1) {
   if( v2 == 0 ) {
      throw "You are dividng by zero!";
   }
   return (v1/v2);
}
      

       Catching Exceptions


The catch block following the try block catches any exception. You can specify what type of exception you want to catch and this is determined by the exception declaration that appears in parentheses following the keyword catch.

  Example:-

   try {
      // your code
    } catch( ExceptionName e ) {
      // code to handle ExceptionName exception
    }

  Example Exception Handling using try,catch and throw 

#include <iostream>
using namespace std;

double division(int v1, int v2) {
   if( v2 == 0 ) {
      throw "You are dividng by zero!";
   }
   return (v1/v2);
}

int main () {
   int a1 = 50;
   int a2 = 0;
   double a3 = 0;
 
   try {
      a3 = division(a1, a2);
      cout << a3 << endl;
   } catch (const char* massage) {
     cout << massage << endl;
   }

   return 0;
}

    Output

    You are dividng by zero!





More Articles of SAYENDEEP MAHANTY:

Name Views Likes
C++ Exception Handling-|| 86 3
C++ Exception Handling 116 3
C++ File Handling EOF 94 5
C++ File Handling Error Handling During file Operation 1135 5
C++ File Handling seekp() Function 106 5
C++ File Handling seekg() Function 105 6
C++ File Handling tellg() Function 127 5
C++ File Handling tellp() Function 133 5
C++ File Handling close() Function 78 5
C++ File Handling:: open() 107 5
C++ File Handling Introduction 131 5
C++ Container Library:: std ::array-II 102 4
Top Django Interview Questions and Answers You Need to Know 303 6
C++ Container Library::std::array 97 6
C++ Container Library Introduction 104 5
Essential Engineering Skills for Your Resume 127 5
Mistakes To Avoid During Technical Interview 133 5
Mistakes You Must Avoid During Work From Home 179 6
Seven Common Mistakes that Beginners Should Avoid While Learning to Code 109 6
Simple Linear Regression 96 6
Python wagtailmenus 123 6
Python wagtail application 120 6
Python guppy GSL 136 5
Python wagtail guppy subpackage heapy 174 6
Python guppy Introduction 215 6
Python wagtail Structural Block 258 6
Python wagtail Integrating into Django Project 370 6
Python wagtail testing 281 7
Python wagtail Basic Block 349 7
Python wagtail image tag 216 7
Python wagtail Templates 180 6
Python wagtail Backends 206 6
Python wagtail Indexing 190 6
Python wagtail Search 173 7
Python wagtail Snippets 192 6
Python wagtail Writing your own page models 165 9
Python wagtail model class PageRevision 196 10
Python wagtail model class Site 199 9
Python wagtail wagtail core model 219 9
Python wagtail page models 189 10
Python wagtail 166 11

Comments