Programming in C++ 1. Learning Outcome At the end of this slide, student able to:  Identify the different types of error in your program  Understand.

Slides:



Advertisements
Similar presentations
Exceptions CSE301 University of Sunderland Harry Erwin, PhD.
Advertisements

Lecture 23 Input and output with files –(Sections 2.13, 8.7, 8.8) Exceptions and exception handling –(Chapter 17)
HST 952 Computing for Biomedical Scientists Lecture 7.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 13 Exception Handling.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Fall 2013 Chapter 13 Exception.
Exception Handling1. 2 Exceptions  Definition  Exception types  Exception Hierarchy  Catching exceptions  Throwing exceptions  Defining exceptions.
Java Programming Exceptions. Java has a built in mechanism for error handling and trapping errors Usually this means dealing with abnormal events or code.
EXCEPTIONS. What’s an exception?? Change the flow of control when something important happens ideally - we catch errors at compile time doesn’t happen.
COMP201 Java Programming Topic 6: Exceptions Reading: Chapter 11.
Slides prepared by Rose Williams, Binghamton University ICS201 Exception Handling University of Hail College of Computer Science and Engineering Department.
1 / 89 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 11 Programming Fundamentals using Java 1.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 17 Exceptions and.
COMP201 Java Programming Topic 7: Exceptions Reading: Chapter 11.
Exceptions Three categories of errors: Syntax errors Runtime errors Logic errors Syntax errors: rules of the language have not been followed. Runtime error:
Exceptions in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 Exception Handling (in a nutshell). 2 Motivations When a program runs into a runtime error, the program terminates abnormally. How can you handle the.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 17 Exceptions and.
Introduction to Java Chapter 11 Error Handling. Motivations When a program runs into a runtime error, the program terminates abnormally. How can you handle.
Java Exception Handling
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Exception Handling in Java Course Lecture Slides 7 th July 2010 “ Admitting.
CS203 Java Object Oriented Programming Errors and Exception Handling.
COMP 103 Comparators and Comparable. RECAP  Iterator and Iterable TODAY  Comparator and Comparable  Exceptions 2 RECAP-TODAY.
Programming in C++ 1. Learning Outcome  At the end of this slide, student able to:  Understand what is Inheritance?  Compare between inheritance and.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 14 Exception Handling and Text.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 12 Exception Handling and Text.
What is an exception? An exception is: – an event that interrupts the normal processing of the program. –an error condition that violates the semantic.
1 Chapter 18 Exception Handling. 2 Motivations F Program runs into a runtime error –program terminates abnormally F How can you handle the runtime error.
Chapter 13 Exception Handling F Claiming Exceptions F Throwing Exceptions F Catching Exceptions F Rethrowing Exceptions  The finally Clause F Cautions.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 18 Exception Handling.
Exception Handling Exceptions and language features to handle them.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Exception Handling.
Exception Handling 1. Introduction Users may use our programs in an unexpected ways. Due to design errors or coding errors, our programs may fail in unexpected.
Exceptions "A slipping gear could let your M203 grenade launcher fire when you least expect it. That would make you quite unpopular in what's left of your.
1.  At the end of this slide, student able to understand the concept of multithreading and exception handling. 2.
COMP Exception Handling Yi Hong June 10, 2015.
Exception Handling in JAVA. Introduction Exception is an abnormal condition that arises when executing a program. In the languages that do not support.
Peter Andreae Engineering and Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington2012 Implementing.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 15 Exceptions and.
Exceptions in Java. Exceptions An exception is an object describing an unusual or erroneous situation Exceptions are thrown by a program, and may be caught.
Exceptions in C++. Exceptions  Exceptions provide a way to handle the errors generated by our programs by transferring control to functions called handlers.
Exceptions in Java. What is an exception? An exception is an error condition that changes the normal flow of control in a program Exceptions in Java separates.
MIT AITI 2004 – Lecture 14 Exceptions Handling Errors with Exceptions.
Copyright © Curt Hill Error Handling in Java Throwing and catching exceptions.
1 Exceptions. 2 Syntax Errors, Runtime Errors, and Logic Errors syntax errors, runtime errors, and logic errors You learned that there are three categories.
1 Chapter 15 Exceptions and Assertions. 2 Objectives F To know what is exception and what is exception handling (§15.2). F To distinguish exception types:
Liang,Introduction to Java Programming,revised by Dai-kaiyu 1 Chapter 15 Exceptions and Assertions Nothing is impossible.
COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi.
CS 112 Programming 2 Lecture 08 Exception Handling & Text I/O (1)
Lecture10 Exception Handling Jaeki Song. Introduction Categories of errors –Compilation error The rules of language have not been followed –Runtime error.
Exception Handling How to handle the runtime errors.
Exception. Agenda Exception. Handling Exceptions. The finally Clause.
Throw, Throws & Try-Catch Statements Explanations and Pictures from: Reference:
ECE122 L23: Exceptions December 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 24 Exceptions.
Exceptions and Error Handling. Exceptions Errors that occur during program execution We should try to ‘gracefully’ deal with the error Not like this.
Lecture 5: Exception Handling and Text File I/O Michael Hsu CSULA.
Garbage Collection It Is A Way To Destroy The Unused Objects. To do so, we were using free() function in C language and delete() in C++. But, in java it.
Agenda Introduction Errors and Exception Exception Hierarchy Classification of Exceptions Built in Exceptions Exception Handling in Java User defined.
Exception Handling. You learned that there are three categories of errors: syntax errors, runtime errors, and logic errors. Syntax errors arise because.
Chapter 13 Exception Handling
Chapter 12 Exception Handling And Text IO
Chapter 13 Exception Handling
Chapter 12 Exception Handling and Text IO
Chapter 12 Exception Handling
EXCEPTION HANDLING OR ERROR HANDLING.
Chapter 11 Exception Handling and Text I/O
Web Design & Development Lecture 7
Managing Errors and Exceptions
Chapter 13 Exception Handling
Chapter 12 Exception Handling and Text IO Part 1
Coming up Exceptions An Example The Throws keyword Try and Catch
Presentation transcript:

