C++

Topic: Exceptions

Explain about standard exceptions with example ?

The C++ Standard library provides a base class specifically designed to declare objects to be thrown as exceptions. It is called exception and is defined in the <exception> header file under the namespace std. This class has the usual default and copy constructors, operators and destructors, plus an additional virtual member function called what that returns a null-terminated character sequence (char *) and that can be overwritten in derived classes to contain some sort of description of the exception.// standard exceptions#include <iostream>#include <exception>using namespace std;class myexception: public exception{  virtual const char* what() const throw()  {    return "My exception happened";  }} myex;int main () {  try  {    throw myex;  }  catch (exception& e)  {    cout << e.what() << endl;  }  return 0;}output:My exception happened.We have placed a handler that catches exception objects by reference (notice the ampersand & after the type), therefore this catches also classes derived from exception, like our myex object of class myexception.All exceptions thrown by components of the C++ Standard library throw exceptions derived from this std::exception class. These are:exception	descriptionbad_alloc	thrown by new on allocation failurebad_cast	thrown by dynamic_cast when fails with a referenced typebad_exception	thrown when an exception type doesn't match any catchbad_typeid	thrown by typeidios_base::failure	thrown by functions in the iostream libraryFor example, if we use the operator new and the memory cannot be allocated, an exception of type bad_alloc is thrown:try{  int * myarray= new int[1000];}catch (bad_alloc&){  cout << "Error allocating memory." << endl;}It is recommended to include all dynamic memory allocations within a try block that catches this type of exception to perform a clean action instead of an abnormal program termination, which is what happens when this type of exception is thrown and not caught. If you want to force a bad_alloc exception to see it in action, you can try to allocate a huge array; On my system, trying to allocate 1 billion ints threw a bad_alloc exception.Because bad_alloc is derived from the standard base class exception, we can handle that same exception by catching references to the exception class:// bad_alloc standard exception#include <iostream>#include <exception>using namespace std;int main () {  try  {    int* myarray= new int[1000];  }  catch (exception& e)  {    cout << "Standard exception: " << e.what() << endl;  }  return 0;}

Browse random answers:

What is Exceptions in c++ ?
Can we generate a C++ source code from the binary file?
what is Un handled exception(KERNEL32.DLL):0xE06D7363 in the context of exception handling? pls answer to my mail id.in the flg code i got a problem.i want to know the entire steps followed in exception handling in this program.#include <iostream>using namespace std;int main(){ cout << "Start"; try  { // start a try block cout << "Inside try block"; cout << "Still inside try block"; throw 78; } catch (double i)  { // catch an error cout << "Caught an exception -- value is: "; cout << i << ""; } cout << "End"; return 0;}
How does throwing and catching exceptions differ from using setjmp and longjmp?
what is the difference betwen wait() and delay()?
How can I get around scope problems in a try/catch?
Explain exception specifications with example ?
Explain about standard exceptions with example ?
Explain about try, catch, and throw Statements (C++) ?
Exceptions are hard to ignore, unlike error codes. ?
What are C++ Exception  Examples ?
Explain about Unhandled C++ Exceptions ?
Explain about exception Handling Overhead ?
Explain about Mixing C (Structured) and C++ Exceptions ?
Explain about Overview of C++ Exception Handling with example ?
Explain about Implications of Using Exceptions with example ?
Explain about Designing With Exceptions with example ?
Explain about exceptions in Real-Time Systems ?
Explain about exception Handling Philosophies with example ?
Explain about exceptions in an Array-Based Stack ?
How many exceptions in a List-Based Stack ?
What are some ways try / catch / throw can improve software quality? 
How do exceptions simplify my function return type and parameter types? 
What does it mean that exceptions separate the "good path" (or "happy path") from the "bad path"? 
Exception handling seems to make my life more difficult; clearly I'm not the problem, am I?? 
How can I handle a destructor that fails? 
How can I handle a constructor that fails? 
How should I handle resources if my constructors may throw exceptions? 
What is exception-safe code? 
Explain about exception Neutrality ?
Explain how we implement exception handling in C++  
Explain terminate() and unexpected() function  ?
Exception handling concept  ?
Describe the way to handle run time error in C++.