2022.11.28 JAVA 23일차 예외처리(try ~ catch ~ finally)2

2022. 11. 28. 16:58JAVA

예제1)2중 try~catch문 실행 순서

public class Exception1128 {

	public static void main(String[] args) {
		//예외처리
		
		try
		{
			System.out.println("외부 try");
			try
			{
				System.out.println("내부 try");
				Exception e = new Exception();
				throw e;
			}
			catch(Exception e)
			{
				System.out.println("(내부 try-catch) exception : " + e);
				System.out.println("예외 던지기 한번더");
				throw e;
			}
			finally
			{
				System.out.println("내부 try-catch finally문");
			}
		}
		catch(Exception e)
		{
			System.out.println("(외부 try-catch) exception : "+e);
		}
		System.out.println("예외처리 끝.");
	}

}

출력1

예제2)initCause() 메서드 사용법

예외생성 클래스

class InstallException extends Exception									//InstallException 예외 생성
{
	InstallException(String msg) 
	{
		super(msg);
	}
}
	
class SpaceException extends Exception									//SpaceException 예외 생성
{
	SpaceException(String msg)
	{
		super(msg);
	}
}

class MemoryException extends Exception									//MemoryException 예외 생성
{
	MemoryException(String msg)
	{
		super(msg);
	}
}

 

메소드 선언부

static void install() throws InstallException
	{
		try
		{
			startInstall();
			copyFiles();
		}
		catch(SpaceException e)
		{
			InstallException ie = new InstallException("설치중 예외발생 1");						//SpaceException이 발생하여 걸리면 InstallException 생성
			ie.initCause(e);				//지정된 예외를 예외로 등록하기 위해 사용.
			throw ie;						//Throwable 클래스에 정의 되어있어 어디서든 사용가능			//main으로 ie 인셉션을 던져줌
			
		}
		catch(MemoryException e)
		{
			InstallException ie = new InstallException("설치중 예외발생 2");						//MemoryException이 발생하여 걸리면 InstallException 생성
			ie.initCause(e);
			throw ie;
		}
		finally
		{
			deleteTempFiles();
		}
	}
	
	static void startInstall() throws SpaceException, MemoryException
	{
		if(!enoughSpace())
		{
			throw new SpaceException("설치 공간 부족");			//enoughSpace메서드가 false일 때 SpaceException 생성 후 startinstall 메서드를 호출한곳으로 예외를 던져줌 
		}
		
		if(!enoughMemory())
		{
			throw new MemoryException("메모리 부족");			//enoughMemory메서드가 false일 때 MemoryException 생성 후 startinstall 메서드를 호출한곳으로 예외를 던져줌
		}
	}
	
	
	static void copyFiles()
	{
		System.out.println("설치에필요한 파일 복사");
	}
	
	static void deleteTempFiles()
	{
		System.out.println("설치 후 설치시 사용한 임시파일 삭제");
	}
	
	static boolean enoughSpace()
	{
		System.out.println("설치시 필요한 공간이 충분한지 확인.");				//설치 시 설치공간 체크
		return true;
	}
	
	static boolean enoughMemory()
	{
		System.out.println("설치시 필요한 메모리 확인.");							//메모리 공간 체크
		return false;
	}

메인

public class Exception1128_3 {
	public static void main(String[] args) {
		//예외처리(연결된 예외처리)
		try														//main 에서 Exception 처리
		{
			install();
		}
		catch(InstallException e)
		{
			System.out.println("111111111111");
			e.printStackTrace();
		}
		catch(Exception e)
		{
			System.out.println("22222222222");
			e.printStackTrace();
		}
	}
}

출력1

출력2 ) initCause() 메서드 미사용 시

메인에서 install메서드 호출 시 던져주는 예외값은 InstallException 밖에 없기때문에

원래는 출력2 처럼 나오는게 정상이다

 

initCause()메서드 사용 시

예외변수1 = initCause(예외변수2) 를 실행 후

throw 에외변수1 을 던져주게 되면 

예외1,예외2 를 같이 던져주게된다

따라서 출력1처럼

e.printStackTrace(); 실행 시

설치중 예외발생2 예외와 메모리부족 예외가 같이 출력된다