Programming in C++ 1

Learning Outcome At the end of this slide, student able to:  Identify the different types of error in your program  Understand the different types of error handler clauses?  Describe the usefulness of catch and finally statement in handling exception?  Understand the rethrowing exceptions 2

3 Exception-Handling Overview What do you think if 4 divided by 0?

Exception-Handling Overview  It is possible C++ can calculate this equation? 4

Exception-Handling Overview  C++ cannot calculate that equation, the output will be failed!! 5

Exception Overview #include using namespace std; const int DefaultSize = 10; int main() { int top = 90; int bottom = 0; try { cout << "top / 2 = " << (top/ 2) << endl; cout << "top divided by bottom = "; if ( bottom == 0 ) throw "Division by zero!"; cout << (top / bottom) << endl; cout << "top / 3 = " << (top/ 3) << endl; } catch( const char * ex ) { cout << "\n*** " << ex << " ***" << endl; } cout << "Done." << endl; return 0; } 6 #include using namespace std; const int DefaultSize = 10; int main() { int top = 90; int bottom = 0; cout << "top / 2 = " << (top/ 2) << endl; cout << "top divided by bottom = "; if ( bottom == 0 ){ cout << (top / bottom) << endl; // cout << "\n*** Division by zero! ***" << endl; } else { cout << (top / bottom) << endl; cout << "top / 3 = " << (top/ 3) << endl; } cout << "Done." << endl; return 0; }

Example of Exception 7 ExceptionDescription ArithmeticExceptionArithmetic error, such as divide-by-zero. ArrayIndexOutOfBoundsExceptionArray index is out-of-bounds. ArrayStoreExceptionAssignment to an array element of an incompatible type. ClassCastExceptionInvalid cast. IllegalArgumentExceptionIllegal argument used to invoke a method. IllegalMonitorStateExceptionIllegal monitor operation, such as waiting on an unlocked thread. IllegalStateExceptionEnvironment or application is in incorrect state. IllegalThreadStateExceptionRequested operation not compatible with current thread state. IndexOutOfBoundsExceptionSome type of index is out-of-bounds. NegativeArraySizeExceptionArray created with a negative size. NullPointerExceptionInvalid use of a null reference. NumberFormatExceptionInvalid conversion of a string to a numeric format. SecurityExceptionAttempt to violate security. StringIndexOutOfBoundsAttempt to index outside the bounds of a string. UnsupportedOperationExceptionAn unsupported operation was encountered.

