Standard exceptions

15
Standard exceptions 1.What is the “exception” ? 2.What can we do with it? 3.What is the commands of library exception? 4.How to use the library exception ? 5. what is Exception specifications? By mohamad al~hsan

Transcript of Standard exceptions

Page 1: Standard exceptions

Standard exceptions

1. What is the “exception” ?2. What can we do with it?3. What is the commands of library exception? 4. How to use the library exception ? 5. what is Exception specifications?

By mohamad al~hsan

Page 2: Standard exceptions

“Exception” aha now I got it what is it!!!!!

IT’s the “واسطه”No!!! I’ll teal you what's

exception is.

By mody ma’ane

Page 3: Standard exceptions

Exceptions Exceptions provide a way to react to exceptional circumstances (like runtime errors) in our program by transferring control to

special functions called handlers.

To catch exceptions we must place a portion of code under exception inspection. This is done by enclosing that portion of code in a try block. When an exceptional circumstance arises within that block, an exception is thrown that transfers the

control to the exception handler. If no exception is thrown, the code continues normally and all handlers are ignored.

An exception is thrown by using the throw keyword from inside the try block. Exception handlers are declared with the keyword catch, which must be placed immediately after the try block lets

see an exampl :->By mody ma’ane

Page 4: Standard exceptions

Example :• #include <iostream>• using namespace std;

• int main () {• try // Our star the “TRY” we are saying to the compiler try this code if its good • //let it work but if it makes a problems pass some thing else (it can be any thing you wont)

• {• throw 20; //it means throw the thing you wont ?? Cane we throw rocks ?? • //no we can throw only expression as one param in this case it’s int (20) .

• }//ok if we throw the expression who is going to catch it • //the catch fun will do it Of course• catch (int e)• {//but can we throw a char and catch an int?? No it must be from the same

kind . • cout << "An exception occurred. Exception Nr. " << e << endl;• }//now I started to understand .• return 0;• }

By mody ma’ane

Page 5: Standard exceptions

• he code under exception handling is enclosed in a try block. In this example this code simply throws an exception:

• A throw expression accepts one parameter (in this case the integer value 20), which is passed as an argument to the exception handler.

• The exception handler is declared with the catch keyword. As you can see, it follows immediately the closing brace of the try block. The catch format is similar to a regular function that always has at least one parameter. The type of this parameter is very important, since the type of the argument passed by the throw expression is checked against it, and only in the case they match, the exception is caught.

• We can chain multiple handlers (catch expressions), each one with a different parameter type. Only the handler that matches its type with the argument specified in the throw statement is executed.

By mody ma’ane

Page 6: Standard exceptions

Example • If we use an ellipsis (...) as the parameter of

catch, that handler will catch any exception no matter what the type of the throw exception is. This can be used as a default handler that catches all exceptions not caught by other handlers if it is specified at last :->

By mody ma’ane

Page 7: Standard exceptions

• In this case the last handler would catch any exception thrown with any parameter that is neither an int nor a char.

• After an exception has been handled the program execution resumes after the try-catch block, not after the throw statement!.

• It is also possible to nest try-catch blocks within more external try blocks. In these cases, we have the possibility that an internal catch block forwards the exception to its external level. This is done with the expression throw; with no arguments. For example:

By mody ma’ane

Page 8: Standard exceptions

• Now I think you have an idea about the

“ Exception “

Yes I think I started to make an idea about the whole thing.

ok lets go on>>>

By mody ma’ane

Page 9: Standard exceptions

The commands of library exception

NumericalError do specific handling for a numerical error.

LapackError handle any other LapackError . and a lots of commands but these are the most important>>Woooow now I’m confused there is a lots of commands!! How do they work ??

Don’t worry I’ll teal you how to work with them.

Ok lets see

All exceptions thrown by components of the C++ Standard library throw exceptions derived from this std::exception class. These are:

By mody ma’ane

LAPACK (Linear Algebra PACKage) is a software library for numerical linear algebra. It provides routines for solving systems of linear equations and linear least squares, eigenvalue problems, and singular value decomposition.

Page 10: Standard exceptions

For example, if we use the operator new and the memory cannot be allocated, an exception of type bad_alloc is thrown : as:->

That’s where the errors in the windows come from ?

Yes, my friend when “example:” you try to Reserve a place in the memory that the system can’t reserve you will see the fowling error “Error allocating memory. ” or ” The system cant allocate memory.”

By mody ma’ane

Page 11: Standard exceptions

• Remark

when you use the exceptions from the library <exception> you must :

1.But “&” after the command :->

2.All exceptions thrown by components of the C++ Standard library throw exceptions derived from this std::exception class.

By mody ma’ane

Page 12: Standard exceptions

A real example :

By mody ma’ane

Page 13: Standard exceptions

Exception specifications When declaring a function we can limit the exception type it might directly or

indirectly throw by appending a throw suffix to the function declaration: float myfunction (char param) throw (int);

This declares a function called myfunction which takes one argument of type char and returns an element of type float. The only exception that this function might throw is an exception of type int. If it throws an exception with a different

type, either directly or indirectly, it cannot be caught by a regular int-type handler.

If this throw specifies is left empty with no type, this means the function is not allowed to throw exceptions. Functions with no throw specifier (regular

functions) are allowed to throw exceptions with any type:int myfunction (int param) throw(); // no exceptions allowed

int myfunction (int param); // all exceptions allowed

By mody ma’ane

Page 14: Standard exceptions

Some error codes (numbers) we can use

• 530 : No enough memory • 1021 : Error while validation of

parameter • 510 : No enough space for

moving home directory • 507 : Unable to update/read

group file • 902 : Unable to get the

Container Root • 911 : Error while parsing config

file.• 1012 : Host name is invalid

• 1315 : Unable to get memory information

• 'C++' Utility error codes:• 101:connection to agent failed• 100:unable to save the ssh key

•‘C' Packets error codes:

• 407:Error while writing given file• 405:Error while opening given file• 406:Error while reading given file• 411 :Unable to read socket• 412 :Unable to write socket• C06 :(Unable to return required value)• C08 :(Parse or Syntax error in the passed in CLIPS expressions)• S00 :(Internal generic system error)• C01 :(Unable to create a system object)• C04 :(This file unable to open)

Good Things To

Know

By mody ma’ane

Page 15: Standard exceptions

Don by : ->• Mohammad al hsan .->

• <-Mohammad Abu Zina .

• Loay kreshan .->

• Under the supervision of doctor ->Weal Al- qasas

Basically From:

By mody ma’ane