SlideShare a Scribd company logo
1 of 3
catch blocks (C++ only)

catch block syntax

>>-catch--(--exception_declaration--)--{--statements--}--------><

You can declare a handler to catch many types of exceptions. The allowable objects that a
function can catch are declared in the parentheses following the catch keyword (the
exception_declaration). You can catch objects of the fundamental types, base and derived class
objects, references, and pointers to all of these types. You can also catch const and volatile
types. The exception_declaration cannot be an incomplete type, or a reference or pointer to an
incomplete type other than one of the following:

       void*
       const void*
       volatile void*
       const volatile void*

You cannot define a type in an exception_declaration.

You can also use the catch(...) form of the handler to catch all thrown exceptions that have
not been caught by a previous catch block. The ellipsis in the catch argument indicates that any
exception thrown can be handled by this handler.

If an exception is caught by a catch(...) block, there is no direct way to access the object
thrown. Information about an exception caught by catch(...) is very limited.

You can declare an optional variable name if you want to access the thrown object in the catch
block.

A catch block can only catch accessible objects. The object caught must have an accessible copy
constructor.


throw expressions (C++ only)
You use a throw expression to indicate that your program has encountered an exception.



throw expression syntax

>>-throw--+-----------------------+----------------------------><
          '-assignment_expression-'
The type of assignment_expression cannot be an incomplete type, or a pointer to an incomplete
type other than one of the following:

       void*
       const void*
       volatile void*
       const volatile void*

The assignment_expression is treated the same way as a function argument in a call or the
operand of a return statement.

If the assignment_expression is a class object, the copy constructor and destructor of that object
must be accessible. For example, you cannot throw a class object that has its copy constructor
declared as private.


Rethrowing an exception (C++ only)
If a catch block cannot handle the particular exception it has caught, you can rethrow the
exception. The rethrow expression (throw without assignment_expression) causes the
originally thrown object to be rethrown.

Because the exception has already been caught at the scope in which the rethrow expression
occurs, it is rethrown out to the next dynamically enclosing try block. Therefore, it cannot be
handled by catch blocks at the scope in which the rethrow expression occurred. Any catch blocks
for the dynamically enclosing try block have an opportunity to catch the exception.

The following example demonstrates rethrowing an exception:

#include <iostream>
using namespace std;

struct E {
const char* message;
E() : message("Class E") { }
};

struct E1 : E {
const char* message;
E1() : message("Class E1") { }
};

struct E2 : E {
const char* message;
E2() : message("Class E2") { }
};

void f() {
try {
cout<< "In try block of f()" <<endl;
cout<< "Throwing exception of type E1" <<endl;
    E1 myException;
throwmyException;
  }
catch (E2& e) {
cout<< "In handler of f(), catch (E2& e)" <<endl;
cout<< "Exception: " <<e.message<<endl;
throw;
  }
catch (E1& e) {
cout<< "In handler of f(), catch (E1& e)" <<endl;
cout<< "Exception: " <<e.message<<endl;
throw;
  }
catch (E& e) {
cout<< "In handler of f(), catch (E& e)" <<endl;
cout<< "Exception: " <<e.message<<endl;
throw;
  }
}

int main() {
try {
cout<< "In try block of main()" <<endl;
f();
  }
catch (E2& e) {
cout<< "In handler of main(), catch (E2& e)" <<endl;
cout<< "Exception: " <<e.message<<endl;
  }
catch (...) {
cout<< "In handler of main(), catch (...)" <<endl;
  }
}
The following is the output of the above example:

In try block of main()
In try block of f()
Throwing exception of type E1
In handler of f(), catch (E1& e)
Exception: Class E1
In handler of main(), catch (...)
The try block in the main() function calls function f(). The try block in function f() throws an object
of type E1 named myException. The handler catch (E1 &e) catches myException. The
handler then rethrowsmyException with the statement throw to the next dynamically enclosing try
block: the try block in the main() function. The handler catch(...) catches myException.

More Related Content

What's hot

