Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 15 Strings String::Concat String::CompareTo, Equals, == If( string1 == S”Hello”) String1->Equals(S”Hello”) String1->CompareTo(S”Hello”) CompareTo.

Similar presentations


Presentation on theme: "Chapter 15 Strings String::Concat String::CompareTo, Equals, == If( string1 == S”Hello”) String1->Equals(S”Hello”) String1->CompareTo(S”Hello”) CompareTo."— Presentation transcript:

1 Chapter 15 Strings String::Concat String::CompareTo, Equals, == If( string1 == S”Hello”) String1->Equals(S”Hello”) String1->CompareTo(S”Hello”) CompareTo return 0, -1 or 1 when String1 is equal, less, or greater Equals and == return true or false

2 String StartWith – determines whether a string instance starts with the string literal passed to it as an argument. Return true or false If( string1->StartWith(S”CS”) ) EndsWith – determines whether a string instance ends with the string literal passed to it as an argument If( string1->EndWith(S”.cpp”) )

3 String IndexOf – locate the first occurrence of a character or substring in a string. Return index of the character or –1 if the character is not found N = String1->IndexOf(‘l’); LastIndexOf – locate the last occurrence of a character in a string

4 SubString( int) – takes one int argument which specifies the starting index. The subString returned contains a copy of the characters from the starting index to the end of the string. SubString(int,int) – takes two int arguments. The first is starting index and the second is length of substring

5 Exception Handling An Exception is an indication of a problem that occurs during a program’s execution. Exception handling enables the programmer to remove error- handling code Writing input format, divided by zero, file not found, etc.

6 Exception handling MC++ uses try block to enable exception handling. A try block consists of try and { } that defines a block of code in which exceptions may occur. Statement that might cause exceptions Statements that should not execute if an exception occurs. try {… } catch ( Exception * ) { … }

7 Catch handler Following try block are zero or more catch blocks ( also called catch handlers) Each catch handler specifies in parentheses an exception parameter that represent the type of exception the catch handler can handle. If it includes an optional parameter name, catch handler can use it to interact with a caught exception object. Optionally, a parameterless catch handler catches all exception types. Optional __finally block contains code that always executes, regardless of whether an exception occurs.

8 A try block must be followed by at least one catch block or __finally block Exception properties Message – stores the error message associated with an Exception object. This message may be a default message associated with the exception type or a customized message passed to an exception object’s constructor when the exception object is constructed.

9 Exception properties StackTrace – contains a string that represents the method call stack The runtime environment keeps a list of method calls that have been made up to a given moment. StackTrace string represents this list of method that had not finished processing at the time the exception occurred. The exact location at which the exception occurs in the program is called the exception’s throw point

10 Example: DivideByZeroException try { int res = num/de; } catch(DivideByZeroException *e) { Console::WriteLine(e->Message); }


Download ppt "Chapter 15 Strings String::Concat String::CompareTo, Equals, == If( string1 == S”Hello”) String1->Equals(S”Hello”) String1->CompareTo(S”Hello”) CompareTo."

Similar presentations


Ads by Google