import java.io.FileInputStream;
import java.io.InputStreamReader;
class A implements Cloneable {
protected Object clone() throws CloneNotSupportException {
return super.clone();
}
}
public class CheckedException {
public static void main(String[] args) {
// 일반 예외
Thread.sleep(1000);
Class cls = Class.forName("java.lang.Objcet2");
InputStreamReader in = new InputStreamReader(System.in);
in.read();
FileInputStream fis = new FileInputStream("text.txt");
A a1 = new A();
A a2 = (A)a1.clone();
}
}
class A{}
class B extends A{}
public class UncheckedException {
public static void main(String[] args) {
// 실행예외
System.out.println(3 / 0);
A a = new A();
B b = (B) a;
int[] array = {1, 2, 3};
System.out.println(array[3]);
int num = Integer.parseInt("10!");
String str = null;
System.out.println(str.charAt(2));
}
}
try {
// 일반 예외, 실행 예외 발생 가능 코드
} catch (예외 클래스명 참조 변수명) {
// 예외가 발생했을 때 처리
} finally {
// 예외 발생 여부에 상관없이 무조건 실행
}
public class TryCatchFinally {
public static void main(String[] args) {
// 1
try {
System.out.println(3 / 0);
System.out.println("프로그램 종료");
} catch (ArithmeticException e) {
System.out.println("숫자는 0으로 나눌 수 없습니다.");
System.out.println("프로그램 종료");
}
// 2
try {
System.out.println(3 / 0);
} catch (ArithmeticException e) {
System.out.println("숫자는 0으로 나눌 수 없습니다");
} finally {
System.out.println("프로그램 종료");
}
}
}
public class MultiCatch_1 {
public static void main(String[] args) {
// 다중 X
try {
System.out.println(3 / 0);
} catch (ArithmeticException e) {
System.out.println("숫자는 0으로 나눌 수 없습니다.");
} finally {
System.out.println("프로그램 종료");
}
try {
int num = Integer.parseInt("10A");
} catch (NumberFormatException e) {
System.out.println("숫자로 바꿀 수 없습니다.");
} finally {
System.out.println("프로그램 종료");
}
System.out.println();
// 다중 O
try {
System.out.println(3 / 0);
int num = Integer.parseInt("10A");
} catch (ArithmeticException e) {
System.out.println("숫자는 0으로 나눌 수 없습니다.");
} catch (NumberFormatException e) {
System.out.println("숫자로 바꿀 수 없습니다.");
} finally {
System.out.println("프로그램 종료");
}
}
}
public class MultiCatch_2 {
public static void main(String[] args) {
try {
System.out.println(3 / 0);
int num = Integer.parseInt("10A");
} catch (NumberFormatException e) {
System.out.println("숫자로 바꿀 수 없습니다.");
} catch (Exception e) {
System.out.println("숫자는 0으로 나눌 수 없습니다.");
} finally {
System.out.println("프로그램 종료");
}
}
}
public class MultiCatch_3 {
public static void main(String[] args) {
try {
System.out.println(3 / 0);
int num = Integer.parseInt("10A");
} catch (ArithmeticException e) {
System.out.println("예외가 발생했습니다.");
} catch (NumberFormatException e) {
System.out.println("예외가 발생했습니다.");
}
// OR 적용
try {
System.out.println(3 / 0);
int num = Integer.parseInt("10A");
} catch (ArithmeticException | NumberFormatException e) {
System.out.println("예외가 발생했습니다.");
}
}
}
import java.io.IOException;
import java.io.InputStreamReader;
public class TryWithResource_1 {
public static void main(String[] args) {
System.out.println("문자을 입력하세요!");
// 리소스 자동 해제
try (InputStreamReader isr1 = new InputStreamReader(System.in);) {
char input = (char)isr1.read();
System.out.println("입력글자 = " + input);
} catch (IOException e) {
e.printStackTrace();
}
// 리소스 수동 해제
InputStreamReader isr2 = null;
try {
isr2 = new InputStreamReader(System.in);
char input = (char) isr2.read();
System.out.println("입력글자 = " + input);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (isr2 != null) {
try {
isr2.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
class A implements AutoCloseable {// <<--
String resource;
A(String resource) {
this.resource = resource;
}
@Override
public void close() throws Exception {// <<--
if (resource != null) {
resource = null;
System.out.println("리소스가 해제되었습니다.");
}
}
}
public class TryWithResource_2 {
public static void main(String[] args) {
A a1 = null;
try {
a1 = new A("특정 파일");
} catch (Exception e) {
System.out.println("예외처리");
} finally {
if (a1.resource != null) {
try {
a1.close();// <<--
} catch (Exception e){
e.printStackTrace();
}
}
}
try (A a2 = new A("특정 파일");) {
} catch (Exception e) {
System.out.println("예외처리");
}
}
}
// 1. 하위 메소드서 직접 예외를 처리
class A {
void abc() {
bcd();
}
void bcd() {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
// 2. 예외를 호출 메소드로 전가
class B {
void abc() {
try {
bcd();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
void bcd() throws InterruptedException {// <<--
Thread.sleep(1000);
}
}
public class ThrowsException_1 {
public static void main(String[] args) {
}
}
public class ThrowsException_2 {
public static void main(String[] args) throws ClassNotFoundException{// <<--
Class cls = Class.forName("java.lang.Object2");
}
}
// 1. 하위메소드에 직접 예외를 처리할 때
class A {
void abc() {
bcd();
}
void bcd() {
try {
Class cls = Class.forName("java.lang.Object");
Thread.sleep(1000);
} catch (ClassNotFoundException | InterruptedException e) {
e.printStackTrace();
}
}
}
// 2. 예외를 호출 메소드로 전가할 때
class B {
void abc() {
try {
bcd();
} catch (ClassNotFoundException | InterruptedException e) {
e.printStackTrace();
}
}
void bcd() throws ClassNotFoundException, InterruptedException{// <<--
Class cls = Class.forName("java.lang.Object");
Thread.sleep(1000);
}
}
public class ThrowsException_3 {
public static void main(String[] args) {
}
}
// 1. 사용자 일반 예외
class MyException extends Exception {
public MyException() {
super();
}
public MyException(String message) {
super(message);
}
}
// 2. 사용자 실행 예외
class MyRTException extends RuntimeException {
public MyRTException() {
super();
}
public MyRTException(String message) {
super(message);
}
}
// 3. 사용자 정의 예외 객체 생성
class A {
MyException me1 = new MyException();
MyException me2 = new MyException("예외 메시지: MyException");
MyRTException mre1 = new MyRTException();
MyRTException mre2 = new MyRTException("예외 메시지: MyRTException");
// 4. 예외 던지기(throw): 던진 시점에 예외 발생
// 방법 1. 예외 직접 처리
void abc_1(int num) {
try {
if (num > 70) {
System.out.println("정상 작동");
} else {
throw me1;// <<--
}
} catch (MyException e) {// <<--
System.out.println("예외 처리 1");
}
}
void bcd_1() {
abc_1(65);
}
// 방법 2. 예외 전가
void abc_2(int num) throws MyException{
if (num > 70) {
System.out.println("정상 작동");
} else {
throw me1;// 예외를 던진 시점에 예외 발생
}
}
void bcd_2() {
try {
abc_2(65);
} catch (MyException e) {// <<--
System.out.println("예외 처리 2");
}
}
}
public class CreateUserException {
public static void main(String[] args) {
A a = new A();
a.bcd_1();
a.bcd_2();
}
}
public class ExceptionMethod_1 {
public static void main(String[] args) {
// 없는 것
try {
throw new Exception();
} catch (Exception e) {
System.out.println(e.getMessage());
}
// 있는 것
try {
throw new Exception("예외메시지");
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
class A {
void abc() throws NumberFormatException {
bcd();
}
void bcd() throws NumberFormatException {
cde();
}
void cde() throws NumberFormatException {
int num = Integer.parseInt("10A");
}
}
public class ExceptionMethod_2 {
public static void main(String[] args) {
A a = new A();
try {
a.abc();
} catch (NumberFormatException e) {
e.printStackTrace();
}
}
}
class MinusExceptions extends Exception {
public MinusExceptions() {
super();
}
public MinusExceptions(String message) {
super(message);
}
}
class OverException extends Exception {
public OverException() {
super();
}
public OverException(String message) {
super(message);
}
}
class A {
void checkScore(int score) throws MinusExceptions, OverException{
if (score < 0) {
throw new MinusExceptions("예외 발생: 음숫값 입력");
} else if (score > 100) {
throw new OverException("예외 발생: 100점 초과");
} else {
System.out.println("정상적인 값입니다.");
}
}
}
public class UserExceptionExample {
public static void main(String[] args) {
A a = new A();
try {
a.checkScore(85);
a.checkScore(150);
} catch (MinusExceptions | OverException e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
}
}
참조
https://choiblack.tistory.com/39
https://codedragon.tistory.com/4447