[Java] Error๐Ÿšจ

jy9922ยท2022๋…„ 7์›” 14์ผ
0

Java

๋ชฉ๋ก ๋ณด๊ธฐ
5/13
post-thumbnail

Error

  1. compile time error
    • code ์ž์ฒด๊ฐ€ ์ž˜๋ชป ์ž‘์„ฑ๋œ ๊ฒฝ์šฐ์˜ ์—๋Ÿฌ
  2. runtime error
    • Error
      • Program์ด ์ง€์†๋  ์ˆ˜ ์—†๋Š” ์˜ค๋ฅ˜ (ํ•ด๊ฒฐ X)
    • Exception
      • ํ•ด๊ฒฐ ๊ฐ€๋Šฅํ•œ ์˜ค๋ฅ˜๋“ค์„ Class๋กœ ์ •์˜ํ•ด ๋†“์€ ๊ฒƒ
      • ํ•ด๋‹น class๋„ ์ƒ์†๊ด€๊ณ„์— ์žˆ์œผ๋ฉด class๋Š” Exception class
      • ํ•˜์œ„ ํด๋ž˜์Šค๋„ NullPointerException ๋“ฑ์ด ์กด์žฌ

Java ํ”„๋กœ๊ทธ๋žจ์„ ์‹คํ–‰ํ•˜๋‹ค๊ฐ€ ํ”„๋กœ๊ทธ๋žจ์ด ์ •์ƒ์ ์ธ ์ˆ˜ํ–‰์ด ์•ˆ๋˜๋ฉด

  • JVM์ด ํ˜„์žฌ ์˜ค๋ฅ˜ ์‚ฌํ•ญ์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ๋ชจ์•„ Exception class์˜ ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.
  • ํ•ด๋‹น ๊ฐ์ฒด๋ฅผ ์šฐ๋ฆฌ๊ฐ€ ํ”„๋กœ๊ทธ๋žจ์ ์œผ๋กœ ์ฒ˜๋ฆฌ๋ฅผ ํ•ด์•ผํ•œ๋‹ค.
    1 ) ์ž˜ ์ฒ˜๋ฆฌ๋˜๋ฉด, ๊ฐ•์ œ ์ข…๋ฃŒ๋˜์ง€ ์•Š๊ณ  ํ”„๋กœ๊ทธ๋žจ์ด ์ง€์†์ ์ธ ์ˆ˜ํ–‰์„ ํ•œ๋‹ค.
    2 ) ์ฒ˜๋ฆฌํ•˜์ง€ ์•Š์œผ๋ฉด, ํ”„๋กœ๊ทธ๋žจ์ด ๊ฐ•์ œ ์ข…๋ฃŒ ๋œ๋‹ค.
  1. ์˜ˆ์™ธ ์ฒ˜๋ฆฌ๋Š” ์•„๋ž˜์™€ ๊ฐ™์€ ๊ตฌ๋ฌธ์œผ๋กœ Exception ์ฒ˜๋ฆฌ๋ฅผ ํ•œ๋‹ค.
try {   }
catch {   }
finally {   }
/* try-catch๋ฌธ */
package lecture0714;

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

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

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

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