17-2: JAVA checked exception

jk·2024년 1월 23일
0

kdt 풀스택

목록 보기
31/127



1. throws 에 대하여 설명하시오.

  • "throws" keyword mention which exceptions this class must handle.
  • "throws" keyword is not needed when there is "try" and "catch" keywords already.



2.checked 와 unckecked Excetpion 을 설명하시오.

  • checked exception: The exceptions are expected to occur before runtime already. So they must be handled by "try"&"catch" or "throws" before compiling.
  • unchecked exception: The exceptions have possibilities to occur in runtime but they dont occur when users run very careful way. So compiler allows to build the code contain the risks of unchecked exceptions when developers write the code.



3. 아래가 컴파일 에러가 나는 이유에 대하여 설명하시오.

try {
		int num = 6 / 0;
} catch (Exception e) {
		e.printStackTrace();
} catch (InputMismatchException e) {
		e.printStackTrace();
}
  • (inputMismatchException instanceof Exception) = true
  • So JVM is not able to reach "inputMismatchException" part.



4.예외처리(Exception Handling) UML(상속도)참고하여 그리시오.





5.아래가 에러가 나는 이유와 해결방법 2가지는?

	public static void main(String[] args)   {
//		
		Path file = Paths.get("C:\\Simple.txt");		
		BufferedWriter writer= null;
//
			writer = Files.newBufferedWriter(file);
//
	        writer.write('A');     // IOException 발생 가능
	        writer.write('Z');     // IOException 발생 가능
//	
	        if(writer != null)
	            writer.close();     // IOException 발생 가능  
//
//		
//		
	}
  • "IOException" is checked exception. So "throws" or "try"&"catch" is needed.
//
//1st simple solution
//
	//public static void main(String[] args)   {
	public static void main(String[] args) throws Exception   {
//
//2st simple solution
//
        try {
            writer = Files.newBufferedWriter(file);
            writer.write('A');     // IOException 발생 가능
            writer.write('Z');     // IOException 발생 가능
            if(writer != null)
                writer.close();     // IOException 발생 가능  
        } catch(IOException e) {
        }
profile
Brave but clumsy

0개의 댓글