devxlogo

Re-Throwing an Exception

Re-Throwing an Exception

An exception is thrown to indicate an abnormal state. The first handle to catch the exception can try to fix the problem. Should it fail to do that, or in case it only managed to perform a partial recovery, it can still (re-) throw the exception as long as there is a higher try-block, which has an appropriate catch-statement. In other words, try-blocks can be nested in a hierarchical order, enabling a re-thrown exception from a lower try-block’s catch-statement to be caught. A re-throw is indicated by a throw without an operand:

 class Exception {/*..*/}; //general base class for exceptionsclass FileException: public Exception {/*..*/};void main() {File f ("db.dat");try {try {//inner try			if (File.IsValid() == false )		 	throw FileException("db.dat"); //construct FileException object and throw it}//inner trycatch(FileException &fe)  { //first handler to cope with the exceptioncout<<"invalid file specification" <
See also  Professionalism Starts in Your Inbox: Keys to Presenting Your Best Self in Email
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist