Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • C++ Data Types
  • C++ Input/Output
  • C++ Arrays
  • C++ Pointers
  • C++ OOPs
  • C++ STL
  • C++ Interview Questions
  • C++ Programs
  • C++ Cheatsheet
  • C++ MCQ
  • C++ Projects
  • C++ Exception Handling
  • C++ Memory Management
Open In App
Next Article:
ios bad() function in C++ with Examples
Next article icon

exception::what() in C++ with Examples

Last Updated : 28 May, 2020
Comments
Improve
Suggest changes
Like Article
Like
Report
The exception::what() used to get string identifying exception. This function returns a null terminated character sequence that may be used to identify the exception. Below is the syntax for the same: Header File:
  #include<exception>  
Syntax:
  virtual const char* what() const throw();  
Return: The function std::what() return a null terminated character sequence that is used to identify the exception. Note: To make use of std::what(), one should set up the appropriate try and catch blocks. Below are the programs to understand the implementation of std::what() in a better way: Program 1: CPP14
// C++ code for exception::what() #include <bits/stdc++.h>  using namespace std;  struct gfg : exception {     const char* what() const noexcept     {         return "GeeksforGeeks!! "                "A Computer Science"                " Portal For Geeks";     } };  // main method int main() {      // try block     try {         throw gfg();     }      // catch block to handle the errors     catch (exception& gfg1) {         cout << gfg1.what();     }      return 0; } 
Output:
  GeeksforGeeks!! A Computer Science Portal For Geeks  
Program 2: CPP14
// C++ code for exception::what()  #include <bits/stdc++.h>  using namespace std;  struct geeksforgeeks : exception {     const char* what() const noexcept     {         return "Hey!!";     } };  // main method int main() {      // try block     try {         throw geeksforgeeks();     }      // catch block to handle the errors     catch (exception& gfg) {         cout << gfg.what();     }      return 0; } 
Output:
  Hey!!  
Reference: http://www.cplusplus.com/reference/exception/exception/what/

Next Article
ios bad() function in C++ with Examples
author
bansal_rtk_
Improve
Article Tags :
  • Misc
  • C++
  • CPP-Functions
Practice Tags :
  • CPP
  • Misc

Similar Reads

  • exception::bad_exception in C++ with Examples
    Standard C++ contains several built-in exception classes, exception::bad_exception is one of them. This is an exception thrown by unexpected handler. Below is the syntax for the same: Header File: include<exception> Syntax: class bad_exception; Return: The exception::bad_exception returns a nu
    2 min read
  • ios operator !() function in C++ with Examples
    The operator!() method of ios class in C++ is used to any error flag of this stream is set. This includes the failbit or the badbit. Syntax: bool operator!() const; Parameters: This method does not accept any parameter. Return Value: This method returns true if any error bit is set of this stream, e
    1 min read
  • ios operator() function in C++ with Examples
    The operator() method of ios class in C++ is used to any error flag of this stream is set. This includes the failbit or the badbit. Syntax: operator void*() const; Parameters: This method does not accept any parameter. Return Value: This method returns a null pointer if any error bit is set of this
    1 min read
  • ios bad() function in C++ with Examples
    The bad() method of ios class in C++ is used to check if the stream is has raised any bad error. It means that this function will check if this stream has its badbit set. Syntax: bool bad() const; Parameters: This method does not accept any parameter. Return Value: This method returns true if the st
    1 min read
  • std::extent() template in C++ with Examples
    The std::is_const template of C++ STL is present in the <type_traits> header file. If A is an array that has a rank greater than B, the extent is the bound of the Ath dimension and if B is zero and A is the array of unknown bound, than the extent value is zero. Header File: #include<type_tr
    2 min read
  • ios good() function in C++ with Examples
    The good() method of ios class in C++ is used to check if the stream is good enough to work. It means that this function will check if this stream has raised any error or not. Syntax: bool good() const; Parameters: This method does not accept any parameter. Return Value: This method returns true if
    1 min read
  • ios fail() function in C++ with Examples
    The fail() method of ios class in C++ is used to check if the stream is has raised any fail error. It means that this function will check if this stream has its failbit set. Syntax: bool fail() const; Parameters: This method does not accept any parameter. Return Value: This method returns true if th
    1 min read
  • ios operator() function in C++11 with Examples
    The operator() method of ios class in C++11 is used to any error flag of this stream is set. This includes the failbit or the badbit. Syntax: explicit operator bool() const; Parameters: This method does not accept any parameter. Return Value: This method returns false if any error bit is set of this
    1 min read
  • ratio_not_equal() in C++ with examples
    The ratio_not_equal is an inbuilt function in C++ STL that checks if two given ratios are not equal or equal. Syntax: template < class ratio1_name, class ratio2_name > ratio_not_equal Template Parameters: The function accepts two template parameters ratio1 and ratio2 which are to be compared.
    1 min read
  • std::string::front() in C++with Examples
    It is used to access the first character from the string. It returns a reference to the first character of the string. Unlike member string::begin, which returns an iterator to this same character, this function returns a direct reference. Syntax: string str ("GeeksforGeeks"); Accessing first charac
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences