[go: up one dir, main page]

0% found this document useful (0 votes)
575 views4 pages

Workshop #2: Exceptions: Learning Outcomes

This document provides instructions for a workshop on exceptions in Java. It has two parts. Part 1 involves writing a program that accepts a number from the user, handles invalid inputs using try-catch, and prints the number or an error message. Part 2 involves writing a program that accepts a string, checks if it matches a regular expression using matches(), handles errors using try-catch and throws, and prints the string or an error message. Step-by-step instructions are provided for both parts, including creating files and using specific Java constructs like do-while and regular expressions. The learning outcomes are to practice handling errors and describe what was learned.

Uploaded by

Long Shino
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
575 views4 pages

Workshop #2: Exceptions: Learning Outcomes

This document provides instructions for a workshop on exceptions in Java. It has two parts. Part 1 involves writing a program that accepts a number from the user, handles invalid inputs using try-catch, and prints the number or an error message. Part 2 involves writing a program that accepts a string, checks if it matches a regular expression using matches(), handles errors using try-catch and throws, and prints the string or an error message. Step-by-step instructions are provided for both parts, including creating files and using specific Java constructs like do-while and regular expressions. The learning outcomes are to practice handling errors and describe what was learned.

Uploaded by

Long Shino
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Workshop #2: Exceptions

Learning Outcomes:

Upon successful completion of this workshop, you will have demonstrated the abilities
to:

 Practice handling errors in your program.


 Describe to your instructor what you have learned in completing this workshop.

Requirements:

Part1: [5 points]

Write a Java program to accept a number and print out it. If the number is below 1 then a
message “the number is invalid” is printed out. Using do..while to input again, try-catch block to
handle errors.

The user interface may be:


Enter the number: - 2
The number is invalid”

Enter the number: world


The number is invalid”

Enter the number: 2


The number is 2

Step by step workshop instructions:


- Create a new project named “workshop2”
- In the project, create a new file named “Part1.java”
- In the method main, you type:

1
Part 2: [5 points]

Write a Java program to accept a string and print out it. If the string does not match SExxx(x is
digit) then a message “the string is invalid” is printed out. Using do..while to input again.
Hint: In library class String, you should use the method matches() to do this, use try-
catch block and use throws to handle errors.

The user interface may be:


Input the string 1: I love u
the string is invalid

Input the string 1: SE123


the string is SE123

Step by step workshop instructions:

Background: In this workshop, you will use the pattern string( also called regular expression,
see more What is a Regular Expression? - Definition from Techopedia). You should read the
document to complete the exercise.

2
Task 1: use try-catch

- In the project “Workshop2”, create a new file named “Part2.java”


- In the method main, you type:

At the row 9, use rules of the regular expression to create a pattern string “SExxx”, x is digit

Task 2: use throws keyword

- create a new file named “Part2_2.java”


- in the method main, type:

3
4

You might also like