-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโll occasionally send you account related emails.
Already on GitHub? Sign in to your account
๐ 2๋จ๊ณ - ๋ฌธ์์ด ๋ง์ ๊ณ์ฐ๊ธฐ #5891
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
์๋
ํ์ธ์ ํจ์ฃผ๋! :)
์ด๋ฒ ๋ฏธ์
๋ฆฌ๋ทฐ๋ฅผ ๋งก์ ๊ฐ๋ฏผ์์
๋๋ค ๐
2๋จ๊ณ ๋ฏธ์
์ ๊ตฌํํด์ฃผ์
จ๋ค์! ๐ ๐ฏ
์์ํ ํผ๋๋ฐฑ ๋จ๊ฒจ๋๋ ธ๋๋ฐ ํ์ธ ๋ถํ๋๋ฆฝ๋๋ค! ๐
if (parsedValue < 0) { | ||
throw new NumberFormatException(); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
NumberFormatException
์ ์ซ์๊ฐ ์๋ ๊ฒฝ์ฐ์ผ ๋ ์ฌ์ฉํ๋ ์์ธ์
๋๋ค :)
์์๋ ์ซ์์ด๊ธฐ ๋๋ฌธ์ ์ฌ๋ฐ๋ฅธ ์์ธ๋ ์๋๊ฒ ๊ฐ์์! ๐ค
์์๋ฅผ ์ ๋ฌํ ๊ฒฝ์ฐ RuntimeException ์์ธ๊ฐ ๋ฐ์ํด์ผ ํ๋ค. (์ : โ-1,2,3โ)
์ด ์๊ตฌ์ฌํญ์ ๋ง์กฑํ๊ธฐ ์ํด ๋ฐ๋ก ์์ธ๋ฅผ ์ ํํ๋ ๋ฐฉ๋ฒ์ ์ด๋จ๊น์?
if (parsedValue < 0) { | |
throw new NumberFormatException(); | |
} | |
if (parsedValue < 0) { | |
throw new RuntimeException(); | |
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
๋ต ํด๋น ์กฐ๊ฑด์ ์ ์ ํ ์์ธ์ธ์ง์ ๋ํ ํ๋จ์ ๋์น ๊ฒ ๊ฐ์ต๋๋ค. ๋ง์ํด์ฃผ์ ๋๋ก ์์ ํ๊ฒ ์ต๋๋ค. ๊ฐ์ฌํฉ๋๋ค!
} catch (NumberFormatException e) { | ||
throw new RuntimeException(); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
๋ณ๋ try/catch ๋ฌธ์ ํ ํ์ ์์๊ฒ ๊ฐ์์!
NumberFormatException
์ unchecked exception ์ด๊ธฐ๋ ํ๊ตฌ์!
java.lang.Object
โโโ java.lang.Throwable
โโโ java.lang.Exception
โโโ java.lang.RuntimeException
โโโ java.lang.IllegalArgumentException
โโโ java.lang.NumberFormatException
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
์! @hyoojuu ๋
๋ฌธ์์ด ๊ณ์ฐ๊ธฐ์ ์ซ์ ์ด์ธ์ ๊ฐ ๋๋ ์์๋ฅผ ์ ๋ฌํ๋ ๊ฒฝ์ฐ RuntimeException ์์ธ๋ฅผ throwํ๋ค.
์ด ์๊ตฌ์ฌํญ์ ์งํค๊ธฐ ์ํด catch ํ๊ณ ๋ค์ ๋ถ๋ชจ ์์ธ๋ก ์ ํํ์ ๊ฑธ๊น์? ๐
NumberFormatException ์ Runtime ์ ์์ํ๊ณ ์๋ ์์ธ์ด๊ธฐ ๋๋ฌธ์ ๋ค์ ์์ธ๋ฅผ ์ ํํ ํ์๊ฐ ์์๊ฒ ๊ฐ์๋ณด์ฌ์ ํผ๋๋ฐฑ ๋จ๊ฒผ์ต๋๋น ๐
์ด๋ฏธ ์๊ณ ๊ณ์
จ๋ ๋ด์ฉ์ด๋ผ๋ฉด ์ค์ฝ~ ์ง๋๊ฐ์๋ฉด ๋ ๊ฒ ๊ฐ์์ ๐
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
์ ๋ต,, ์์ํ๊ณ ์๋ ๊ฒ์ ์๊ณ ์์๋๋ฐ
Unchecked Exception์ ๋ช
์์ ์ผ๋ก ์์ธ ์ฒ๋ฆฌ๋ฅผ ํ์ง ์์๋ ๋๋ค๋ ์ ์ ๋ํ ์ดํด๊ฐ ๋ถ์กฑํ๋ ๊ฒ ๊ฐ์ต๋๋ค.
์์ ํ๊ฒ ์ต๋๋ค! ๊ฐ์ฌํฉ๋๋ค.
int size = this.numbers.size(); | ||
|
||
assertThat(size).isEqualTo(3); | ||
assertThat(this.numbers).hasSize(3); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
๐
} | ||
|
||
@ParameterizedTest | ||
@ValueSource(ints = {1, 2, 3}) | ||
void ์ซ์_ํฌํจ์ฌ๋ถ_ํ์ธ(int value) { | ||
assertThat(this.numbers.contains(value)).isTrue(); | ||
assertThat(this.numbers).contains(value); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
๐
private static String[] split(String text) { | ||
Matcher matcher = PATTERN.matcher(text); | ||
if (!matcher.find()) { | ||
return text.split(DELIMITER); | ||
} | ||
|
||
String customDelimiter = matcher.group(1); | ||
String delimitedText = matcher.group(2); | ||
return delimitedText.split(customDelimiter); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
์ด๋ฒ ๋ฏธ์ ์์ ์ค์ํ ๊ธฐ๋ฅ์ ๋ด๋นํ๋ split ๋ฉ์๋์์ ๋๊ฐ์ง์ ํ์๋ฅผ ํ๊ณ ์๋๊ฒ ๊ฐ์์!
- ๊ตฌ๋ถ์๋ฅผ ์ฐพ๊ธฐ
- ๊ตฌ๋ถ์๋ฅผ ๊ธฐ์ค์ผ๋ก ๋ถ๋ฆฌ
split ์ด๋ผ๋ ๋ฉ์๋๋ 2๋ฒ์ ํด๋นํ๋ ํ์๋ง ์กด์ฌํ๊ฒ ๋ฆฌํฉํ ๋ง ํ๋๊ฑด ์ด๋จ๊น์? ๐ค
private static String[] split(String text, String delimiter) {
return text.split(delimiter);
}
์ ์ฒ๋ผ ๋ฆฌํฉํ ๋ง์ด ๋๋ ค๋ฉด ์๋์ ๊ฐ์ด ๋ฉ์๋๋ก ๋๋๋ ๋ฐฉ๋ฒ์ด ์์๊ฒ ๊ฐ์์ :)
- ๊ตฌ๋ถ์๋ฅผ ์ฐพ๊ธฐ
- ๊ตฌ๋ถ์๋ก ๋ถํ ํ text ์ถ์ถ
String delimiter = findDelimiter(text);
String textToSplit = findTextToSplit(text);
sum(toInt(split(textToSplit, delimiter);
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
๋ต ๋ง์ํด์ฃผ์ ๋๋ก 1, 2 ๊ฐ๊ฐ์ ํ์๋ง ํ๋ ๋ฉ์๋๋ก ๋ถ๋ฆฌํด๋ณด๊ฒ ์ต๋๋ค!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ํผ๋๋ฐฑ๋ฐ์ ์ํด์ฃผ์
จ๋ค์! ๐
๋ค์ ๋จ๊ณ ์งํ์์๋ ์๊ตฌ์ฌํญ ์งํํ๋ฉด์ ์ฒดํฌ๋ฆฌ์คํธ๋ฅผ ๋จผ์ ์์ฑํ ๋ค ์งํํด๋ณด์๋๊ฒ ์ถ์ฒ๋๋ฆฝ๋๋ค!
๊ทธ๋ผ ๋ค์ ๋จ๊ณ ์งํํด์ฃผ์ธ์ ๐ฅ
String delimiter = findDelimiter(text); | ||
String textToSplit = findTextToSplit(text); | ||
return sum(toInts(split(textToSplit, delimiter))); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
๐ฏ
return parsedValue; | ||
} | ||
|
||
private static void validate(int value) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
๐
No description provided.