Exception_01.java
Exception_02.java
Exception_03.java
RuntimeException re = new RuntimeException("2팀 지켜본다.");
try{
throw re;
}catch(RuntimeException e){
re.printStackTrace();
}
Exception e1 = new Exception("지각하지마!");
try{
throw e1;
}catch (Exception e{
System.out.println("예외 발생시키기 성공쓰~!");
System.out.println(e.getMessage()); //이유만
e.printStackTrace();// 실제 예외 발생시, 위치+이유 출력
}
try{
//예외가 발생할 수 있는 코드
}catch(Exception e){ //예외종류
// 수행한 코드에서 Exception 이라는 예외가 발생했을 때 처리할 문장
}catch(Exception e2){ //예외종류
// 수행한 코드에서 Exception 이라는 예외가 발생했을 때 처리할 문장
}
int number = 100;
int result = 0;
for(int i = 0 ; i <10 ; i++){
int random = (int)(Math.random()*5);
try{
result = number/random; //★ 그룹핑 주의
System.out.println(result); //★ 예외발생하면 result없음
}catch(ArithmeticException e){
System.out.println("0으로 나누지말라고..내가..그랬자나..");
}
}
System.out.println("종료");
Exception e1 = new Exception();
throw e1;
throw new Exception();
public class Exception_04 {
public static void main(String[] args) {
//여기가 View라고 생각
Controller.idCheck();
}
}
class Controller{
static void idCheck(){//원래는 인스턴스, 편의를 위해 static
Service.idCheck();
}
}
class Service{
static void idCheck(){
try {
Dao.idCheck(); //Dao 오류-Service가 처리, db트랜잭션 오류도 Service에서
} catch (SQLException e) {
e.printStackTrace();
}
}
}
class Dao{
static void idCheck() throws SQLException{ //여러개 가능
SQLException sq = new SQLException("ORA-0001");
throw sq;
}
}
try{
//예외가 발생할 수 있는 코드
}catch(Exception e){
//수행한 코드에서 Exception 이라는 예외가 발생했을 때 처리할 문장
}catch(Exception e){
//수행한 코드에서 Exception 이라는 예외가 발생했을 때 처리할 문장
}finally{
//예외발생에 상관없이 수행되어야 할 코드
}
public class Exception_05{
public static void main(String[] args){
try{
copyFiles();
install();
//deleteFiles();
}catch (Exception e) {
e.printStackTrace();
//deleteFiles();
}finally{
deleteFiles(); //어짜피 실행되니까 여기로 빼줌
}
}
static void copyFiles(){
}
static void install();{
}
static void deleteFiles(){
}
}
참고 잘 했더용 ^_^💘