public static void main(String[] args) {
int cnt = 0;
try {
Class car = Class.forName("com.jinbro.source.polymorphism.Car");
} catch (ClassNotFoundException e) {
System.out.println("해당 클래스가 존재하지않습니다");
}
//runtime exception
String str = null;
try {
System.out.println(str);
} catch (NullPointerException e) {
System.out.println(e.getMessage());
} finally {
cnt++;
}
//runtime exception
char[] cArr = new char[3];
try {
System.out.println(cArr[3]);
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("초과하셨습니다");
} finally {
cnt++;
}
//runtime exception
String str2 = "1234a";
try {
int num = Integer.parseInt(str2);
} catch (NumberFormatException e) {
System.out.println("변환할 수 없습니다");
} finally {
cnt++;
}
//runtime exception
try {
Animal animal = new Animal();
Dog dog = (Dog) animal;
} catch (ClassCastException e) {
System.out.println("잘못된 캐스팅입니다");
} finally {
cnt++;
}
System.out.println(cnt);
//multi catch(1)
try {
String msg = null;
System.out.println(msg);
} catch (Exception e) {
System.out.println("category : " + e.getMessage());
}
//multi catch(2)
try {
} catch (NullPointerException e) {} catch (NumberFormatException e) {} catch (ClassCastException e) {}
// java7~ multi catch
try {
} catch (ClassCastException | NumberFormatException | ArrayIndexOutOfBoundsException e) {
} catch (Exception e) {
}
}
// ~java6 리소스 객체 사용할 때 예외처리
FileInputStream fis = null;
try{
fis = new FileInputStream("file.txt");
} catch (IOException e){
} finally {
if(fis != null){
try {
fis.close();
} catch(IOException e){
}
}
}
// java7~ 리소스 객체 사용할 때 예외처리(1) : try-with-resources
try(FileInputStream fis1 = new FileInputStream("file.txt")){
} catch (IOException e){
}
// java7~ 리소스 객체 사용할 때 예외처리(2) : try-with-resources
try(FileInputStream fis2 = new FileInputStream("input.txt");
FileOutputStream fos = new FileOutputStream("output.txt"))
{
/* try */
} catch(IOException e){
}
class XFileInputStream implements AutoCloseable{
public static void main(String[] args) {
try(XFileInputStream xis = new XFileInputStream("jinbro.txt")){
xis.read();
throw new Exception(); //강제예외발생
}catch(Exception e){
System.out.println("예외발생!");
}
}
String file;
public XFileInputStream(String name){
file = name;
}
public void read(){
System.out.println(file + " 읽기 시작");
}
@Override
public void close() throws Exception {
System.out.println("읽기 종료");
}
}
public class ThrowsTest {
public static void main(String[] args) {
try{
ThrowsTest.method1(null);
}catch(Exception e){
System.out.println(e.getMessage());
}
}
public static void method1(String msg) throws IOException, NullPointerException{
String str = msg;
System.out.println(str);
}
}
public class JinbroException extends RuntimeException {
public JinbroException() {
}
public JinbroException(String msg) {
super(msg);
}
}
'java' 카테고리의 다른 글
[Java] 표준 API #2 - System, Class + java.lang.reflect (0) | 2017.10.01 |
---|---|
[Java] 표준 API #1 - API 소개, Object(lang), Objects(util) (0) | 2017.10.01 |
[Java] 인터페이스 그리고 OOP 다형성 (0) | 2017.09.26 |
[Java] JDBC와 RDBMS 개념 이야기 (0) | 2017.09.25 |
[Java] 객체지향과 상속#2 - 자바 다형성 구현과 상속 (0) | 2017.09.24 |
댓글