user-img
Language/Java 59
Throws (์˜ˆ์™ธ ์ฒ˜๋ฆฌ ๋ฏธ๋ฃจ๊ธฐ)
Throws ํ•ด๋‹น ๋งค์†Œ๋“œ์—์„œ ์ผ์–ด๋‚  ์ˆ˜ ์žˆ๋Š” ์˜ˆ์™ธ๋ฅผ ์ž์ฒด์ ์œผ๋กœ ํ•ด๊ฒฐ ์•ˆํ•˜๊ณ , ๋‚˜ ๋ชฐ๋ผ๋ผ ํ•˜๊ณ  ๋ฐ–์œผ๋กœ ๋˜์ง€๋Š” ๋ช…๋ น์–ด ๋งค์†Œ๋“œ๋ฅผ ํ˜ธ์ถœํ•œ ์ชฝ์—์„œ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•ด์•ผ ํ•œ๋‹ค. ์ฑ…์ž„์ „๊ฐ€ํ•˜๋Š” ๋ช…๋ น์–ด ๋งค์†Œ๋“œ๋ฅผ ํ˜ธ์ถœํ•œ ์ชฝ์—์„œ ๋˜ throws ์“ฐ๋ฉด, ๊ทธ๋ƒฅ ํ”„๋กœ๊ทธ๋žจ ํผ์ ธ๋„ ์ฑ…์ž„ ์•ˆ ์ง€๊ฒ ๋‹ค๋Š” ์†Œ๋ฆฌ์ž„. import java.io.FileWriter; import java.io.IOException; public class _07_Throws { public static void main(String[] args) { // ํ˜ธ์ถœํ•œ ๋ฉ”์ธ ๋งค์†Œ๋“œ์—์„œ ํ•ด๊ฒฐํ•˜๊ฑฐ๋‚˜, ์—ฌ๊ธฐ์„œ๋„ throws๋กœ ๋‚˜๋ชฐ๋ผ๋ผ ํ•˜๊ฑฐ๋‚˜ // ๋ฉ”์ธ ๋ฉ”์†Œ๋“œ์—์„œ๋„ throws๋กœ ๋˜์ง€๋ฉด ๊ทธ๋ƒฅ ๋ฌธ์ œ ์ƒ๊ธฐ๋Š” ๊ฑฐ ๊ฐ์•ˆํ•˜๊ณ  ๋Œ๋ฆฌ๊ฒ ๋‹จ ์†Œ๋ฆฌ์ž„. try { writeFile(); } catc..
2023.03.02
Language/Java
์‚ฌ์šฉ์ž ์ •์˜ ์˜ˆ์™ธ
์˜ˆ์™ธ๋ฅผ ํ•ญ์ƒ throw new Exception(" ์˜ˆ์™ธ ๋ฐœ์ƒ ์‹œ ํ•˜๊ณ  ์‹ถ์€ ๋ง"); ๋กœ ํ•œ๋‹ค๋ฉด, ๋งˆ์ง€๋ง‰ catch ๊ตฌ๋ฌธ์—์„œ ํ•  ์ผ์ด ๋„ˆ๋ฌด ๋งŽ๋‹ค. Exception์ด๋ž€ ํŠน์ •์ ์ด์ง€ ์•Š๊ณ  ๋ชจ๋“  ๊ฑธ ํฌ๊ด„ํ•˜๋Š” ์˜ˆ์™ธ์— ๋˜์กŒ๊ธฐ ๋•Œ๋ฌธ์—, ()์•ˆ์˜ ๋ง์ด ์•„๋‹ˆ๋ฉด ๋ฌด์Šจ ์ด์œ  ๋•œ์— ์˜ˆ์™ธ๊ฐ€ ์ผ์–ด๋‚ฌ๋Š”์ง€ ์•Œ๊ธฐ๋„ ์‰ฝ์ง€ ์•Š๋‹ค. ๋”ฐ๋ผ์„œ ์šฐ๋ฆฌ๊ฐ€ ์ง์ ‘ ํŠน์ • ์ˆœ๊ฐ„์—๋งŒ ์ž‘๋™ํ•˜๋Š” ์˜ˆ์™ธ ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค๊ณ  ๊ทธ ์˜ˆ์™ธ ๊ฐ์ฒด๋ฅผ ์ด์šฉํ•˜์—ฌ ์˜ˆ์™ธ ์ฒ˜๋ฆฌ๋ฅผ ํ• ๋ ค๊ณ  ํ•œ๋‹ค. public class _06_CustomException { public static void main(String[] args) { // ์‚ฌ์šฉ์ž ์ •์˜ ์˜ˆ์™ธ int age = 17; try { if(age < 19){ // System.out.println("๋งŒ 19์„ธ ๋ฏธ๋งŒ์—๊ฒŒ๋Š” ํŒ๋งคํ•˜์ง€ ์•Š์Šต..
2023.03.02
Language/Java
Try with Resources
1. ์„ค๋ช… AutoClosable์ด๋ž€ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•œ ํด๋ž˜์Šค์˜ ๊ฐ์ฒด๋ฅผ try๊ตฌ๋ฌธ ()์•ˆ์—์„œ ์ƒ์„ฑ ํ–ˆ์„ ๋•Œ๋ฅผ ๊ฐ€์ •ํ•œ๋‹ค. ํ•ด๋‹น ๊ฐ์ฒด๊ฐ€ try catch ๊ตฌ๋ฌธ์„ ๋‹ค ์ง€๋‚˜๊ฐ”์œผ๋ฉด, try ๊ตฌ๋ฌธ์ด ์ž๋™์œผ๋กœ close ๋งค์†Œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ close์˜ ๊ธฐ๋Šฅ์„ ์ˆ˜ํ–‰ํ•˜๊ฒŒ ํ•œ๋‹ค. ํ•ด๋‹น ์Šคํ‚ฌ์€ ํด๋ž˜์Šค๊ฐ€ AutoClosable์ด๋ผ๋Š” ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ ํ•ด์•ผ๋งŒ ์“ธ ์ˆ˜ ์žˆ๋‹ค. 2. ์ฝ”๋“œ ๋ฆฌ๋ทฐ import java.io.BufferedWriter; public class _05_TryWithResources { public static void main(String[] args) { // close ๊ตฌ๋ฌธ๋„ ์˜ˆ์™ธ์ฒ˜๋ฆฌ๊ฐ€ ํ•„์š”ํ•ด์„œ // try-catch-finally ๊ตฌ๋ฌธ ์†์— ๋˜ try catch ๊ตฌ๋ฌธ์ด ๋“ค์–ด๊ฐ„ ๊ฑธ ๋ณผ ์ˆ˜ ์žˆ๋‹ค. MyF..
2023.02.28
Language/Java
Finally ๊ตฌ๋ฌธ
1. ์„ค๋ช… ์˜ˆ์™ธ ๊ตฌ๋ฌธ์—์„œ ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•˜๊ฑด ๋ง๊ฑด ์˜ˆ์™ธ์ฒ˜๋ฆฌ ํ›„ ๋ฌด์กฐ๊ฑด ์‹คํ–‰ํ•ด์•ผํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ์ ์–ด ๋†“๋Š” ๊ตฌ๋ฌธ 2. ์ฝ”๋“œ๋ฆฌ๋ทฐ public class _04_Finally { public static void main(String[] args) { try{ System.out.println("ํƒ์‹œ์˜ ๋ฌธ์„ ์—ฐ๋‹ค."); throw new Exception("ํœด๋ฌด ํƒ์‹œ"); // System.out.println("ํƒ์‹œ์— ํƒ‘์Šนํ•œ๋‹ค."); // ์˜ˆ์™ธ๋ฅผ ๋˜์ง€๋Š” ๋ฌธ์žฅ ์ฃผ์„ ์ฒ˜๋ฆฌ ํ•˜๊ณ  ํƒ์‹œ๋ฅผ ํƒ‘์Šนํ•œ๋‹ค๋Š” ์ •์ƒ ์ถœ๋ ฅ๋ฌธ์ด ๋‚˜์™€๋„ finally ๊ตฌ๋ฌธ ์‹คํ–‰ } catch (Exception e) { System.out.println("!! ๋ฌธ์ œ ๋ฐœ์ƒ: " + e.getMessage()); } finally { System.out.pri..
2023.02.28
Language/Java
thumbnail
Throw
1. ์ •์˜ ์ผ๋ถ€๋Ÿฌ ์˜ˆ์™ธ๋ฅผ ๋ฐœ์ƒ ์‹œํ‚ค๋Š” Keyword (1)ํ˜•ํƒœ // ๋‚ด๊ฐ€ Catch ์ชฝ์œผ๋กœ ๋˜์งˆ ์ƒˆ๋กœ์šด ์˜ˆ์™ธ ๊ฐ์ฒด ์ƒ์„ฑ throw new Exception("๋ฌด์Šจ ์—๋Ÿฌ์ธ์ง€ ์„ค๋ช…"); // throw์—์„œ ๋˜์ง„ ์˜ˆ์™ธ ๊ฐ์ฒด๋Š” Catch์—์„œ ๋ฐ›์•„์„œ ์ฒ˜๋ฆฌํ•œ๋‹ค. 2. ์ฝ”๋“œ ๋ฆฌ๋ทฐ public static void main(String[] args) { //๋‚˜์ด๊ฐ€ 19์„ธ ๋ฏธ๋งŒ์ผ ๊ฒฝ์šฐ ์˜ˆ์™ธ ์ฒ˜๋ฆฌ. int age = 17; try { if(age < 19){ throw new Exception("๋งŒ 19์„ธ ๋ฏธ๋งŒ์—๊ฒŒ๋Š” ํŒ๋งคํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค."); } else { System.out.println("์ฃผ๋ฌธํ•˜์‹  ์ƒํ’ˆ ์—ฌ๊ธฐ ์žˆ์Šต๋‹ˆ๋‹ค."); } }catch (Exception e){ //์˜ค๋ฅ˜๊ฐ€ ๋ฌด์—‡์ธ์ง€ ๋ณด์—ฌ์ฃผ๋Š” ์˜ˆ์™ธ ๊ฐ์ฒด ๋งค์†Œ๋“œ e.p..
2023.02.24
Language/Java
์˜ค๋ฅ˜์— ๋”ฐ๋ฅธ ๋งž์ถค ์˜ˆ์™ธ ์ฒ˜๋ฆฌ
1. ๊ฐœ๋ณ„ ์˜ˆ์™ธ ์ฒ˜๋ฆฌ ๋ฐฉ๋ฒ• Exception ํด๋ž˜์Šค๋Š” ๋ชจ๋“  ์˜ˆ์™ธ๋“ค์˜ ์กฐ์ƒ ํด๋ž˜์Šค ์ด๋‹ค. ๊ทธ๋ž˜์„œ ๋ชจ๋“  ์˜ˆ์™ธ๋ฅผ ๋‹คํ˜•์„ฑ์˜ ์›๋ฆฌ์— ์˜ํ•ด ๋‹ค ๋ฐ›์„ ์ˆ˜ ์žˆ์—ˆ๋˜ ๊ฒƒ์ด๋‹ค. catch์˜ () ์•ˆ์— ์˜ˆ์™ธ๋ณ„๋กœ ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค์–ด์ฃผ๋ฉด, ํ•ด๋‹น ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒ ํ–ˆ์„ ์‹œ์—๋งŒ ๋ฐœ๋™ํ•˜๋Š” ์˜ˆ์™ธ ์ฒ˜๋ฆฌ ๊ตฌ๋ฌธ์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค. // ๋ฐฐ์—ด ์ธ๋ฑ์Šค๋ฅผ ๋ฒ—์–ด๋‚œ ๊ณณ์— ๋Œ€ํ•œ ์†Œ์Šค ์‹คํ–‰์„ ํ•  ๊ฒฝ์šฐ์˜ ์˜ˆ์™ธ์ฒ˜๋ฆฌ๋ฌธ, // ์ž˜๋ชป๋œ ํ˜•๋ณ€ํ™˜์ด ์ผ์–ด๋‚ฌ์„ ๊ฒฝ์šฐ์˜ ์˜ˆ์™ธ์ฒ˜๋ฆฌ๋ฌธ์„ ๋”ฐ๋กœ ๋‘์—ˆ๋‹ค. // ๋งจ ๋งˆ์ง€๋ง‰์€ ๋‚˜๋จธ์ง€ ์˜ˆ์™ธ๋“ค์— ๋Œ€ํ•œ ์ผ๊ด„ ์ฒ˜๋ฆฌ์ด๋‹ค. catch (ArrayIndexOutOfBoundsException e){ System.out.println("์ธ๋ฑ์Šค๋ฅผ ์ž˜๋ชป ์„ค์ •ํ–ˆ์–ด์š”."); } catch (ClassCastException e) { System.out.println(..
2023.02.23
Language/Java
thumbnail
Run-Time(๋Ÿฐํƒ€์ž„) ์˜ค๋ฅ˜์™€ ์˜ˆ์™ธ ์ฒ˜๋ฆฌ
1. ๋Ÿฐํƒ€์ž„ ์˜ค๋ฅ˜๋ž€? (1)๋Ÿฐํƒ€์ž„์˜ค๋ฅ˜์™€ ์ปดํŒŒ์ผ ์˜ค๋ฅ˜ ๋น„๊ต ์ข…๋ฅ˜ ์ปดํŒŒ์ผ ์—๋Ÿฌ ๋Ÿฐํƒ€์ž„ ์—๋Ÿฌ ๋œป ์†Œ์Šค์ฝ”๋“œ๋ฅผ ๊ธฐ๊ณ„์–ด๋กœ ๋ฐ”๊ฟ€ ๋•Œ (์ปดํŒŒ์ผ ์‹œ) ์ผ์–ด๋‚˜๋Š” ์—๋Ÿฌ. ๋Œ€๊ฒŒ ๋ฌธ๋ฒ•์  ์˜ค๋ฅ˜๋กœ ์ธํ•ด ์ผ์–ด๋‚œ๋‹ค. ์ฝ”๋“œ ์‹คํ–‰ ๋„์ค‘์— ์ผ์–ด๋‚˜๋Š” ์—๋Ÿฌ ์ฝ”๋“œ๊ฐ€ ๋ฌธ๋ฒ•์ ์œผ๋กœ ๋ง์€ ๋˜์ง€๋งŒ, ๋…ผ๋ฆฌ์ ์œผ๋กœ ๋ง์ด ์•ˆ๋  ๋•Œ ์ผ์–ด๋‚œ๋‹ค. Run ํ–ˆ์„ ์‹œ ์• ์ดˆ์— ๋ฌธ๋ฒ•์ ์œผ๋กœ ํ‹€๋ ค์„œ Run ์กฐ์ฐจ ์•ˆ๋œ๋‹ค. ๋Ÿฐํƒ€์ž„ ์˜ค๋ฅ˜ ์žˆ์„ ์‹œ ํ”„๋กœ๊ทธ๋žจ์€ ์ฝ”๋“œ๋ฅผ ๋๊นŒ์ง€ ์‹คํ–‰ํ•˜์ง€ ๋ชปํ•˜๊ณ  ์ข…๋ฃŒ๋œ๋‹ค. (๋Ÿฐํƒ€์ž„ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•œ ์ง€์ ์—์„œ ํ”„๋กœ๊ทธ๋žจ์ด ์ข…๋ฃŒ๋œ๋‹ค.) ์˜ˆ์‹œ int i = "๋ฌธ์ž์—ด"; (์• ์ดˆ์— ๋ง์ด ์•ˆ๋˜๋Š” ๊ตฌ์กฐ) int[] arr = new int[3]; arr[5] = 100; ํ•ด๋‹น ๋ฐฐ์—ด์€ ๊ธธ์ด๊ฐ€ 3์ด๋ฏ€๋กœ, 5์— ๊ฐ’์„ ์ง‘์–ด ๋„ฃ์„ ์ˆ˜ ์—†๊ธฐ์— ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค. ์ฝ”๋“œ ํ•œ ์ค„ ํ•œ ์ค„์€ ๋ฌธ๋ฒ•..
2023.02.23
Language/Java
[JAVA] Stream ์‹ค์Šต
1. ์ฝ”๋“œ๋ฆฌ๋ทฐ // "python", "java", "javascript","c","c++","c#" // C๋กœ ์‹œ์ž‘ํ•˜๋Š” ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด ์ถ”์ถœ /*.startsWith()๋Š” String ํด๋ž˜์Šค์˜ ๋งค์†Œ๋“œ๋กœ์„œ ()์•ˆ์˜ ๊ฐ’์ด ๋ฌธ์ž์—ด์— ์žˆ๋Š”์ง€๋ฅผ boolean์œผ๋กœ ๋ฐ˜ํ™˜ .filter()๋Š” ()์•ˆ์˜ ์กฐ๊ฑด์— ๋งž๋Š” ์š”์†Œ๋งŒ ๊ณจ๋ผ์„œ ๋งŒ๋“  ์ƒˆ ์ŠคํŠธ๋ฆผ์„ ๋ฐ˜ํ™˜ */ Arrays.stream(langs).filter(x-> x.startsWith("c")).forEach(System.out::println); System.out.println("-----------------------------------------"); // java ๋ผ๋Š” ๊ธ€์ž๋ฅผ ํฌํ•จํ•˜๋Š” ์–ธ์–ด Arrays.stream(langs).filter(x-> x.co..
2023.02.19
Language/Java