Advertisement
Guest User

Untitled

a guest
Feb 10th, 2016
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.97 KB | None | 0 0
  1. *자바2
  2.  
  3. 1. 예외처리
  4. 2. java.lang 패키지
  5. 3. inner 클래스
  6. 4. java.util
  7. 5. 컬렉션 프레임워크
  8. 6. 유틸
  9. 7. 쓰레드
  10. 8. io패키지
  11. 8. awt
  12. ====================
  13. * 예외처리
  14. 1. 예외 : 에러와 비슷. 시스템에서 처리할 수 있는 상황
  15. 2. 처리 : 예외가 발생했을때 어떻게 처리할 것 인가?
  16.  
  17. 예외 Exception 클래스
  18.  
  19. if문! : 저수준의 에러처리
  20. try ~ catch : 고수준의 에러처리
  21.  
  22. 처리
  23. try : 예외가 발생할 수 있는 소스
  24. catch : 어떻게 처리할 것인가?
  25. catch : 어떻게 처리할 것인가?
  26. catch : 어떻게 처리할 것인가?
  27. catch : 어떻게 처리할 것인가? ..
  28.  
  29. finally 까지.. 내일은 throw부터 시작
  30.  
  31. ---------------------
  32.  
  33. package exceptionex;
  34.  
  35. import javax.swing.JOptionPane;
  36.  
  37. public class ExceptionTest {
  38. public static void main(String[] args) {
  39. int num1 = Integer.parseInt(JOptionPane.showInputDialog("정수1 입력"));
  40. int num2 = Integer.parseInt(JOptionPane.showInputDialog("정수2 입력"));
  41. try{
  42. System.out.println(num1/num2);
  43. }
  44. catch(ArithmeticException e) {
  45. e.printStackTrace(); // 에러문구에 대한 정보를 출력한다.
  46. //System.out.println(e.getMessage()); // 단어만 보여준다.
  47. //System.out.println("0으로 나눴습니다.");
  48. while(num2==0)
  49. {
  50. num2 = Integer.parseInt(JOptionPane.showInputDialog("0이 아닌 수를 입력하세요\n"
  51. + "정수2 입력"));
  52. }
  53. System.out.println(num1/num2);
  54. }
  55.  
  56. System.out.println("산술연산을 시작한 후.. 에러가 발생되면 이 문장을 볼 수 없어요");
  57.  
  58. /*
  59. if (num2 != 0)
  60. {
  61. System.out.println(num1/num2);
  62. }
  63. else
  64. {
  65. System.out.println("0으로 나눌 수 없어요.");
  66. }
  67. */
  68. }
  69. }
  70.  
  71. ---------------------------
  72.  
  73. package exceptionex;
  74.  
  75. public class ExceptionTest2 {
  76. static void add(int a, int b){
  77.  
  78. }
  79. public static void main(String[] args) { // args - 매개변수, parameter
  80. System.out.println(args[0]); //10
  81. System.out.println(args[1]); //20
  82. //int ar1 = Integer.parseInt(args[0]);
  83. //int ar2 = Integer.parseInt(args[1]);
  84. //System.out.println(ar1+ar2);
  85. System.out.println(args[0]+args[1]); // 1020
  86. System.out.println(Integer.parseInt(args[0]) + Integer.parseInt(args[1]));
  87. // 문자열 10,20 -> 정수 -> 10+20
  88.  
  89. //int num1 = Integer.parseInt(JOptionPane.showInputDialog("정수1 입력"));
  90.  
  91.  
  92. }
  93. }
  94.  
  95. --------------------------------
  96.  
  97. package exceptionex;
  98.  
  99. import javax.swing.JOptionPane;
  100.  
  101. public class ExceptionTest3 {
  102. /* 1. 만약 args[0]이 "0"이면 어떡하지? : 산술 익셉션
  103. * 2. 정수가 아니라면??? "123a" : 숫자형식 NumberFormat
  104. * 3. 데이터가 없다면? : NullPointer도 없는거..
  105. * 배열 [0]에서 꺼내올 게 없다.
  106. * ArrayIndexOutOfBoundException
  107. */
  108. public static void main(String[] args) {
  109.  
  110. int ar1=0;
  111. try {
  112. // 첫번째 인수를 정수로 파싱하겠다.
  113. ar1 = Integer.parseInt(args[0]);
  114. System.out.println(50/ar1); // 나눗셈 수행한다
  115.  
  116. } catch (NumberFormatException e) {
  117. // 숫자 형식이 아니다.. 이때는 새로 입력 받자.
  118. e.printStackTrace();
  119.  
  120. } catch (ArithmeticException e) {
  121. //ar1=0이 아닌데이터로 계속 입력받기
  122.  
  123. } catch (ArrayIndexOutOfBoundsException e){
  124. //args[0]에 파싱할 데이터가 없다.
  125. //ar1 = Integer.parseInt(JOptionPane.showInputDialog("나눌 수 입력");
  126.  
  127. //배열 1개를 우선 만들어 놓자(정의)
  128. // 정의 없인 에러. args가 없었으므로 지정된 공간이 없다.
  129. args = new String[1];
  130. ar1 = Integer.parseInt(args[0] = JOptionPane.showInputDialog("나눌 수 입력"));
  131.  
  132. } catch (Exception e){
  133. //기타 에러
  134. e.printStackTrace();
  135. }
  136. System.out.println("에러가 나오면 어쩔 수 없어.. 그냥 넘어가고"
  137. + "그 다음 문장을 실행하자");
  138.  
  139. }
  140. }
  141.  
  142. -----------------------------------
  143.  
  144. package exceptionex;
  145.  
  146. public class ExceptionTest4 {
  147.  
  148. public static void main(String[] args) {
  149.  
  150. int ar1=0;
  151. try {
  152. // 첫번째 인수를 정수로 파싱하겠다.
  153. ar1 = Integer.parseInt(args[0]);
  154. System.out.println(50/ar1); // 나눗셈 수행한다
  155.  
  156. } catch (Exception e){
  157. e.printStackTrace();
  158. }
  159. System.out.println("에러가 나오면 어쩔 수 없어.. 그냥 넘어가고"
  160. + " 그 다음 문장을 실행하자");
  161.  
  162. }
  163. }
  164.  
  165. --------------------------------------
  166.  
  167. package exceptionex;
  168.  
  169. public class ExceptionTest5 {
  170.  
  171. public static void main(String[] args) {
  172.  
  173. int ar1=0;
  174. try {
  175. ar1 = Integer.parseInt(args[0]);
  176. System.out.println(50/ar1);
  177.  
  178. // 하위 Exception들은 Exception보다 윗줄에 있어야 함.
  179. } catch (ArrayIndexOutOfBoundsException e){
  180. System.out.println("배열의 요소가 없음");
  181. } catch (Exception e){
  182. e.printStackTrace();
  183. }
  184. // 상위 클래스가 위에 있어서 에러가 된다.-> exception보다 먼저 등장해야한다.
  185. /*catch (ArrayIndexOutOfBoundsException e){
  186. System.out.println("배열의 요소가 없음");
  187. }*/
  188. System.out.println("에러가 나오면 어쩔 수 없어.. 그냥 넘어가고"
  189. + " 그 다음 문장을 실행하자");
  190.  
  191. }
  192. }
  193.  
  194. -------------------------------------------
  195.  
  196. package exceptionex;
  197.  
  198. public class ExceptionTest6 {
  199.  
  200. public static void main(String[] args) {
  201.  
  202. int ar1=0;
  203. try {
  204. ar1 = Integer.parseInt(args[0]);
  205. System.out.println(50/ar1);
  206.  
  207. } catch (ArrayIndexOutOfBoundsException e){
  208. System.out.println("배열의 요소가 없음");
  209. } catch (Exception e){
  210. e.printStackTrace();
  211. }
  212. // 하나만 등장한다.
  213. // 반드시 실행해야 될 내용을 적는다.
  214. finally {
  215. System.out.println("catch 실행 여부에 상관없이 반드시 통과되는 영역이다.");
  216. }
  217.  
  218. System.out.println("에러가 나오면 어쩔 수 없어.. 그냥 넘어가고"
  219. + " 그 다음 문장을 실행하자");
  220.  
  221. }
  222. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement