Java Seminar 6 Exception Exception public class Main
Java Seminar 6
Exception
Exception public class Main { public static void main(String[] args) { int num 1 = 10; int num 2 = 0; if (num 2 != 0) { System. out. println(num 1 / num 2); } else { System. out. println("나눌 수 없습니다. "); } } } 전통 적인 방법은 예방이다. if문을 사용해서 나누지 못하는 케이스를 제거하자.
Exception package com. jiharu. main; public class Main { public static void main(String[] args) { int num 1 = 10; int num 2 = 0; try { System. out. println(num 1 / num 2); } catch (Arithmetic. Exception e) { System. out. println("나눌 수 없습니다. "); } } } try-catch문은 try문안에서 일어나는 Exception(예외)를 감지한다. 해당 하는 예외가 일어났을 경우 그 예외에 해당하는 문제를 해결할 수 있다.
Exception package com. jiharu. main; import java. util. Scanner; public class Main { private static Scanner sc; public static void main(String[] args) { sc = new Scanner(System. in); try { System. out. print("첫번째 숫자 입력 : "); int num 1 = sc. next. Int(); System. out. print("두번째 숫자 입력 : "); int num 2 = sc. next. Int(); System. out. println(num 1 / num 2); } catch (Arithmetic. Exception e) { System. out. println("나눌 수 없습니다. "); } } } 다른 에러도 일어날 수 있다면 어떻게 해야할까?
Exception package com. jiharu. main; import java. util. Input. Mismatch. Exception; import java. util. Scanner; public class Main { private static Scanner sc; public static void main(String[] args) { sc = new Scanner(System. in); try { System. out. print("첫번째 숫자 입력 : "); int num 1 = sc. next. Int(); System. out. print("두번째 숫자 입력 : "); int num 2 = sc. next. Int(); System. out. println(num 1 / num 2); } catch (Arithmetic. Exception e) { System. out. println("나눌 수 없습니다. "); } catch (Input. Mismatch. Exception e) { System. out. println("타입 매칭이 되지 않습니다. "); } } } 만약 둘이상을 한꺼번에 처리하는 방법은 무엇일까?
Exception – 최상위 클래스 Exception사용 package com. jiharu. main; import java. util. Scanner; public class Main { private static Scanner sc; public static void main(String[] args) { sc = new Scanner(System. in); try { System. out. print("첫번째 숫자 입력 : "); int num 1 = sc. next. Int(); System. out. print("두번째 숫자 입력 : "); int num 2 = sc. next. Int(); System. out. println(num 1 / num 2); } catch (Exception e) { System. out. println("타입 매칭이 되지 않습니다. "); } } }
안드로이드 프로그래밍 <? xml version="1. 0" encoding="utf-8"? > <!--activity_main. xml--> <Linear. Layout xmlns: android="http: //schemas. android. com/apk/res/android" xmlns: app="http: //schemas. android. com/apk/res-auto" xmlns: tools="http: //schemas. android. com/tools" android: layout_width="match_parent" android: layout_height="match_parent" tools: context="com. jiharu. testapp. Main. Activity"> </Linear. Layout> 레이아웃을 Linear로 만들어준다.
안드로이드 프로그래밍 <? xml version="1. 0" encoding="utf-8"? > <!--activity_main. xml--> <Linear. Layout xmlns: android="http: //schemas. android. com/apk/res/android" xmlns: app="http: //schemas. android. com/apk/res-auto" xmlns: tools="http: //schemas. android. com/tools" android: layout_width="match_parent" android: layout_height="match_parent" tools: context="com. jiharu. testapp. Main. Activity"> </Linear. Layout> 레이아웃을 Linear로 만들어준다.
안드로이드 프로그래밍 <? xml version="1. 0" encoding="utf-8"? > <!--activity_main. xml--> <Linear. Layout xmlns: android="http: //schemas. android. com/apk/res/android" xmlns: app="http: //schemas. android. com/apk/res-auto" xmlns: tools="http: //schemas. android. com/tools" android: layout_width="match_parent" android: layout_height="match_parent" android: orientation="vertical" tools: context="com. jiharu. testapp. Main. Activity"> <Edit. Text android: id="@+id/edit. Text" android: layout_width="match_parent" android: layout_height="wrap_content" android: ems="10" android: input. Type="text. Person. Name" /> <Edit. Text android: id="@+id/edit. Text 2" android: layout_width="match_parent" android: layout_height="wrap_content" android: ems="10" android: input. Type="text. Person. Name" /> <Button android: id="@+id/button" android: layout_width="match_parent" android: layout_height="wrap_content" android: text="계산" /> <Text. View android: id="@+id/text. View" android: layout_width="match_parent" android: layout_height="wrap_content" android: text="결과: " /> </Linear. Layout> 다시 text모드로 보면 위와 같이 나온다.
안드로이드 프로그래밍 //Main. Activity. java package com. jiharu. testapp; import android. support. v 7. app. App. Compat. Activity; import android. os. Bundle; import android. view. View; import android. widget. Button; import android. widget. Edit. Text; import android. widget. Text. View; public class Main. Activity extends App. Compat. Activity { @Override protected void on. Create(Bundle saved. Instance. State) { super. on. Create(saved. Instance. State); set. Content. View(R. layout. activity_main); final Button btn = (Button) find. View. By. Id(R. id. button); final Text. View tv = (Text. View) find. View. By. Id(R. id. text. View); final Edit. Text et 1 = (Edit. Text) find. View. By. Id(R. id. edit. Text); final Edit. Text et 2 = (Edit. Text) find. View. By. Id(R. id. edit. Text 2); btn. set. On. Click. Listener(new View. On. Click. Listener() { @Override public void on. Click(View view) { tv. set. Text("결과: "+(Integer. parse. Int(et 1. get. Text(). to. String()) +Integer. parse. Int(et 2. get. Text(). to. String()))); } } on. Create안에서 변수를 사용할 경우 final을 붙혀준다.
안드로이드 프로그래밍 //Main. Activity. java package com. jiharu. testapp; import android. support. v 7. app. App. Compat. Activity; import android. os. Bundle; import android. view. View; import android. widget. Button; import android. widget. Edit. Text; import android. widget. Text. View; public class Main. Activity extends App. Compat. Activity { private Button btn = (Button) find. View. By. Id(R. id. button); private Text. View tv = (Text. View) find. View. By. Id(R. id. text. View); private Edit. Text et 1 = (Edit. Text) find. View. By. Id(R. id. edit. Text); private Edit. Text et 2 = (Edit. Text) find. View. By. Id(R. id. edit. Text 2); @Override protected void on. Create(Bundle saved. Instance. State) { super. on. Create(saved. Instance. State); set. Content. View(R. layout. activity_main); btn. set. On. Click. Listener(new View. On. Click. Listener() { @Override public void on. Click(View view) { tv. set. Text("결과: "+(Integer. parse. Int(et 1. get. Text(). to. String()) +Integer. parse. Int(et 2. get. Text(). to. String()))); } } 외부의 필드로 선언할 경우 final선언을 하지 않는다.
- Slides: 45