쌍용교육(JAVA)/JavaScript

쌍용교육 -JavaScript수업 38일차 exception

구 승 2024. 4. 15. 14:14

s01_exception.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>예외처리</title>
</head>
<body>
<script type="text/javascript">
	//예외가 발생하지 않는 경우
	document.write('1<br>');
	try{
		//예외가 발생할 가능성이 있는 문구
		document.write('2<br>');
	}catch(Exception){
		document.write('3<br>');
	}
	document.write('4<br>');
	document.write('---------------------<br>');
	
	//예외가 발생한 경우
	document.write('1<br>');
	try{
		document.write(num); //예외 발생
		document.write('2<br>');
	}catch(Exception){
		document.write('3<br>');
		
	}
	document.write('4');
</script>
</body>
</html>

s02_exception.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>예외처리</title>
</head>
<body>
<script type="text/javascript">
	try{
		let result=10/0;
		
		if(!isFinite(result)){//수를 0으로 나눴을 떄 진입
			//예외를 일부러 발생시킴
			throw 'DivideByZeroException'; 
		}
	}catch(exception){
		document.write(exception);
	}
	document.write('<br>프로그램 끝!!');
</script>
</body>
</html>

s03_exception.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>예외처리</title>
</head>
<body>
<script type="text/javascript">
	try{
		let result=10/0;
		
		if(!isFinite(result)){//수를 0으로 나눴을 떄 진입
			//예외를 일부러 발생시킴
			throw 'DivideByZeroException'; 
		}
	}catch(exception){
		document.write(exception);
	}finally{
		document.write('<br>오류가 있거나 또는 없어도 반드시 수행');
	}
	document.write('<br>프로그램 끝!!');
</script>
</body>
</html>