100 likes | 131 Views
Learn about exceptions in software engineering, including defining checked and unchecked exceptions, and how to handle problems using exceptions. Includes examples and best practices.
E N D
SE-1021Software Engineering II • Week 7, Class 1 • Exceptions • Defining checked & unchecked • Handling problems with and without exceptions • Week 7, Friday: Exam II • (Examples from text)
More Definitions • Checked exception – An exception that the compiler checks is handled • Unchecked exception – An exception that the compiler implicitly “throws” from a method if it is not handled explicitly. SE-1021 Dr. Josiah Yoder Slide style: Dr. Hornick
General Principle • If a problem can be handled simply without exceptions, don’t use them • Otherwise, do use them SE-1021 Dr. Josiah Yoder Slide style: Dr. Hornick
Problem 1a • Edit divideByZero/DivisionByZeroExample to keep asking for the denominator if it is zero SE-1021 Dr. Josiah Yoder Slide style: Dr. Hornick
Problem 1b (aka 2a) • Next, handle wrong input • If either thing entered is wrong, loop back and ask for both numerator and denominator again SE-1021 Dr. Josiah Yoder Slide style: Dr. Hornick
Problem 3a • Complete the NumerList example so that it prints the mean if the use behaves. • No need to handle user error SE-1021 Dr. Josiah Yoder Slide style: Dr. Hornick
Problem 3b • If the user enters “q”, print “NaN” using double’s NaN value • Avoid catching the exception SE-1021 Dr. Josiah Yoder Slide style: Dr. Hornick
Problem 3c • If the user enters something invalid (not q, not an integer), handle the problem. Do not re-prompt the user. • Fix the out-of-bounds exception if it occurs SE-1021 Dr. Josiah Yoder Slide style: Dr. Hornick
To catch individually, what order? • Exception • IllegalArgumentException • IOException • NumberFormatException • RuntimeException SE-1021 Dr. Josiah Yoder Slide style: Dr. Hornick
… } catch ( ) { } catch ( ) { } catch ( ) { } catch ( ) { } catch ( ) { } SE-1021 Dr. Josiah Yoder Slide style: Dr. Hornick