Other Exception  AWTException  AclNotFoundException  ActivationException  AlreadyBoundException  ApplicationException  ArithmeticException  ArrayIndexOutOfBoundsException  AssertionException  BackingStoreException  BadAttributeValueExpException  BadBinaryOpValueExpException  BadLocationException  BadStringOperationException  BatchUpdateException  BrokenBarrierException  CertificateException  ChangedCharSetException  CharConversionException  CharacterCodingException  ClassCastException  ClassNotFoundException  CloneNotSupportedException  ClosedChannelException  ConcurrentModificationException  DataFormatException 8 DatatypeConfigurationException DestroyFailedException EOFException Exception ExecutionException ExpandVetoException FileLockInterruptionException FileNotFoundException FishFaceException FontFormatException GSSException GeneralSecurityException IIOException IOException IllegalAccessException IllegalArgumentException IllegalClassFormatException IllegalStateException IndexOutOfBoundsException InputMismatchException InstantiationException InterruptedException InterruptedIOException IntrospectionException InvalidApplicationException InvalidMidiDataException InvalidPreferencesFormatException InvalidTargetObjectTypeException

 InvocationTargetException  JAXBException  JMException  KeySelectorException  LastOwnerException  LineUnavailableException  MalformedURLException  MarshalException  MidiUnavailableException  MimeTypeParseException  NamingException  NegativeArraySizeException  NoSuchElementException  NoSuchFieldException  NoSuchMethodException  NoninvertibleTransformException  NotBoundException  NotOwnerException  NullPointerException  NumberFormatException  ObjectStreamException  ParseException  ParserConfigurationException  PrintException  PrinterException  PrivilegedActionException  PropertyVetoException 9 ProtocolException RefreshFailedException RemarshalException RemoteException RuntimeException SAXException SOAPException SQLException SQLWarning SSLException ScriptException ServerNotActiveException SocketException SyncFailedException TimeoutException TooManyListenersException TransformException TransformerException URIReferenceException URISyntaxException UTFDataFormatException UnknownHostException UnknownServiceException UnmodifiableClassException UnsupportedAudioFileException UnsupportedCallbackException UnsupportedEncodingException UnsupportedFlavorException UnsupportedLookAndFeelException UnsupportedOperationException UserException XAException XMLParseException XMLSignatureException XMLStreamException XPathException ZipException

Friendly Advice about Exception  Actually, it is very difficult to remember all exception especially to junior programmer. However, the programmer can use online recourses. 10

11 System Errors System errors are thrown by JVM and represented in the Error class. The Error class describes internal system errors. Such errors rarely occur. If one does, there is little you can do beyond notifying the user and trying to terminate the program gracefully.

12 Exceptions Exception describes errors caused by your program and external circumstances. These errors can be caught and handled by your program.

13 Runtime Exceptions RuntimeException is caused by programming errors, such as bad casting, accessing an out-of-bounds array, and numeric errors.

14 Throwing Exceptions Example /** Set a new radius */ public void setRadius(double newRadius) throws IllegalArgumentException throws IllegalArgumentException { if (newRadius >= 0) radius = newRadius; else throw new IllegalArgumentException( "Radius cannot be negative"); }

15 Catching Exceptions try { statements; // Statements that may throw exceptions } catch (Exception1 exVar1) { handler for exception1; } catch (Exception2 exVar2) { handler for exception2; }... catch (ExceptionN exVar3) { handler for exceptionN; }

16 Catching Exceptions

17 Trace a Program Execution animation try { statements; } catch(TheException ex) { handling ex; } finally { finalStatements; } Next statement; Suppose no exceptions in the statements

18 Trace a Program Execution animation try { statements; } catch(TheException ex) { handling ex; } finally { finalStatements; } Next statement; The final block is always executed

19 Trace a Program Execution animation try { statements; } catch(TheException ex) { handling ex; } finally { finalStatements; } Next statement; Next statement in the method is executed

20 Trace a Program Execution animation try { statement1; statement2; statement3; } catch(Exception1 ex) { handling ex; } finally { finalStatements; } Next statement; Suppose an exception of type Exception1 is thrown in statement2

21 Trace a Program Execution animation try { statement1; statement2; statement3; } catch(Exception1 ex) { handling ex; } finally { finalStatements; } Next statement; The exception is handled.

22 Trace a Program Execution animation try { statement1; statement2; statement3; } catch(Exception1 ex) { handling ex; } finally { finalStatements; } Next statement; The final block is always executed.

23 Trace a Program Execution animation try { statement1; statement2; statement3; } catch(Exception1 ex) { handling ex; } finally { finalStatements; } Next statement; The next statement in the method is now executed.

24 Trace a Program Execution animation try { statement1; statement2; statement3; } catch(Exception1 ex) { handling ex; } catch(Exception2 ex) { handling ex; throw ex; } finally { finalStatements; } Next statement; statement2 throws an exception of type Exception2.

25 Trace a Program Execution animation try { statement1; statement2; statement3; } catch(Exception1 ex) { handling ex; } catch(Exception2 ex) { handling ex; throw ex; } finally { finalStatements; } Next statement; Handling exception

26 Trace a Program Execution animation try { statement1; statement2; statement3; } catch(Exception1 ex) { handling ex; } catch(Exception2 ex) { handling ex; throw ex; } finally { finalStatements; } Next statement; Execute the final block

27 Trace a Program Execution animation try { statement1; statement2; statement3; } catch(Exception1 ex) { handling ex; } catch(Exception2 ex) { handling ex; throw ex; } finally { finalStatements; } Next statement; Rethrow the exception and control is transferred to the caller