Clean Code: Chapter 3 Function
Clean Code: Chapter 3 FunctionClean Code: Chapter 3 Function
Clean Code: Chapter 3 FunctionKent Huang
 
Scala 5 Concepts and Pitfals
Scala 5 Concepts and PitfalsScala 5 Concepts and Pitfals
Scala 5 Concepts and PitfalsTomer Ben David
 
Rethrowing exception- JAVA
Rethrowing exception- JAVARethrowing exception- JAVA
Rethrowing exception- JAVARajan Shah
 
Practical JavaScript Programming - Session 6/8
Practical JavaScript Programming - Session 6/8Practical JavaScript Programming - Session 6/8
Practical JavaScript Programming - Session 6/8Wilson Su
 
شرح مقرر البرمجة 2 لغة جافا - الوحدة الثالثة
شرح مقرر البرمجة 2   لغة جافا - الوحدة الثالثةشرح مقرر البرمجة 2   لغة جافا - الوحدة الثالثة
شرح مقرر البرمجة 2 لغة جافا - الوحدة الثالثةجامعة القدس المفتوحة
 
C++ Expression Templateを使って式をコンパイル時に微分
C++ Expression Templateを使って式をコンパイル時に微分C++ Expression Templateを使って式をコンパイル時に微分
C++ Expression Templateを使って式をコンパイル時に微分masahiro nishiba
 
Advanced php
Advanced phpAdvanced php
Advanced phpAnne Lee
 
Александр Трищенко: PHP 7 Evolution
Александр Трищенко: PHP 7 EvolutionАлександр Трищенко: PHP 7 Evolution
Александр Трищенко: PHP 7 EvolutionOleg Poludnenko
 
شرح مقرر البرمجة 2 لغة جافا - الوحدة الرابعة
شرح مقرر البرمجة 2   لغة جافا - الوحدة الرابعةشرح مقرر البرمجة 2   لغة جافا - الوحدة الرابعة
شرح مقرر البرمجة 2 لغة جافا - الوحدة الرابعةجامعة القدس المفتوحة
 
Refactoring group 1 - chapter 3,4,6
Refactoring   group 1 - chapter 3,4,6Refactoring   group 1 - chapter 3,4,6
Refactoring group 1 - chapter 3,4,6Duy Lâm
 
Flashback, el primer malware masivo de sistemas Mac
Flashback, el primer malware masivo de sistemas MacFlashback, el primer malware masivo de sistemas Mac
Flashback, el primer malware masivo de sistemas MacESET Latinoamérica
 
Web Application Development using PHP Chapter 2
Web Application Development using PHP Chapter 2Web Application Development using PHP Chapter 2
Web Application Development using PHP Chapter 2Mohd Harris Ahmad Jaal
 
[KOSSA] C++ Programming - 17th Study - STL #3
[KOSSA] C++ Programming - 17th Study - STL #3[KOSSA] C++ Programming - 17th Study - STL #3
[KOSSA] C++ Programming - 17th Study - STL #3Seok-joon Yun
 
Checking Wine with PVS-Studio and Clang Static Analyzer
Checking Wine with PVS-Studio and Clang Static AnalyzerChecking Wine with PVS-Studio and Clang Static Analyzer
Checking Wine with PVS-Studio and Clang Static AnalyzerAndrey Karpov
 

What's hot (20)

Constructor,destructors cpp
Constructor,destructors cppConstructor,destructors cpp
Constructor,destructors cpp
 
Clean Code: Chapter 3 Function
Clean Code: Chapter 3 FunctionClean Code: Chapter 3 Function
Clean Code: Chapter 3 Function
 
Effective Modern C++
Effective Modern C++Effective Modern C++
Effective Modern C++
 
Unix open
Unix openUnix open
Unix open
 
Scala 5 Concepts and Pitfals
Scala 5 Concepts and PitfalsScala 5 Concepts and Pitfals
Scala 5 Concepts and Pitfals
 
Rethrowing exception- JAVA
Rethrowing exception- JAVARethrowing exception- JAVA
Rethrowing exception- JAVA
 
