[JAVA] ch08-08. 예외 처리 8
·
JAVA/예제
public class Ex08 { public static void main(String[] args){ throw new RuntimeException(); //RuntimeException은 try catch 밖에서도 사용할 수 있다. 자유도가 높다. 개발자들이 많이씀. } }
[JAVA] ch08-07. 예외 처리 7
·
JAVA/예제
public class Ex07 { public static void main(String[] args){ try{ throw new Exception(); //Exception은 try catch 안에서만 쓸 수 있다. }catch(Exception e){} } }
[JAVA] ch08-06. 예외 처리 6
·
JAVA/예제
public class Ex06 { public static void main(String[] args){ try{ throw new Exception("willful");//throw: 예외처리 하겠다는 명령어. throw하는 순간 Exception객체 생성 가능, catch 블록으로 넘어감. Exception객체를 생성하면서 willful이라는 메시지를 담음. }catch(Exception e){ System.out.println(e.getMessage()); e.printStackTrace(); //객체 안에 있는 에러메시지를 전부 뽑아낼 수 있다. 발생한 시간 순서대로. } System.out.println("quit."); } }
[JAVA] ch08-05. 예외 처리 5
·
JAVA/예제
public class Ex05 { public static void main(String[] args){ System.out.println(1); System.out.println(2); try{ System.out.println(3); System.out.println(0/0); //Exception 발생. System.out.println(4); }catch(ArithmeticException e){ System.out.println(5); } System.out.println(6); } }
[JAVA] ch08-04. 예외 처리 4
·
JAVA/예제
public class Ex04 { public static void main(String[] args) { System.out.println(1); System.out.println(2); try{ System.out.println(3); System.out.println(4); }catch(Exception e){ //Exception이 없으므로 catch block 실행 x System.out.println(5); } System.out.println(6); } }
[JAVA] ch08-03. 예외 처리 3
·
JAVA/예제
public class Ex03 { public static void main(String[] args) { int number = 100; int result = 0; for(int i = 0 ; i < 10 ; i++) { try{ result = number / (int)(Math.random() * 10); System.out.println(result); }catch(ArithmeticException e){//RuntimeException, Exception 모두 사용 가능. 다형성 때문에 System.out.println("0으로 나눌 수 없습니다."); } } } }
[JAVA] ch08-02. 예외 처리 2
·
JAVA/예제
public class Ex02 { public static void main(String[] args) { int number = 100; int result = 0; for(int i = 0 ; i < 10 ; i++) { result = number / (int)(Math.random() * 10); //분모가 0이 되면 디버그가 생김 System.out.println(result); } } }
[JAVA] ch08-01. 예외 처리
·
JAVA/예제
public class Ex01 { public static void main(String[] args){ try{ //try: 정상 흐름 try{}catch(Exception e) {} //Exception이 발생한 순간 try 블럭 안에 있는 명령어들 다 건너 뛰고 catch 블럭이 실행됨. }catch(Exception e){ //catch: 비정상 흐름 try{}catch(Exception e2){} } try{}catch(Exception e){} //catch 옆에는 Exception 객체만 들어올 수 있다. } }
[JAVA] ch07-21. 팩토리 패턴(Factory Pattern)
·
JAVA/예제
public class Ex21 { //Factory Pattern // 객체를 생성하는 과정이 복잡할 경우 사용 public static void main(String[] args){ new XMLParser().parse("문서"); new HTMLParser().parse("문서"); Parsable parser = ParserManager.getParser("XML"); parser.parse("a.XML"); parser = ParserManager.getParser("HTML"); parser.parse("b.HTML"); } } interface Parsable{ void parse(String fileName); //문서의 이름을 받아오기위해서는 String타입이 적당. } class XM..
[JAVA] ch07-20. 객체 지향 20
·
JAVA/예제
public class Ex20 { public static void main(String[] args){ Fighter fighter = new Fighter(); if(fighter instanceof Thing) //fighter는 Thing에 소속되어있다.. (true)이므로 출력. System.out.println("fight instanceof Thing"); if(fighter instanceof Fightable) //fighter is instance of Fightable. System.out.println("fight instanceof Fightable"); if(fighter instanceof Movable) System.out.println("fight instanceof Mo..
[JAVA] ch07-19. 객체 지향 19
·
JAVA/예제
import java.util.ArrayList; /*library API 일부에 collection framework api 단순한 객체의 집합 {ArrayList - 제일 많이씀 ,Map, Set} 배열써도 되는데 arraylist 쓰는 이유 배열은 사이즈가 고정이되고 arraylist는 사이즈 자동조정 */ public class Ex19 { //개발하는 시점에는 계속 테스트 public static void main(String[] args){ Shopper shopper = new Shopper(); Tablet tablet = new Tablet(); PC pc = new PC(); Audio audio = new Audio(); shopper.buy(tablet); shopper.buy(pc..
[JAVA] ch07-18. 객체 지향 18
·
JAVA/예제
public class Ex18 { public static void main(String[] args){ Yoker yoker = new Yoker(); Tablet tablet = new Tablet(); PC pc = new PC(); Audio audio = new Audio(); yoker.buy(tablet); yoker.buy(pc); yoker.buy(audio); yoker.summary(); } } class Audio extends Product{ Audio(){ super(50); } public String toString(){ return "Audio"; } } class Yoker{ int money = 1000; int bonus = 0; Product[] item = new..