File tree Expand file tree Collapse file tree 3 files changed +31
-14
lines changed
exercises/practice/error-handling Expand file tree Collapse file tree 3 files changed +31
-14
lines changed Original file line number Diff line number Diff line change @@ -7,9 +7,19 @@ You will learn how to:
77- Check input types and throw errors for invalid inputs.
88- Throw errors for specific cases (e.g., empty strings).
99- Return the uppercase version of the string if it is valid.
10+ - Handle and catch errors using a try..catch block
1011
11- Your function should:
1212
13- - Throw a ` TypeError ` if input is not a string.
14- - Throw a general ` Error ` if input is an empty string.
15- - Return the uppercase form of the string 'hello'.
13+ Implement the processString function using a ` try…catch ` block.
14+
15+ Inside the ` try ` block:
16+
17+ - If the input is not a string, throw a ` TypeError ` .
18+
19+ - If the input is an empty string, throw a generic ` Error ` .
20+
21+ - Otherwise, return the input in ` uppercase ` .
22+
23+ Inside the ` catch ` block:
24+ - console log the ` error ` message
25+ - ` throw ` the ` error ` so it can be tested for its type.
Original file line number Diff line number Diff line change 11export const processString = ( input ) => {
2- if ( typeof input !== 'string' ) {
3- throw new TypeError ( ) ;
2+ try {
3+ if ( typeof input !== 'string' ) {
4+ throw new TypeError ( 'Input must be a string' ) ;
5+ }
6+ if ( input === '' ) {
7+ throw new Error ( 'Input cannot be empty' ) ;
8+ }
9+ return input . toUpperCase ( ) ;
10+ } catch ( error ) {
11+ console . log ( error . message ) ;
12+ throw error ;
413 }
5- if ( input === '' ) {
6- throw new Error ( ) ;
7- }
8- return input . toUpperCase ( ) ;
914} ;
Original file line number Diff line number Diff line change 1+ //
2+ // This is only a SKELETON file for the 'Error handling' exercise. It's been provided as a
3+ // convenience to get you started writing code faster.
4+ //
5+
16export const processString = ( input ) => {
2- //TODO: implement this
3- //should throw TypeError if input is not a string
4- //should throw a general Error if input is an empty string
5- //should return the uppercase version of the string 'hello'
7+ throw new Error ( 'Remove this line and implement the function' ) ;
68} ;
You can’t perform that action at this time.
0 commit comments