Php & my sql
Php & my sqlPhp & my sql
Php & my sql
 
Practical JavaScript Programming - Session 6/8
Practical JavaScript Programming - Session 6/8Practical JavaScript Programming - Session 6/8
Practical JavaScript Programming - Session 6/8
 
شرح مقرر البرمجة 2 لغة جافا - الوحدة الثالثة
شرح مقرر البرمجة 2   لغة جافا - الوحدة الثالثةشرح مقرر البرمجة 2   لغة جافا - الوحدة الثالثة
شرح مقرر البرمجة 2 لغة جافا - الوحدة الثالثة
 
C++ Expression Templateを使って式をコンパイル時に微分
C++ Expression Templateを使って式をコンパイル時に微分C++ Expression Templateを使って式をコンパイル時に微分
C++ Expression Templateを使って式をコンパイル時に微分
 
Advanced php
Advanced phpAdvanced php
Advanced php
 
Александр Трищенко: PHP 7 Evolution
Александр Трищенко: PHP 7 EvolutionАлександр Трищенко: PHP 7 Evolution
Александр Трищенко: PHP 7 Evolution
 
Modern PHP
Modern PHPModern PHP
Modern PHP
 
شرح مقرر البرمجة 2 لغة جافا - الوحدة الرابعة
شرح مقرر البرمجة 2   لغة جافا - الوحدة الرابعةشرح مقرر البرمجة 2   لغة جافا - الوحدة الرابعة
شرح مقرر البرمجة 2 لغة جافا - الوحدة الرابعة
 
Refactoring group 1 - chapter 3,4,6
Refactoring   group 1 - chapter 3,4,6Refactoring   group 1 - chapter 3,4,6
Refactoring group 1 - chapter 3,4,6
 
Flashback, el primer malware masivo de sistemas Mac
Flashback, el primer malware masivo de sistemas MacFlashback, el primer malware masivo de sistemas Mac
Flashback, el primer malware masivo de sistemas Mac
 
Web Application Development using PHP Chapter 2
Web Application Development using PHP Chapter 2Web Application Development using PHP Chapter 2
Web Application Development using PHP Chapter 2
 
[KOSSA] C++ Programming - 17th Study - STL #3
[KOSSA] C++ Programming - 17th Study - STL #3[KOSSA] C++ Programming - 17th Study - STL #3
[KOSSA] C++ Programming - 17th Study - STL #3
 
Php variables
Php variablesPhp variables
Php variables
 
Checking Wine with PVS-Studio and Clang Static Analyzer
Checking Wine with PVS-Studio and Clang Static AnalyzerChecking Wine with PVS-Studio and Clang Static Analyzer
Checking Wine with PVS-Studio and Clang Static Analyzer
 

Viewers also liked

Namespace--defining same identifiers again
Namespace--defining same identifiers againNamespace--defining same identifiers again
Namespace--defining same identifiers againAjay Chimmani
 
Virtual memory
Virtual memoryVirtual memory
Virtual memoryMohd Arif
 
Week7 exception handling
Week7 exception handlingWeek7 exception handling
Week7 exception handlingAlpesh Oza
 
Exception Handling
Exception HandlingException Handling
Exception HandlingAlpesh Oza
 
Chapter 9 - Virtual Memory
Chapter 9 - Virtual MemoryChapter 9 - Virtual Memory
Chapter 9 - Virtual MemoryWayne Jones Jnr
 
Virtual Memory and Paging
Virtual Memory and PagingVirtual Memory and Paging
Virtual Memory and PagingEmery Berger
 

Viewers also liked (9)

Namespace--defining same identifiers again
Namespace--defining same identifiers againNamespace--defining same identifiers again
Namespace--defining same identifiers again
 
Virtual memory
Virtual memoryVirtual memory
Virtual memory
 
Week7 exception handling
Week7 exception handlingWeek7 exception handling
Week7 exception handling
 
Virtual memory 20070222-en
Virtual memory 20070222-enVirtual memory 20070222-en
Virtual memory 20070222-en
 
