How to Handle Exceptions Step 1 Place a try block around the - - PowerPoint PPT Presentation

how to handle exceptions
SMART_READER_LITE
LIVE PREVIEW

How to Handle Exceptions Step 1 Place a try block around the - - PowerPoint PPT Presentation

How to Handle Exceptions Step 1 Place a try block around the statement(s) that may throw the exception. try { ... } Step 2 Place a catch block right after the try block. catch (... Exception e) { ... } CSE 1020 moodle.yorku.ca Throwing


slide-1
SLIDE 1

How to Handle Exceptions

Step 1 Place a try block around the statement(s) that may throw the exception. try { ... } Step 2 Place a catch block right after the try block. catch (... Exception e) { ... }

moodle.yorku.ca CSE 1020

slide-2
SLIDE 2

Throwing Exceptions

Question How can the client throw an exception?

moodle.yorku.ca CSE 1020

slide-3
SLIDE 3

Throwing Exceptions

Question How can the client throw an exception? Answer throw new ...Exception(...);

moodle.yorku.ca CSE 1020

slide-4
SLIDE 4

Throwing Exceptions

Question How can the client throw an exception? Answer throw new ...Exception(...); Question Why would a client ever throw an exception?

moodle.yorku.ca CSE 1020

slide-5
SLIDE 5

Throwing Exceptions

Question How can the client throw an exception? Answer throw new ...Exception(...); Question Why would a client ever throw an exception? Answer For example, the client may want to separate the error handling code from the rest.

moodle.yorku.ca CSE 1020

slide-6
SLIDE 6

Check11D

Write a program that reads from the user a mathematical expression that may contain nested precedence characters, which are the following: parentheses ( and ), brackets [ and ], and braces { and }. For example, the input string can be {a-b*[c/(d+e)]+f*[g-h*{i+j*k}]-m/(n+l)/w} The program should ignore all other characters and focus only on the above precedence characters. Specifically, it should determine whether these characters are “OK”, “Imbalanced”, “Overlapping”

  • r “Too Deeply Nested”.

moodle.yorku.ca CSE 1020

slide-7
SLIDE 7

Check11D

Enter expression [a+b*(a-d]) Overlapping Enter expression (a+(b-[a/d]) Imbalanced Enter expression (a+b)]-a Imbalanced Enter expression {a-[b/(a+d)]} OK Enter expression (((([[a+b]/a]+d)+e)+f) Too Deeply Nested

moodle.yorku.ca CSE 1020

slide-8
SLIDE 8

Check11D

Create an instance of the type.lib.CharStack class, an

  • rdered collection of chars, using its default constructor.

Scan the input string character by character, left to right. Ignore characters different from the precedence characters. If an open precedence character is encountered (i.e., ( or [ or {), store something on the stack. If a close precedence character is encountered, remove from the stack the character that was stored last. If the removed character corresponds to the close precedence character then continue scanning. Otherwise, end the program with the “Overlapping” message.

moodle.yorku.ca CSE 1020

slide-9
SLIDE 9

Check11D

If you need to remove a character from the stack but find it empty, or you complete scanning the string but find the stack not empty, then end the program with the “Imbalanced” message. If you need to store something in the stack but find it full, then end the program with the “Too Deeply Nested” message. If the program did not end in one of the above cases, print “OK.”

moodle.yorku.ca CSE 1020

slide-10
SLIDE 10

A Multiclass Application

CSE 1020 moodle.yorku.ca

moodle.yorku.ca CSE 1020

slide-11
SLIDE 11

UML class diagrams

Problem Generate UML class diagrams for classes from their code.

moodle.yorku.ca CSE 1020

slide-12
SLIDE 12

UML class diagrams

Problem Generate UML class diagrams for classes from their code. Problem Generate a UML class diagram for a class and its components (aggregation) from its code.

moodle.yorku.ca CSE 1020

slide-13
SLIDE 13

Aggregation

Definition Aggregation is a binary relation on classes. The pair (A, P) of classes is in the aggregation relation if class A (aggregate) has an attribute of type P (part). The aggregation relation is also known as the has-a relation. Instead of saying that (A, P) is in the aggregation relation, we

  • ften simply say that A has-a P.

moodle.yorku.ca CSE 1020

slide-14
SLIDE 14

UML class diagrams

Problem Prompt the user “Enter class name: ”, read the user’s input, and print the names of all components of the class. Reprompt the user if the class cannot be found.

moodle.yorku.ca CSE 1020

slide-15
SLIDE 15

UML class diagrams

Problem Prompt the user “Enter class name: ”, read the user’s input, and print the class and all its components using PlantUML.

moodle.yorku.ca CSE 1020

slide-16
SLIDE 16

PlantUML

@startuml class AbstractFoods interface List interface Map AbstractFoods o-- Map AbstractFoods o-- List AbstractFoods o-- Map @enduml

moodle.yorku.ca CSE 1020

slide-17
SLIDE 17

PlantUML

moodle.yorku.ca CSE 1020

slide-18
SLIDE 18

PlantUML

StringBuffer diagram = ...; SourceStringReader reader = new SourceStringReader(diagram.toString()); String fileName = ...; File file = new File(fileName); OutputStream stream = new FileOutputStream(file); reader.generateImage(stream);

moodle.yorku.ca CSE 1020

slide-19
SLIDE 19

To do

Study the remainder of Chapter 11.

moodle.yorku.ca CSE 1020