Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- *자바2
- 1. 예외처리
- 2. java.lang 패키지
- 3. inner 클래스
- 4. java.util
- 5. 컬렉션 프레임워크
- 6. 유틸
- 7. 쓰레드
- 8. io패키지
- 8. awt
- ====================
- * 예외처리
- 1. 예외 : 에러와 비슷. 시스템에서 처리할 수 있는 상황
- 2. 처리 : 예외가 발생했을때 어떻게 처리할 것 인가?
- 예외 Exception 클래스
- if문! : 저수준의 에러처리
- try ~ catch : 고수준의 에러처리
- 처리
- try : 예외가 발생할 수 있는 소스
- catch : 어떻게 처리할 것인가?
- catch : 어떻게 처리할 것인가?
- catch : 어떻게 처리할 것인가?
- catch : 어떻게 처리할 것인가? ..
- finally 까지.. 내일은 throw부터 시작
- ---------------------
- package exceptionex;
- import javax.swing.JOptionPane;
- public class ExceptionTest {
- public static void main(String[] args) {
- int num1 = Integer.parseInt(JOptionPane.showInputDialog("정수1 입력"));
- int num2 = Integer.parseInt(JOptionPane.showInputDialog("정수2 입력"));
- try{
- System.out.println(num1/num2);
- }
- catch(ArithmeticException e) {
- e.printStackTrace(); // 에러문구에 대한 정보를 출력한다.
- //System.out.println(e.getMessage()); // 단어만 보여준다.
- //System.out.println("0으로 나눴습니다.");
- while(num2==0)
- {
- num2 = Integer.parseInt(JOptionPane.showInputDialog("0이 아닌 수를 입력하세요\n"
- + "정수2 입력"));
- }
- System.out.println(num1/num2);
- }
- System.out.println("산술연산을 시작한 후.. 에러가 발생되면 이 문장을 볼 수 없어요");
- /*
- if (num2 != 0)
- {
- System.out.println(num1/num2);
- }
- else
- {
- System.out.println("0으로 나눌 수 없어요.");
- }
- */
- }
- }
- ---------------------------
- package exceptionex;
- public class ExceptionTest2 {
- static void add(int a, int b){
- }
- public static void main(String[] args) { // args - 매개변수, parameter
- System.out.println(args[0]); //10
- System.out.println(args[1]); //20
- //int ar1 = Integer.parseInt(args[0]);
- //int ar2 = Integer.parseInt(args[1]);
- //System.out.println(ar1+ar2);
- System.out.println(args[0]+args[1]); // 1020
- System.out.println(Integer.parseInt(args[0]) + Integer.parseInt(args[1]));
- // 문자열 10,20 -> 정수 -> 10+20
- //int num1 = Integer.parseInt(JOptionPane.showInputDialog("정수1 입력"));
- }
- }
- --------------------------------
- package exceptionex;
- import javax.swing.JOptionPane;
- public class ExceptionTest3 {
- /* 1. 만약 args[0]이 "0"이면 어떡하지? : 산술 익셉션
- * 2. 정수가 아니라면??? "123a" : 숫자형식 NumberFormat
- * 3. 데이터가 없다면? : NullPointer도 없는거..
- * 배열 [0]에서 꺼내올 게 없다.
- * ArrayIndexOutOfBoundException
- */
- public static void main(String[] args) {
- int ar1=0;
- try {
- // 첫번째 인수를 정수로 파싱하겠다.
- ar1 = Integer.parseInt(args[0]);
- System.out.println(50/ar1); // 나눗셈 수행한다
- } catch (NumberFormatException e) {
- // 숫자 형식이 아니다.. 이때는 새로 입력 받자.
- e.printStackTrace();
- } catch (ArithmeticException e) {
- //ar1=0이 아닌데이터로 계속 입력받기
- } catch (ArrayIndexOutOfBoundsException e){
- //args[0]에 파싱할 데이터가 없다.
- //ar1 = Integer.parseInt(JOptionPane.showInputDialog("나눌 수 입력");
- //배열 1개를 우선 만들어 놓자(정의)
- // 정의 없인 에러. args가 없었으므로 지정된 공간이 없다.
- args = new String[1];
- ar1 = Integer.parseInt(args[0] = JOptionPane.showInputDialog("나눌 수 입력"));
- } catch (Exception e){
- //기타 에러
- e.printStackTrace();
- }
- System.out.println("에러가 나오면 어쩔 수 없어.. 그냥 넘어가고"
- + "그 다음 문장을 실행하자");
- }
- }
- -----------------------------------
- package exceptionex;
- public class ExceptionTest4 {
- public static void main(String[] args) {
- int ar1=0;
- try {
- // 첫번째 인수를 정수로 파싱하겠다.
- ar1 = Integer.parseInt(args[0]);
- System.out.println(50/ar1); // 나눗셈 수행한다
- } catch (Exception e){
- e.printStackTrace();
- }
- System.out.println("에러가 나오면 어쩔 수 없어.. 그냥 넘어가고"
- + " 그 다음 문장을 실행하자");
- }
- }
- --------------------------------------
- package exceptionex;
- public class ExceptionTest5 {
- public static void main(String[] args) {
- int ar1=0;
- try {
- ar1 = Integer.parseInt(args[0]);
- System.out.println(50/ar1);
- // 하위 Exception들은 Exception보다 윗줄에 있어야 함.
- } catch (ArrayIndexOutOfBoundsException e){
- System.out.println("배열의 요소가 없음");
- } catch (Exception e){
- e.printStackTrace();
- }
- // 상위 클래스가 위에 있어서 에러가 된다.-> exception보다 먼저 등장해야한다.
- /*catch (ArrayIndexOutOfBoundsException e){
- System.out.println("배열의 요소가 없음");
- }*/
- System.out.println("에러가 나오면 어쩔 수 없어.. 그냥 넘어가고"
- + " 그 다음 문장을 실행하자");
- }
- }
- -------------------------------------------
- package exceptionex;
- public class ExceptionTest6 {
- public static void main(String[] args) {
- int ar1=0;
- try {
- ar1 = Integer.parseInt(args[0]);
- System.out.println(50/ar1);
- } catch (ArrayIndexOutOfBoundsException e){
- System.out.println("배열의 요소가 없음");
- } catch (Exception e){
- e.printStackTrace();
- }
- // 하나만 등장한다.
- // 반드시 실행해야 될 내용을 적는다.
- finally {
- System.out.println("catch 실행 여부에 상관없이 반드시 통과되는 영역이다.");
- }
- System.out.println("에러가 나오면 어쩔 수 없어.. 그냥 넘어가고"
- + " 그 다음 문장을 실행하자");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement