This chapter discusses exception handling in C++. An exception is an undesirable event that occurs during program execution. A try/catch block is used to handle exceptions, where code that may cause exceptions is placed in the try block and catch blocks specify the exception type and handling code. When an exception is thrown in a try block, the corresponding catch block executes if the exception type matches. If no catch block matches, the call stack unwinds until the exception is caught or the program terminates.
TMPA-2017: Tools and Methods of Program Analysis
3-4 March, 2017, Hotel Holiday Inn Moscow Vinogradovo, Moscow
Vellvm - Verifying the LLVM
Steve Zdancewic (Professor, USA University of Pennsylvania)
For video follow the link: https://youtu.be/jDPAtUfnoBU
Would like to know more?
Visit our website:
www.tmpaconf.org
www.exactprosystems.com/events/tmpa
Follow us:
https://www.linkedin.com/company/exactpro-systems-llc?trk=biz-companies-cym
https://twitter.com/exactpro
TMPA-2017: Evolutionary Algorithms in Test Generation for digital systemsIosif Itkin
TMPA-2017: Tools and Methods of Program Analysis
3-4 March, 2017, Hotel Holiday Inn Moscow Vinogradovo, Moscow
Evolutionary Algorithms in Test Generation for digital systems
Yuriy Skobtsov, Vadim Skobtsov, St.Petersburg Polytechnic University
For presentation follow the link: https://www.youtube.com/watch?v=gUnKmPg614k
Would like to know more?
Visit our website:
www.tmpaconf.org
www.exactprosystems.com/events/tmpa
Follow us:
https://www.linkedin.com/company/exactpro-systems-llc?trk=biz-companies-cym
https://twitter.com/exactpro
TMPA-2017: Tools and Methods of Program Analysis
3-4 March, 2017, Hotel Holiday Inn Moscow Vinogradovo, Moscow
Vellvm - Verifying the LLVM
Steve Zdancewic (Professor, USA University of Pennsylvania)
For video follow the link: https://youtu.be/jDPAtUfnoBU
Would like to know more?
Visit our website:
www.tmpaconf.org
www.exactprosystems.com/events/tmpa
Follow us:
https://www.linkedin.com/company/exactpro-systems-llc?trk=biz-companies-cym
https://twitter.com/exactpro
TMPA-2017: Evolutionary Algorithms in Test Generation for digital systemsIosif Itkin
TMPA-2017: Tools and Methods of Program Analysis
3-4 March, 2017, Hotel Holiday Inn Moscow Vinogradovo, Moscow
Evolutionary Algorithms in Test Generation for digital systems
Yuriy Skobtsov, Vadim Skobtsov, St.Petersburg Polytechnic University
For presentation follow the link: https://www.youtube.com/watch?v=gUnKmPg614k
Would like to know more?
Visit our website:
www.tmpaconf.org
www.exactprosystems.com/events/tmpa
Follow us:
https://www.linkedin.com/company/exactpro-systems-llc?trk=biz-companies-cym
https://twitter.com/exactpro
Operation “Blue Star” is the only event in the history of Independent India where the state went into war with its own people. Even after about 40 years it is not clear if it was culmination of states anger over people of the region, a political game of power or start of dictatorial chapter in the democratic setup.
The people of Punjab felt alienated from main stream due to denial of their just demands during a long democratic struggle since independence. As it happen all over the word, it led to militant struggle with great loss of lives of military, police and civilian personnel. Killing of Indira Gandhi and massacre of innocent Sikhs in Delhi and other India cities was also associated with this movement.
Safalta Digital marketing institute in Noida, provide complete applications that encompass a huge range of virtual advertising and marketing additives, which includes search engine optimization, virtual communication advertising, pay-per-click on marketing, content material advertising, internet analytics, and greater. These university courses are designed for students who possess a comprehensive understanding of virtual marketing strategies and attributes.Safalta Digital Marketing Institute in Noida is a first choice for young individuals or students who are looking to start their careers in the field of digital advertising. The institute gives specialized courses designed and certification.
for beginners, providing thorough training in areas such as SEO, digital communication marketing, and PPC training in Noida. After finishing the program, students receive the certifications recognised by top different universitie, setting a strong foundation for a successful career in digital marketing.
Unit 8 - Information and Communication Technology (Paper I).pdfThiyagu K
This slides describes the basic concepts of ICT, basics of Email, Emerging Technology and Digital Initiatives in Education. This presentations aligns with the UGC Paper I syllabus.
A workshop hosted by the South African Journal of Science aimed at postgraduate students and early career researchers with little or no experience in writing and publishing journal articles.
Introduction to AI for Nonprofits with Tapp NetworkTechSoup
Dive into the world of AI! Experts Jon Hill and Tareq Monaur will guide you through AI's role in enhancing nonprofit websites and basic marketing strategies, making it easy to understand and apply.
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...Dr. Vinod Kumar Kanvaria
Exploiting Artificial Intelligence for Empowering Researchers and Faculty,
International FDP on Fundamentals of Research in Social Sciences
at Integral University, Lucknow, 06.06.2024
By Dr. Vinod Kumar Kanvaria
Synthetic Fiber Construction in lab .pptxPavel ( NSTU)
Synthetic fiber production is a fascinating and complex field that blends chemistry, engineering, and environmental science. By understanding these aspects, students can gain a comprehensive view of synthetic fiber production, its impact on society and the environment, and the potential for future innovations. Synthetic fibers play a crucial role in modern society, impacting various aspects of daily life, industry, and the environment. ynthetic fibers are integral to modern life, offering a range of benefits from cost-effectiveness and versatility to innovative applications and performance characteristics. While they pose environmental challenges, ongoing research and development aim to create more sustainable and eco-friendly alternatives. Understanding the importance of synthetic fibers helps in appreciating their role in the economy, industry, and daily life, while also emphasizing the need for sustainable practices and innovation.
This slide is special for master students (MIBS & MIFB) in UUM. Also useful for readers who are interested in the topic of contemporary Islamic banking.
How to Make a Field invisible in Odoo 17Celine George
It is possible to hide or invisible some fields in odoo. Commonly using “invisible” attribute in the field definition to invisible the fields. This slide will show how to make a field invisible in odoo 17.
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...Levi Shapiro
Letter from the Congress of the United States regarding Anti-Semitism sent June 3rd to MIT President Sally Kornbluth, MIT Corp Chair, Mark Gorenberg
Dear Dr. Kornbluth and Mr. Gorenberg,
The US House of Representatives is deeply concerned by ongoing and pervasive acts of antisemitic
harassment and intimidation at the Massachusetts Institute of Technology (MIT). Failing to act decisively to ensure a safe learning environment for all students would be a grave dereliction of your responsibilities as President of MIT and Chair of the MIT Corporation.
This Congress will not stand idly by and allow an environment hostile to Jewish students to persist. The House believes that your institution is in violation of Title VI of the Civil Rights Act, and the inability or
unwillingness to rectify this violation through action requires accountability.
Postsecondary education is a unique opportunity for students to learn and have their ideas and beliefs challenged. However, universities receiving hundreds of millions of federal funds annually have denied
students that opportunity and have been hijacked to become venues for the promotion of terrorism, antisemitic harassment and intimidation, unlawful encampments, and in some cases, assaults and riots.
The House of Representatives will not countenance the use of federal funds to indoctrinate students into hateful, antisemitic, anti-American supporters of terrorism. Investigations into campus antisemitism by the Committee on Education and the Workforce and the Committee on Ways and Means have been expanded into a Congress-wide probe across all relevant jurisdictions to address this national crisis. The undersigned Committees will conduct oversight into the use of federal funds at MIT and its learning environment under authorities granted to each Committee.
• The Committee on Education and the Workforce has been investigating your institution since December 7, 2023. The Committee has broad jurisdiction over postsecondary education, including its compliance with Title VI of the Civil Rights Act, campus safety concerns over disruptions to the learning environment, and the awarding of federal student aid under the Higher Education Act.
• The Committee on Oversight and Accountability is investigating the sources of funding and other support flowing to groups espousing pro-Hamas propaganda and engaged in antisemitic harassment and intimidation of students. The Committee on Oversight and Accountability is the principal oversight committee of the US House of Representatives and has broad authority to investigate “any matter” at “any time” under House Rule X.
• The Committee on Ways and Means has been investigating several universities since November 15, 2023, when the Committee held a hearing entitled From Ivory Towers to Dark Corners: Investigating the Nexus Between Antisemitism, Tax-Exempt Universities, and Terror Financing. The Committee followed the hearing with letters to those institutions on January 10, 202
2. Objectives
• In this chapter, you will:
– Learn what an exception is
– Learn how to handle exceptions within a program
– Learn how a try/catch block is used to handle
exceptions
– Learn how to throw an exception
– Become familiar with C++ exception classes and
how to use them in a program
2C++ Programming: From Problem Analysis to Program Design, Seventh Edition
3. Objectives (cont’d.)
– Learn how to create your own exception classes
– Discover how to throw and rethrow an exception
– Explore exception handling techniques
– Explore stack unwinding
3C++ Programming: From Problem Analysis to Program Design, Seventh Edition
4. Introduction
• Exception: undesirable event detectable
during program execution
• Code to handle exceptions depends on the
type of application being developed
• May or may not want the program to terminate
when an exception occurs
• Can add exception-handling code at point
where an error can occur
4C++ Programming: From Problem Analysis to Program Design, Seventh Edition
5. Handling Exceptions Within a
Program
• Assert function:
– Checks if an expression meets certain condition(s)
– If conditions are not met, it terminates the
program
• Example: division by 0
– If divisor is zero, assert terminates the program
with an error message
5C++ Programming: From Problem Analysis to Program Design, Seventh Edition
6. C++ Mechanisms
of Exception Handling
• try/catch block: used to handle exceptions
• Exception must be thrown in a try block and
caught by a catch block
• C++ provides support to handle exceptions via
a hierarchy of classes
6C++ Programming: From Problem Analysis to Program Design, Seventh Edition
7. try/catch Block
• Statements that may generate an exception
are placed in a try block
• The try block also contains statements that
should not be executed if an exception occurs
• try block is followed by one or more catch
blocks
7C++ Programming: From Problem Analysis to Program Design, Seventh Edition
9. try/catch Block (cont’d.)
• catch block:
– Specifies the type of exception it can catch
– Contains an exception handler
• If the heading of a catch block contains ...
(ellipses) in place of parameters
– Block can catch exceptions of all types
• If no exception is thrown in a try block
– All catch blocks are ignored
– Execution resumes after the last catch block
9C++ Programming: From Problem Analysis to Program Design, Seventh Edition
10. try/catch Block (cont’d.)
• If an exception is thrown in a try block
– Remaining statements (in block) are ignored
• Program searches catch blocks in order,
looking for an appropriate exception handler
– If the type of thrown exception matches the
parameter type in one of the catch blocks:
• Code of that catch block executes
• Remaining catch blocks are ignored
10C++ Programming: From Problem Analysis to Program Design, Seventh Edition
11. try/catch Block (cont’d.)
• A catch block can have at most one catch
block parameter
– catch block parameter becomes a placeholder
for the value thrown
11C++ Programming: From Problem Analysis to Program Design, Seventh Edition
12. Throwing an Exception
• For try/catch to work, the exception
must be thrown in the try block
• General syntax:
throw exception;
where expression is a constant value,
variable, or object
• Object being thrown can be a specific object
or an anonymous object
• In C++, an exception is a value
12C++ Programming: From Problem Analysis to Program Design, Seventh Edition
13. Order of catch Blocks
• catch block can catch:
– All exceptions of a specific type
– All types of exceptions
• A catch block with an ellipsis (. . .) catches
any type of exception
– If used, it should be the last catch block of
that sequence
• Be careful about the order in which you list
catch blocks
13C++ Programming: From Problem Analysis to Program Design, Seventh Edition
14. Using C++ Exception Classes
• C++ provides support to handle exceptions via
a hierarchy of classes
• what function: returns a string containing the
exception object thrown by C++’s built-in
exception classes
• class exception: base class of the
exception classes provided by C++
– Contained in the header file exception
14C++ Programming: From Problem Analysis to Program Design, Seventh Edition
15. Using C++ Exception Classes (cont’d.)
• Two subclasses of exception (defined in
stdexcept):
– logic_error includes subclasses:
• invalid_argument: for use when illegal
arguments are used in a function call
• out_of_range: string subscript out of range error
• length_error: if a length greater than the
maximum allowed for a string object is used
– runtime_error includes subclasses:
• overflow_error and underflow_error
15C++ Programming: From Problem Analysis to Program Design, Seventh Edition
16. Creating Your Own Exception Classes
• Can create your own exception classes to
handle specific exceptions
– C++ uses the same mechanism to process these
exceptions
• throw statement: used to throw your own
exceptions
• Any class can be an exception class
– How you use the class makes it an exception class
16C++ Programming: From Problem Analysis to Program Design, Seventh Edition
17. Creating Your Own Exception Classes
(cont’d.)
• Exception class with member variables
typically includes:
– Constructors
– The function what
17C++ Programming: From Problem Analysis to Program Design, Seventh Edition
18. Rethrowing and Throwing an
Exception
• When an exception occurs in a try block,
control immediately passes to one of the
catch blocks, which either:
– Handles the exception, or partially processes the
exception, then rethrows the same exception
– Rethrows another exception for the calling
environment to handle
• This allows you to provide exception-
handling code all in one place
18C++ Programming: From Problem Analysis to Program Design, Seventh Edition
19. Rethrowing and Throwing an
Exception (cont’d.)
• Syntax to rethrow an exception caught by a
catch block:
– If the same exception is to be rethrown:
– If a different exception is to be thrown
where expression is a constant value,
variable, or object
19C++ Programming: From Problem Analysis to Program Design, Seventh Edition
20. Rethrowing and Throwing an
Exception (cont’d.)
• Object being thrown can be:
– A specific object
– An anonymous object
• A function specifies the exceptions it throws in
its heading using the throw clause
20C++ Programming: From Problem Analysis to Program Design, Seventh Edition
21. Exception-Handling Techniques
• When an exception occurs, the programmer
usually has three choices:
– Terminate the program
– Include code to recover from the exception
– Log the error and continue
21C++ Programming: From Problem Analysis to Program Design, Seventh Edition
22. Terminate the Program
• In some cases, it is best to terminate the
program when an exception occurs
• Example: if an input file does not exist when
the program executes
– There is no point in continuing with the program
– Program can output an appropriate error message
and terminate
22C++ Programming: From Problem Analysis to Program Design, Seventh Edition
23. Fix the Error and Continue
• In some cases, you will want to handle the
exception and let the program continue
• Example: a user inputs a letter instead of a
number
– The input stream will enter the fail state
– Can include the necessary code to keep prompting
the user to input a number until the entry is valid
23C++ Programming: From Problem Analysis to Program Design, Seventh Edition
24. Log the Error and Continue
• Example: if the program is designed to run a
nuclear reactor or continuously monitor a
satellite
– It cannot be terminated if an exception occurs
• When an exception occurs
– The program should write the exception into a file
and continue to run
24C++ Programming: From Problem Analysis to Program Design, Seventh Edition
25. Stack Unwinding
• When an exception is thrown in a function,
the function can do the following:
– Do nothing
– Partially process the exception and throw the
same exception or a new exception
– Throw a new exception
• In each case, the function-call stack is
unwound so that the exception can be caught
in the next try/catch block
25C++ Programming: From Problem Analysis to Program Design, Seventh Edition
26. Stack Unwinding (cont’d.)
• When the function call stack is unwound:
– The function in which the exception was not
caught and/or rethrown terminates
– Memory for its local variables is destroyed
• Stack unwinding continues until:
– A try/catch handles the exception, or
– The program does not handle the exception
• The function terminate is called to terminate
the program
26C++ Programming: From Problem Analysis to Program Design, Seventh Edition
27. Summary
• Exception: an undesirable event detectable
during program execution
• assert checks whether an expression
meets a specified condition; terminates if
not met
• try/catch block handles exceptions
• Statements that may generate an
exception are placed in a try block
27C++ Programming: From Problem Analysis to Program Design, Seventh Edition
28. Summary (cont’d.)
• catch block specifies type of exception it
can catch and contains an exception handler
• If no exceptions are thrown in a try block,
all catch blocks for that try block are
ignored
• Data type of catch block parameter
specifies type of exception that catch block
can catch
28C++ Programming: From Problem Analysis to Program Design, Seventh Edition
29. Summary (cont’d.)
• exception: base class for exception
classes
• what function: returns a string containing
the exception object thrown by built-in
exception classes
• You can create your own exception classes
• A function specifies the exceptions it throws
in its heading with the throw clause
29C++ Programming: From Problem Analysis to Program Design, Seventh Edition
30. Summary (cont’d.)
• If the program does not handle the
exception, then the function terminate
terminates the program
• Any class can be considered an exception
class, based on how it is used
30C++ Programming: From Problem Analysis to Program Design, Seventh Edition