F6dc1 session6 c++
F6dc1 session6 c++F6dc1 session6 c++
F6dc1 session6 c++
 
Exception Handling
Exception HandlingException Handling
Exception Handling
 
Chapter 9 - Virtual Memory
Chapter 9 - Virtual MemoryChapter 9 - Virtual Memory
Chapter 9 - Virtual Memory
 
Cache memory presentation
Cache memory presentationCache memory presentation
Cache memory presentation
 
Virtual Memory and Paging
Virtual Memory and PagingVirtual Memory and Paging
Virtual Memory and Paging
 

Similar to C++ catch blocks and throw expressions

6-Exception Handling and Templates.pptx
6-Exception Handling and Templates.pptx6-Exception Handling and Templates.pptx
6-Exception Handling and Templates.pptxSatyamMishra237306
 
Exceptions ref
Exceptions refExceptions ref
Exceptions ref. .
 
Exception Handling1
Exception Handling1Exception Handling1
Exception Handling1guest739536
 
Exception handling
Exception handlingException handling
Exception handlingWaqas Abbasi
 
Exception handling and templates
Exception handling and templatesException handling and templates
Exception handling and templatesfarhan amjad
 
Exceptions and Exception Handling in C++
Exceptions and Exception Handling in C++Exceptions and Exception Handling in C++
Exceptions and Exception Handling in C++IRJET Journal
 
Welcome to Modern C++
Welcome to Modern C++Welcome to Modern C++
Welcome to Modern C++Seok-joon Yun
 
Exception Handling
Exception HandlingException Handling
Exception HandlingReddhi Basu
 
Error and exception in python
Error and exception in pythonError and exception in python
Error and exception in pythonjunnubabu
 
Exception Handling
Exception HandlingException Handling
Exception Handlingbackdoor
 
Asterisk: PVS-Studio Takes Up Telephony
Asterisk: PVS-Studio Takes Up TelephonyAsterisk: PVS-Studio Takes Up Telephony
Asterisk: PVS-Studio Takes Up TelephonyAndrey Karpov
 
Unit II Java & J2EE regarding Java application development
Unit II Java & J2EE regarding Java application developmentUnit II Java & J2EE regarding Java application development
Unit II Java & J2EE regarding Java application developmentrohitgudasi18
 

Similar to C++ catch blocks and throw expressions (20)

6-Exception Handling and Templates.pptx
6-Exception Handling and Templates.pptx6-Exception Handling and Templates.pptx
6-Exception Handling and Templates.pptx
 
Exceptions ref
Exceptions refExceptions ref
Exceptions ref
 
Exception Handling1
Exception Handling1Exception Handling1
Exception Handling1
 
Templates
TemplatesTemplates
Templates
 
Exception handling
Exception handlingException handling
Exception handling
 
Exception handling and templates
Exception handling and templatesException handling and templates
Exception handling and templates
 
Exceptions and Exception Handling in C++
Exceptions and Exception Handling in C++Exceptions and Exception Handling in C++
Exceptions and Exception Handling in C++
 
Welcome to Modern C++
Welcome to Modern C++Welcome to Modern C++
Welcome to Modern C++
 
Java unit3
Java unit3Java unit3
Java unit3
 
Exception Handling
Exception HandlingException Handling
Exception Handling
 
Exception handling in Java
Exception handling in JavaException handling in Java
Exception handling in Java
 
Exception handling
Exception handlingException handling
Exception handling
 
Exception handling
Exception handlingException handling
Exception handling
 
Error and exception in python
Error and exception in pythonError and exception in python
Error and exception in python
 
Exception Handling
Exception HandlingException Handling
Exception Handling
 
Exception
ExceptionException
Exception
 
Exceptions
ExceptionsExceptions
Exceptions
 
Asterisk: PVS-Studio Takes Up Telephony
Asterisk: PVS-Studio Takes Up TelephonyAsterisk: PVS-Studio Takes Up Telephony
Asterisk: PVS-Studio Takes Up Telephony
 
Java 5 Features
Java 5 FeaturesJava 5 Features
Java 5 Features
 
Unit II Java & J2EE regarding Java application development
Unit II Java & J2EE regarding Java application developmentUnit II Java & J2EE regarding Java application development
Unit II Java & J2EE regarding Java application development
 

C++ catch blocks and throw expressions

  • 1. catch blocks (C++ only) catch block syntax >>-catch--(--exception_declaration--)--{--statements--}-------->< You can declare a handler to catch many types of exceptions. The allowable objects that a function can catch are declared in the parentheses following the catch keyword (the exception_declaration). You can catch objects of the fundamental types, base and derived class objects, references, and pointers to all of these types. You can also catch const and volatile types. The exception_declaration cannot be an incomplete type, or a reference or pointer to an incomplete type other than one of the following: void* const void* volatile void* const volatile void* You cannot define a type in an exception_declaration. You can also use the catch(...) form of the handler to catch all thrown exceptions that have not been caught by a previous catch block. The ellipsis in the catch argument indicates that any exception thrown can be handled by this handler. If an exception is caught by a catch(...) block, there is no direct way to access the object thrown. Information about an exception caught by catch(...) is very limited. You can declare an optional variable name if you want to access the thrown object in the catch block. A catch block can only catch accessible objects. The object caught must have an accessible copy constructor. throw expressions (C++ only) You use a throw expression to indicate that your program has encountered an exception. throw expression syntax >>-throw--+-----------------------+---------------------------->< '-assignment_expression-'
  • 2. The type of assignment_expression cannot be an incomplete type, or a pointer to an incomplete type other than one of the following: void* const void* volatile void* const volatile void* The assignment_expression is treated the same way as a function argument in a call or the operand of a return statement. If the assignment_expression is a class object, the copy constructor and destructor of that object must be accessible. For example, you cannot throw a class object that has its copy constructor declared as private. Rethrowing an exception (C++ only) If a catch block cannot handle the particular exception it has caught, you can rethrow the exception. The rethrow expression (throw without assignment_expression) causes the originally thrown object to be rethrown. Because the exception has already been caught at the scope in which the rethrow expression occurs, it is rethrown out to the next dynamically enclosing try block. Therefore, it cannot be handled by catch blocks at the scope in which the rethrow expression occurred. Any catch blocks for the dynamically enclosing try block have an opportunity to catch the exception. The following example demonstrates rethrowing an exception: #include <iostream> using namespace std; struct E { const char* message; E() : message("Class E") { } }; struct E1 : E { const char* message; E1() : message("Class E1") { } }; struct E2 : E { const char* message; E2() : message("Class E2") { } }; void f() { try { cout<< "In try block of f()" <<endl;
  • 3. cout<< "Throwing exception of type E1" <<endl; E1 myException; throwmyException; } catch (E2& e) { cout<< "In handler of f(), catch (E2& e)" <<endl; cout<< "Exception: " <<e.message<<endl; throw; } catch (E1& e) { cout<< "In handler of f(), catch (E1& e)" <<endl; cout<< "Exception: " <<e.message<<endl; throw; } catch (E& e) { cout<< "In handler of f(), catch (E& e)" <<endl; cout<< "Exception: " <<e.message<<endl; throw; } } int main() { try { cout<< "In try block of main()" <<endl; f(); } catch (E2& e) { cout<< "In handler of main(), catch (E2& e)" <<endl; cout<< "Exception: " <<e.message<<endl; } catch (...) { cout<< "In handler of main(), catch (...)" <<endl; } } The following is the output of the above example: In try block of main() In try block of f() Throwing exception of type E1 In handler of f(), catch (E1& e) Exception: Class E1 In handler of main(), catch (...) The try block in the main() function calls function f(). The try block in function f() throws an object of type E1 named myException. The handler catch (E1 &e) catches myException. The handler then rethrowsmyException with the statement throw to the next dynamically enclosing try block: the try block in the main() function. The handler catch(...) catches myException.