[Java] Error๐Ÿ˜ก๐Ÿคฌ

soyeonยท2022๋…„ 7์›” 14์ผ
0
post-thumbnail

Error

Compile time error

: ์ฝ”๋“œ ์ž์ฒด๋ฅผ ์ž˜๋ชป ์ž‘์„ฑํ•œ ๊ฒƒ

Run time error

: ์‹คํ–‰ ์‹œ์— ๋ฐœ์ƒํ•˜๋Š” ์—๋Ÿฌ

  • Error
    : ํ”„๋กœ๊ทธ๋žจ์ด ์Šค์Šค๋กœ ํ•ด๊ฒฐํ•  ์ˆ˜ ์—†๋Š” ์˜ค๋ฅ˜. ํ”„๋กœ๊ทธ๋žจ์ด ์ง€์†๋  ์ˆ˜ ์—†๋Š” ์˜ค๋ฅ˜
  • Exception
    : ํ•ด๊ฒฐ ๊ฐ€๋Šฅํ•œ ์˜ค๋ฅ˜
    ์ตœ์ƒ์œ„ class๊ฐ€ Exception class์ด๋‹ค. ํ•˜๋‹จ์— ์—ฌ๋Ÿฌ Exception class๊ฐ€ ์กด์žฌํ•œ๋‹ค.
    ex) NullPointerException
    ํ”„๋กœ๊ทธ๋žจ์„ ์‹คํ–‰ํ•˜๋‹ค๊ฐ€ Exception์ด ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๋Š” ์ƒํ™ฉ์— ์ฒ˜ํ•˜๋ฉด, JVM์ด ํ˜„์žฌ ์˜ค๋ฅ˜์ƒํ™ฉ์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ๋ชจ์•„ instance๋ฅผ ์ƒ์„ฑํ•œ๋‹ค. ์ด ๊ฐ์ฒด๋ฅผ ์šฐ๋ฆฌ๊ฐ€ ํ”„๋กœ๊ทธ๋žจ์ ์œผ๋กœ ์ฒ˜๋ฆฌ๋ฅผ ํ•ด์•ผ ํ•œ๋‹ค. ๋งŒ์•ฝ, ์ž˜ ์ฒ˜๋ฆฌ๋œ๋‹ค๋ฉด ํ”„๋กœ๊ทธ๋žจ์ด ๊ฐ•์ œ์ข…๋ฃŒ ๋˜์ง€ ์•Š๊ณ  ์ง€์†์ ์ธ ์ˆ˜ํ–‰์ด ๊ฐ€๋Šฅํ•˜๋‹ค. ํ•˜์ง€๋งŒ ์ฒ˜๋ฆฌํ•˜์ง€ ์•Š๋Š”๋‹ค๋ฉด ํ”„๋กœ๊ทธ๋žจ์ด ๊ฐ•์ œ์ข…๋ฃŒ ๋œ๋‹ค.

์˜ˆ์™ธ ์ฒ˜๋ฆฌ ๊ตฌ๋ฌธ (try catch finally)

try : Exception์ด ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๋Š” ๊ตฌ๋ฌธ์„ ๋„ฃ๋Š”๋‹ค.
catch : ๋งŒ๋“ค์–ด์ง„ Exception ๊ฐ์ฒด๋ฅผ ์žก์•„์„œ ์ฒ˜๋ฆฌํ•œ๋‹ค.
finally : ์˜ค๋ฅ˜๊ฐ€ ์žˆ๊ฑด ์—†๊ฑด ๋ฌด์กฐ๊ฑด ์‹คํ–‰๋œ๋‹ค.

์—ฐ์Šต ์ฝ”๋“œ

  • ์˜ค๋ฅ˜ ๋ฐœ์ƒ ์ฝ”๋“œ
    : 0์œผ๋กœ ๋‚˜๋ˆ„์–ด์„œ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค. "ํ”„๋กœ๊ทธ๋žจ ์‹œ์ž‘!"๋งŒ ์ถœ๋ ฅ๋˜๊ณ , Exception์ด ๋ฐœ์ƒํ•œ๋‹ค.
package lecture0714;

public class Main {
	public static void main(String[] args) {
		System.out.println("ํ”„๋กœ๊ทธ๋žจ ์‹œ์ž‘!");
		
		int result = 10 / 0;  // ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.
		
		System.out.println("ํ”„๋กœ๊ทธ๋žจ ์ข…๋ฃŒ");
	}
}
  • ์˜ˆ์™ธ ์ฒ˜๋ฆฌ ์ฝ”๋“œ
    : ํ”„๋กœ๊ทธ๋žจ์ด ์ข…๋ฃŒ๋˜์ง€ ์•Š๊ณ  ์‹คํ–‰๋œ๋‹ค.
package lecture0714;

