์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
- ์๋ฐ๋ผ์ด๋ธ์คํฐ๋
- index full scan
- ์๋ฐ
- ์๋ฐ์์์ฐ๋ ๋๋ฅผ ์ฌ์ฉํ๋ ์ด์
- ๋ฐ์ดํฐ๋ฒ ์ด์ค๊ฒฉ๋ฆฌ์์ค
- effectiveJava
- ์ํฐ๋ ๋ณ์ข ๊ฐ๋ฐ์
- index skip scan
- InnoDB์ํคํ ์ฒ
- Hikari Connection Pool
- ์๋ผ์คํฑ์์น๋ฉ์ธ์ง์ฉ๋
- ์ธ๋ฑ์คํ์ค์บ
- enum ์์ฑ์ ์ ๊ทผ ์ ํ์
- mysql์ํคํ ์ฒ
- hikari cp ์ค์
- SVN
- ์ธ๋ฑ์ค์คํต์ค์บ
- Item6
- ๋ฐ์ฉ๋ ์ค๊ตญ์ด
- builder null
- ์๋ฐ์ฐ๋ ๋
- assert.notnull
- ์ํฐ๋ ๊ฐ๋ฐ์ํ์ฌ
- mysql
- ์์
- ์ดํํฐ๋ธ์๋ฐ
- ์์ดํ 15
- item15
- ์๋ฐ์์
- ๋น์ผ๊ฐ์ฒด์์ฑ
- Today
- Total
โ๐ป๊ธฐ๋กํ๋ ๋ธ๋ก๊ทธ
์์ธ์ฒ๋ฆฌ ๋ณธ๋ฌธ
๐ฉ๐ซ ์๋ฐ ๋ผ์ด๋ธ ์คํฐ๋ 8์ฃผ ์ฐจ - ์์ธ์ฒ๋ฆฌ
1. ์ค๋ฅ์ ์ข ๋ฅ
2. ์๋ฐ์์ ์์ธ ์ฒ๋ฆฌ ๋ฐฉ๋ฒ (try, catch, throw, throws, finally)
2.1 try-catch๋ฌธ์ผ๋ก ์์ธ ์ฒ๋ฆฌํ๊ธฐ
2.2 try - with -resources
2.3 ์์ธ ๋ฐ์์ํค๊ธฐ - throw
2.4 throwsํค์๋๋ฅผ ์ฌ์ฉํ์ฌ ๋ฉ์๋์์ ์์ธ ์ ์ธํ๊ธฐ
2.5 printStackTrace()์ getMessage()
3. ์๋ฐ๊ฐ ์ ๊ณตํ๋ ์์ธ ๊ณ์ธต ๊ตฌ์กฐ
4. Exception๊ณผ Error์ ์ฐจ์ด๋?
5. RuntimeException๊ณผ RE๊ฐ ์๋ ๊ฒ์ ์ฐจ์ด๋?
6. ์ปค์คํ ํ ์์ธ ๋ง๋๋ ๋ฐฉ๋ฒ
1. ์ค๋ฅ์ ์ข ๋ฅ
- ์ปดํ์ผ ์ค๋ฅ
์ฝ๋ ์์ฑ ์ค ๋ฌธ๋ฒ์ ์ธ ์ค๋ฅ
- ์คํ ์ค๋ฅ (runtime error)
์ปดํ์ผ์ ์ ์์ ์ด์์ง๋ง , ํ๋ก๊ทธ๋จ ์คํ ์ค ๋ฐ์ํ ์ ์๋ ์ค๋ฅ
์๋ฐ๋ ๋ฐ์ ๊ฐ๋ฅํ ์ค๋ฅ์ ๋ํด ์์ธ์ฒ๋ฆฌ๋ฅผ ํ์ฌ ํ๋ก๊ทธ๋จ์ด ๋น์ ์์ ์ผ๋ก ์ข ๋ฃ๋๋ ๊ฒ์ ๋ง๊ณ ๋ก๊ทธ๋ก ๋จ๊ธธ ์ ์๋ค.
2. ์๋ฐ์์ ์์ธ ์ฒ๋ฆฌ ๋ฐฉ๋ฒ (try, catch, throw, throws, finally)
2.1 try-catch๋ฌธ์ผ๋ก ์์ธ ์ฒ๋ฆฌํ๊ธฐ
try{
์์ธ๊ฐ ๋ฐ์ํ ์ ์๋ ์ฝ๋
}catch(์ฒ๋ฆฌํ ์์ธ ํ์
e){
try์์ ์์ธ ๋ฐ์ ์ ์ฌ๊ธฐ์ ์์ธ๋ฅผ ์ฒ๋ฆฌ
try์ ์ฝ๋๊ฐ ๋ชจ๋ ์คํ๋๋ค์ catch๋ก ์ค๋๊ฒ์ด ์๋๋ผ ์์ธ ๋ฐ์ ํ ๋ฐ๋ก ์ค๊ฒ๋จ
๋ฐ์ํ ์์ธ๊ฐ ์ฒ๋ฆฌํ ์์ธ ํ์
๊ณผ ๋งค์นญ์ด ๋์ด์ผํจ
}finally{
์์ธ ๋ฐ์ ์ฌ๋ถ์ ์๊ด์์ด ํญ์ ์ํ๋์ด์ผํ๋ ๋ถ๋ถ
์ฌ์ฉํ ๋ฆฌ์์ค๋ฅผ ๋ซ๋ ๋ถ๋ถ
}
try๋ธ๋ก์์ ์์ธ๊ฐ ๋ฐ์ํ๋ฉด, ์์ธ๊ฐ ๋ฐ์ํ ์์น์์ ๋ฐ๋ก ํด๋น catch ๋ธ๋ก์ผ๋ก ์ด๋ํ์ฌ try ๋ธ๋ก์์ exception ์๋ ์ฝ๋๋ ์คํ๋์ง ์๋๋ค.
System.out.println(1);
System.out.println(2);
try{
System.out.println(3);
System.out.println(0/0);//exception ๋ฐ์
System.out.println(4);//์คํ๋์ง์๋๋ค.
} catch (ArithmeticException ae) {
System.out.println(5);
}
System.out.println(6);
์์ ์ฝ๋์์ 0/0 ArithmeticException์ด ๋ฐ์ํ๋ฏ๋ก try-catch ์คํ ์์์ ๋ฐ๋ฅด๋ฉด 4๋ ์ถ๋ ฅ ๊ฒฐ๊ณผ์ ๋์ค์ง ์๋๋ค.
์์ธ๊ฐ ๋ฐ์ํ๋ฉด ๋ฐ์ํ ์์ธ์ ํด๋นํ๋ ํด๋์ค์ ์ธ์คํด์ค๊ฐ ์์ฑ๋๋ค.
- catch๋ธ๋ก์ ์ฐจ๋ก๋๋ก ๋ด๋ ค๊ฐ๋ฉด์ catch๋ธ๋ก์ ๊ดํธ ๋ด์ ์ ์ธ๋ ์ฐธ์กฐ ๋ณ์ ์ข ๋ฅ์ ์์ธ ํด๋์ค์ ์ธ์คํด์ค๋ฅผ instanceOf์ฐ์ฐ์๋ฅผ ์ด์ฉํด ๊ฒ์ฌํ๋ค.
- ํด๋น catch๋ธ๋ญ์ ์ฐพ์ผ๋ฉด catch ๋ธ๋ก์ ์ํ ํ, try-catch๋ฅผ ๋น ์ ธ๋์ค๊ฒ ๋๋ค. ๋ง์ฝ ์ฐพ์ง ๋ชปํ๋ค๋ฉด, ์์ธ ์ฒ๋ฆฌ๋์ง ์์
System.out.println(1);
System.out.println(2);
try{
System.out.println(3);
System.out.println(0/0);//exception ๋ฐ์
System.out.println(4);//์คํ๋์ง์๋๋ค.
} catch (ArithmeticException ae) {
if(ae instanceof ArithmeticException){
System.out.println("ArithmeticException instnace Check");
}
System.out.println(5);
}
System.out.println(6);
2.2 try - with -resources
fileinputstream(autoCloseable ๊ตฌํํ๊ณ ์์)๊ณผ ๊ฐ์ด ๋ฌด์กฐ๊ฑด ๋ซ์์ค์ผ ํ๋ ๊ฒฝ์ฐ
- ๋ฆฌ์์ค๋ฅผ ์๋์ผ๋ก ํด์ ํ๋๋ก ์ ๊ณตํ๋ ๊ตฌ๋ฌธ
- ์๋ฐ 7๋ถํฐ ์ ๊ณต
- ํด๋น ๋ฆฌ์์ค๊ฐ autoCloseable์ ๊ตฌํํ ๊ฒฝ์ฐ close()๋ฅผ ๋ช ์์ ์ผ๋ก ํธ์ถํ์ง ์์๋ try {} ๋ธ๋ก์์ ์คํ๋ ๋ฆฌ์์ค๋ ์ ์ฐ์ ์ธ ๊ฒฝ์ฐ๋ ์์ธ๊ฐ ๋ฐ์ํ ๊ฒฝ์ฐ ๋ชจ๋ ์๋์ผ๋ก close()๊ฐ ํธ์ถ๋จ
์๋ฐ 9 ์ดํ์์ try- with -resources ๋ฌธ
์ธ๋ถ์์ ์ ์ธํ ๋ณ์ ๋ช ๊ทธ๋๋ก ์ฌ์ฉ ๊ฐ๋ฅ
[์๋ฐ 9 ์ด์ ]
AutoCloseObj obj = new AutoCloseObj();
try (AutoCloseObj obj2 = obj){//๋ค๋ฅธ ์ฐธ์กฐ ๋ณ์๋ก ๋ค์ ์ ์ธํด์ผํจ
throw new Exception();
}catch (Exception e){
System.out.println("์์ธ ๋ถ๋ถ");
}
[์๋ฐ 9 ์ดํ]
AutoCloseObj obj = new AutoCloseObj();
try (obj){//์ธ๋ถ์์ ์ ์ธํ ๋ณ์๋ฅผ ๊ทธ๋๋ก ์ธ ์ ์์
throw new Exception();
}catch (Exception e){
System.out.println("์์ธ ๋ถ๋ถ");
}
2.3 ์์ธ ๋ฐ์์ํค๊ธฐ - throw
1. ์ฐ์ฐ์ new ํค์๋๋ฅผ ํตํด ์์ธ ํด๋์ค ๊ฐ์ฒด ์ ์ธ
Exception e = new Exception("๊ณ ์๋ก ๋ฐ์ํ ์์ธ");
2. throwํค์๋๋ฅผ ํตํด ์์ธ ๋ฐ์
throw e;
์์ ์ฝ๋๋ฅผ ํ ์ค๋ก ์ฌ์ฉ ๊ฐ๋ฅ
exception ์ธ์คํด์ค ์์ฑ ์ ์์ฑ์์ ๋ฌธ์์ด์ ๋ฃ์ผ๋ฉด ์ธ์คํด์ค์ ๋ฉ์์ง๋ก ์ ์ฅ๋์ด getMessage()๋ก ํธ์ถ ๊ฐ๋ฅ
try{
throw new Exception("๊ณ ์๋ก ๋ฐ์ํ ์์ธ");
}catch(Exception e){
e.getMessage();
}
2.4 throwsํค์๋๋ฅผ ์ฌ์ฉํ์ฌ ๋ฉ์๋์์ ์์ธ ์ ์ธํ๊ธฐ
๋ฉ์๋์ throws๋ฅผ ๋ช ์ํ๋ ๊ฒ์ ์์ธ๋ฅผ ์ฒ๋ฆฌํ๋ ๊ฒ์ด ์๋๋ผ, ์์ ์ ํธ์ถํ ๋ฉ์๋์๊ฒ ์์ธ๋ฅผ ์ ๋ฌํ๋ ๊ฒ์ด๋ค.
์ด๋ฐ ์์ผ๋ก ๊ณ์ ํธ์ถ ์คํ์ ์๋ ๋ฉ์๋์ ์ ๋ฌ๋๋ค๊ฐ main๋ฉ์๋์์๋ ์์ธ๊ฐ ์ฒ๋ฆฌ๋์ง ์๋๋ค๋ฉด, ํ๋ก๊ทธ๋จ์ด ๋น์ ์์ ์ผ๋ก ์ข ๋ฃ๋๋ค.
main()์์ Throws๋ฅผ ์ฌ์ฉํ๋ฉด ๊ฐ์ ๋จธ์ ์์ ์ฒ๋ฆฌ๋๋ค.
void method() throws Exception1, Exception2{
//๋ฉ์๋์ ๋ด์ฉ
}
2.5 printStackTrace()์ getMessage()
printStackTrace() ์์ธ ๋ฐ์ ์ ํธ์ถ ์คํ์ ์๋ ๋ฉ์๋์ ์ ๋ณด์ ์์ธ ๋ฉ์์ง๋ฅผ ํ๋ฉด์ ์ถ๋ ฅ
getMessage() ๋ฐ์ํ ์์ธ ํด๋์ค์ ์ธ์คํด์ค์ ์ ์ฅ๋ ๋ฉ์์ง๋ฅผ ์ป์ ์ ์์
System.out.println(1);
System.out.println(2);
try{
System.out.println(3);
System.out.println(0/0);//exception ๋ฐ์
System.out.println(4);//์คํ๋์ง์๋๋ค.
} catch (ArithmeticException ae) {
ae.printStaceTrace();
System.out.println(5);
}
System.out.println(6);
3. ์๋ฐ๊ฐ ์ ๊ณตํ๋ ์์ธ ๊ณ์ธต ๊ตฌ์กฐ
Error, Exception ํด๋์ค ๋ชจ๋ object์กฐ์ ํด๋์ค
์์ธ ํด๋์ค๋ค์ ๋ ๊ฐ์ง ๊ทธ๋ฃน์ผ๋ก ๋๋ ์ ์๋ค.
RuntimeException๊ณผ ์์ ํด๋์ค, Exception ํด๋์ค์ ์์๋ค
4. Exception๊ณผ Error์ ์ฐจ์ด๋?
- ์๋ฌ(error)→ ์๋ฌ ๋ฐ์ ์ ํ๋ก๊ทธ๋จ์ ๋น์ ์์ ์ธ ์ข ๋ฃ
- ์์คํ ์ ์ธ ๋ถ๋ถ, ๋ฐ๋ณต์ ์ธ ์ฌ๊ท ํธ์ถ๋ก ์ธํ stack over flow ๋ฑ ๊ฐ์ ๋จธ์ ์์ ๋ฐ์ ๋์ ๋ฉ๋ชจ๋ฆฌ๋ฅผ ๋ค ์ฌ์ฉํ ๊ฒฝ์ฐ
- → ๋ฉ๋ชจ๋ฆฌ ๋ถ์กฑ(OutOfMemoryError), ์คํ์ค๋ฒํ๋ก์ฐ(StackOverflowError)๋ฑ ์ผ๋จ ๋ฐ์ํ๋ฉด ๋ณต๊ตฌ ๋ถ๊ฐํ ์ฌ๊ฐํ ์ค๋ฅ
- ์์ธ(exception)ํ๋ก๊ทธ๋จ์์ ์ ๊ฑฐ ๊ฐ๋ฅํ ์ค๋ฅ, ์ฝ์ผ๋ ค๋ ํ์ผ์ด ์์ , ๋คํธ์ํฌ ์์ผ ์ฐ๊ฒฐ ์ค๋ฅ ๋ฑ
- → ์๋ฐ ํ๋ก๊ทธ๋จ์์ ์์ธ์ ๋ํ ์ฒ๋ฆฌ๊ฐ ๊ฐ๋ฅํจ
- → ์์ต์ด ๊ฐ๋ฅํ๋ฉฐ ๊ฐ๋ฐ์๊ฐ ๋ฏธ๋ฆฌ ์์ํ์ฌ ์์ธ์ฒ๋ฆฌ๊ฐ ๊ฐ๋ฅํจ
5. RuntimeException๊ณผ RE๊ฐ ์๋ ๊ฒ์ ์ฐจ์ด๋?
- RuntimeException
- ArrayIndexOutOfBoundsException
- NullPointerException
- ClassCastException
- ArithmeticException (์ ์๋ฅผ 0์ผ๋ก ๋๋๋ ๊ฒฝ์ฐ)
- RuntimeException(์ฃผ๋ก ํ๋ก๊ทธ๋๋จธ์ ์ค์๋ก ๋ฐ์๋ ์ ์๋ ์์ธ)๊ณผ ์ด์ธ์ Exception ํด๋์ค๋ค ( ์ฌ์ฉ์ ๋์์ ์ํด ๋ฐ์ํ๋ ๊ฒฝ์ฐ)
- RuntimException ์ด์ธ์ ํด๋์ค
- FileNotFoundException : FileNot ์กด์ฌํ์ง ์๋ ํ์ผ ์ด๋ฆ ์ ๋ ฅ
- ClassNotFoundException
- DataFormatException ๋ฑ
- ํ๋ก๊ทธ๋จ ์ฌ์ฉ์๋ค์ ์ํด ๋ฐ์ ๊ฐ๋ฅ.
6. ์ปค์คํ ํ ์์ธ ๋ง๋๋ ๋ฐฉ๋ฒ
jdk์์ ์ ๊ณต๋๋ ์์ธ ํด๋์ค ์ธ์ ์ฌ์ฉ์๊ฐ ํ์์ ์ํด ์์ธ ํด๋์ค๋ฅผ ์ ์ํ์ฌ ์ฌ์ฉ
๊ธฐ์กด jdkํด๋์ค์์ Exceptionํด๋์ค๋ฅผ ์์๋ฐ์ ์์ธ ํด๋์ค ๋ง๋ค์๋ค.
package exception;
public class IDFormatException extends Exception {
public IDFormatException (String message){
//์์ฑ์์์ exception ๋ฉ์ธ์ง ๋ฐ์
super(message);
}
}
ํ ์คํธ ํด๋์ค์์ ์ง์ ์ปค์คํ ํ ์์ธ ํด๋์ค๋ก ์์ธ์ฒ๋ฆฌ
public class IDFormatTest {
private String userID;
private String getUserID(){
return userID;
}
public void setUserID(String userID) throws IDFormatException{
//throws exception์ ๋ฏธ๋ฃธ
//throw๋ exception ๋ฐ์
if(userID == null){
throw new IDFormatException("id can not null");//์์ฑ์ ๋ฉ์ธ์ง ์ ๋ฌ
}else if(userID.length() < 8 || userID.length() > 20) {
throw new IDFormatException("id๋ 8์์ด์, 20์์ดํ๋ก ์ค์ ํ์ธ์");
}
this.userID = userID;
}
public static void main(String[] args) {
IDFormatTest idTest = new IDFormatTest();
String myId = null;
try {
idTest.setUserID(myId);
} catch (IDFormatException e) {
System.out.println(e);
}
}
}
์๋ฐ์ ์ ์ ์ฐธ๊ณ
ํจ์คํธ์บ ํผ์ค ์๋ฐ ์ฌ์ธ์ ๊ฐ์ ์ฐธ๊ณ
'Language > Java' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Enum (0) | 2021.01.28 |
---|---|
Thread - ๋ฉํฐ์ฐ๋ ๋ ํ๋ก๊ทธ๋๋ฐ (0) | 2021.01.22 |
[ JAVA ] ์ธํฐํ์ด์ค (0) | 2021.01.08 |
[ JAVA ] package (0) | 2021.01.01 |
[ JAVA ] ์์ (0) | 2020.12.25 |