
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to throw a C++ exception?
Exception handling is used to handle the exceptions. We can use try catch block to protect the code. Exception can be thrown anywhere within the code block. The keyword “throw” is used to throw an exception.
Here is an example of throw in C++ language,
Example
#include <iostream> using namespace std; int display(int x, int y) { if( y == 0 ) { throw "Division by zero condition!"; } return (x/y); } int main () { int a = 50; int b = 0; int c = 0; try { c = display(a, b); cout << c << endl; } catch (const char* msg) { cerr << msg << endl; } return 0; }
Output
Here is the output
Division by zero condition!
- Related Questions & Answers
- How to throw custom exception in Kotlin?
- Throw Custom Exception in Kotlin
- How to throw an exception from a static block in Java?
- Can a constructor throw an exception in Java?
- While chaining, can we throw unchecked exception from a checked exception in java?
- toDataURL throw Uncaught Security exception in HTML
- How do I manually throw/raise an exception in Python?
- Is it possible to throw exception without using "throws Exception" in java?
- How can I get a JavaScript stack trace when I throw an exception?
- How do you throw an Exception without breaking a for loop in java?
- Can we throw an Unchecked Exception from a static block in java?
- What is the difference between throw e and throw new Exception(e) in catch block in java?
- While overriding can the subclass choose not to throw an exception in java?
- Can the abstract methods of an interface throw an exception in java?
- How to raise Python exception from a C extension?
Advertisements