public class Main {
	public static void main(String[] args) {
		System.out.println("ํ”„๋กœ๊ทธ๋žจ ์‹œ์ž‘!");
		
		try {  // exception์ด ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๋Š” ์ฝ”๋“œ
			int result = 10 / 0;  
		} catch(ArithmeticException e) {
			// exception์„ ์ฒ˜๋ฆฌํ•˜๋Š” ์ฝ”๋“œ
			System.out.println("์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค!");
		}
		
		System.out.println("ํ”„๋กœ๊ทธ๋žจ ์ข…๋ฃŒ");
	}
}
  • ์ƒˆ๋กœ์šด ์˜ค๋ฅ˜ ๋ฐœ์ƒ ์ฝ”๋“œ
    : ์ฒ˜๋ฆฌํ•œ ์˜ˆ์™ธ์™€ ๋‹ค๋ฅธ ์ข…๋ฅ˜์˜ ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•˜์˜€๋‹ค. catch๋ฌธ์„ ์‚ฌ์šฉํ–ˆ์–ด๋„ ์˜๋ฏธ๊ฐ€ ์—†๋‹ค. NullPointerException์„ ์ฒ˜๋ฆฌํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹ค.
package lecture0714;

public class Main {
	public static void main(String[] args) {
		System.out.println("ํ”„๋กœ๊ทธ๋žจ ์‹œ์ž‘!");
		
		try {  // exception์ด ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๋Š” ์ฝ”๋“œ
			Object obj = null;
			System.out.println(obj.toString());  
		} catch(ArithmeticException e) {
			// exception์„ ์ฒ˜๋ฆฌํ•˜๋Š” ์ฝ”๋“œ
			System.out.println("์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค!");
		}
		
		System.out.println("ํ”„๋กœ๊ทธ๋žจ ์ข…๋ฃŒ");
	}
}
  • ์˜ˆ์™ธ ์ฒ˜๋ฆฌ ์ฝ”๋“œ
    : catch๋ฅผ ์ถ”๊ฐ€ํ•ด์„œ ์ƒˆ๋กœ์šด ์˜ˆ์™ธ๋ฅผ ์ฒ˜๋ฆฌํ•˜๋ฉด ๋œ๋‹ค.
package lecture0714;

public class Main {
	public static void main(String[] args) {
		System.out.println("ํ”„๋กœ๊ทธ๋žจ ์‹œ์ž‘!");
		
		try {  // exception์ด ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๋Š” ์ฝ”๋“œ
			Object obj = null;
			System.out.println(obj.toString());  
		} catch(ArithmeticException e) {
			// exception์„ ์ฒ˜๋ฆฌํ•˜๋Š” ์ฝ”๋“œ
			System.out.println("์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค!");
		} catch(NullPointerException e) {
			System.out.println("์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค!");
		}
		
		System.out.println("ํ”„๋กœ๊ทธ๋žจ ์ข…๋ฃŒ");
	}
}
  • finally ์‚ฌ์šฉ ์ฝ”๋“œ
package lecture0714;

public class Main {
	public static void main(String[] args) {
		System.out.println("ํ”„๋กœ๊ทธ๋žจ ์‹œ์ž‘!");
		
		try {  // exception์ด ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๋Š” ์ฝ”๋“œ
			Object obj = null;
			System.out.println(obj.toString()); 
			int result = 10 / 0;
		} catch(ArithmeticException e) {
			// exception์„ ์ฒ˜๋ฆฌํ•˜๋Š” ์ฝ”๋“œ
			System.out.println("ArithmeticException ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค!");
		} catch(NullPointerException e) {
			System.out.println("NullPointerException ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค!");
		} finally {
			// ๋ฌด์กฐ๊ฑด ์‹คํ–‰๋œ๋‹ค.
			System.out.println("finally ์‹คํ–‰");
		}
		
		System.out.println("ํ”„๋กœ๊ทธ๋žจ ์ข…๋ฃŒ");
	}
}
  • ๋ชจ๋“  exception ์ฒ˜๋ฆฌํ•˜๊ธฐ ์ฝ”๋“œ
package lecture0714;

public class Main {
	public static void main(String[] args) {
		System.out.println("ํ”„๋กœ๊ทธ๋žจ ์‹œ์ž‘!");
		
		try {  // exception์ด ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๋Š” ์ฝ”๋“œ
			Object obj = null;
			System.out.println(obj.toString()); 
			int result = 10 / 0;
		} catch(Exception e) {
			// exception์„ ์ฒ˜๋ฆฌํ•˜๋Š” ์ฝ”๋“œ
			System.out.println("์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค!");
		}
		
		System.out.println("ํ”„๋กœ๊ทธ๋žจ ์ข…๋ฃŒ");
	}
}

์ƒ์œ„ Exception์ด catch๋ฌธ์— ๋จผ์ € ๋“ฑ์žฅํ•˜๋ฉด ์•ˆ๋œ๋‹ค.

public class Main {
	public static void main(String[] args) {
		System.out.println("ํ”„๋กœ๊ทธ๋žจ ์‹œ์ž‘!");
		
		try {  // exception์ด ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๋Š” ์ฝ”๋“œ
			Object obj = null;
			System.out.println(obj.toString()); 
			int result = 10 / 0;
		} catch(Exception e) {
			// exception์„ ์ฒ˜๋ฆฌํ•˜๋Š” ์ฝ”๋“œ
			System.out.println("์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค!");
		} catch(NullPointerException e) {  // ์˜ค๋ฅ˜ ๋ฐœ์ƒ
			System.out.println("์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค!");
		}
		
		System.out.println("ํ”„๋กœ๊ทธ๋žจ ์ข…๋ฃŒ");
	}
}

0๊ฐœ์˜ ๋Œ“๊ธ€