티스토리에 저장했던 글을 옮겼습니다.
https://mrcocoball.tistory.com/92
try {
예외 발생할 수 있는 부분
} catch (예외 타입 e) {
예외 발생 시 예외를 처리하는 부분
}
public class ArrayExceptionHandling {
public static void main(String[] args) {
int[] arr = {1,2,3,4,5};
try {
for (int i=0; i<=5; i++) {
System.out.println(arr[i]);
}
}catch(ArrayIndexOutOfBoundsException e) {
System.out.println(e.getMessage());
System.out.println(e.toString());
}
System.out.println("정상 수행 완료");
}
}
public class FileExceptionHandling2 {
public static void main(String[] args) {
FileInputStream fis = null;
try {
fis = new FileInputStream("a.txt");
System.out.println("read");
} catch (FileNotFoundException e) {
System.out.println(e);
return;
} finally {
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
System.out.println("finally");
}
System.out.println("처리 완료");
}
}
public class AutoCloseableObj implements AutoCloseable{
@Override
public void close() throws Exception {
System.out.println("Closing......");
}
}
public class AutoCloseTest {
public static void main(String[] args) {
AutoCloseableObj obj = new AutoCloseableObj();
try(obj) {
throw new Exception();
} catch (Exception e) {
System.out.println("Exception");
}
System.out.println("End");
}
}
public class FileExceptionHandling {
public static void main(String[] args) {
// FileInputStream fis = null;
try(FileInputStream fis = new FileInputStream("a.txt")) {
System.out.println("read");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("처리 완료");
}
}
public class ThrowException {
public Class loadClass(String fileName, String className) throws ClassNotFoundException, FileNotFoundException {
// 예외 처리를 loadClass로 넘기겠다는 뜻
FileInputStream fis = new FileInputStream(fileName);
Class c = Class.forName(className);
return c;
}
public static void main(String[] args) {
ThrowException test = new ThrowException();
try {
test.loadClass("a.txt", "abc");
} catch (ClassNotFoundException e) {
System.out.println(e);
} catch (FileNotFoundException e) {
System.out.println(e);
/* } catch (FileNotFoundException | ClassNotFoundException e) {
System.out.println(e); */ // 한번에 처리
} catch (Exception e) {
System.out.println(e);
}
System.out.println("end");
}
}
/* } catch (FileNotFoundException | ClassNotFoundException e) {
System.out.println(e); */ // 한번에 처리
ThrowException test = new ThrowException();
try {
test.loadClass("a.txt", "abc");
} catch (ClassNotFoundException e) {
System.out.println(e);
} catch (FileNotFoundException e) {
System.out.println(e);
/* } catch (FileNotFoundException | ClassNotFoundException e) {
System.out.println(e); */ // 한번에 처리
} catch (Exception e) {
System.out.println(e);
}
System.out.println("end");
}
public class PasswordException extends Exception {
public PasswordException(String message) {
super(message);
}
}
public class PasswordTest {
private String password;
public String getPassword() {
return password;
}
public void setPassword(String password) throws PasswordException {
if(password == null) {
throw new PasswordException("비밀번호는 null 일 수 없다와");
}
else if (password.length() < 5) {
throw new PasswordException("비밀번호는 5자 이상이어야 한다와");
}
else if (password.matches("[a-zA-Z]+")) {
throw new PasswordException("비밀번호는 숫자나 특문을 포함해야한다와");
}
this.password = password;
}
public static void main(String[] args) {
PasswordTest test = new PasswordTest();
String password = null;
try {
test.setPassword(password);
System.out.println("오류 없음1");
} catch (PasswordException e) {
System.out.println(e.getMessage());
}
password = "abcd";
try {
test.setPassword(password);
System.out.println("오류 없음2");
} catch (PasswordException e) {
System.out.println(e.getMessage());
}
password = "abcde";
try {
test.setPassword(password);
System.out.println("오류 없음3");
} catch (PasswordException e) {
System.out.println(e.getMessage());
}
password = "abcde#1";
try {
test.setPassword(password);
System.out.println("오류 없음4");
} catch (PasswordException e) {
System.out.println(e.getMessage());
}
}
}