1 / 12

JAVA STUDY

JAVA STUDY. 예외 처리. 예외처리란 ?. 예기치 못한 상황이나 위험 요소가 있는 메소드를 호출하게 될 때에 프로그램이 정지하거나 시스템이 정지되는 상황이 발생할 수 있는데 자바에서는 이를 ‘ 예외 ’ 라고 합니다 . 예외처리란 , 이런 예외 상황이 발생했을 시에 그 상황을 처리할 수 있는 코드를 만드는 것입니다 .

coyne
Download Presentation

JAVA STUDY

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. JAVA STUDY 예외 처리

  2. 예외처리란? • 예기치 못한 상황이나 위험 요소가 있는 메소드를 호출하게 될 때에프로그램이 정지하거나 시스템이 정지되는 상황이 발생할 수 있는데 자바에서는 이를 ‘예외’라고 합니다. • 예외처리란, 이런 예외 상황이 발생했을 시에 그 상황을 처리할 수 있는 코드를 만드는 것입니다. • 그리고, 자바는 이러한 예외가 발생했을 경우 프로그램 개발자가 처리할 수 있도록 하기 위해 예외의 종류를 정의하고, 언어 수준에서 예외를 처리할 수 있는 구문들을 제공해 주고 있습니다.

  3. 어떻게 쓸까요? • try/catch 문 try { Foo f = x.doRiskyTing(); int b = f.getNum(); } catch (Exception ex) { System.out.println(“failed”); } System.out.println(“We made it!”); 위험한 부분은 try 블록에 넣습니다. 예외 상황이 발생했을 때 할 일은 catch 블록에 넣습니다.

  4. 어떻게 쓸까요? • try/catch 문 try { Foo f = x.doRiskyTing(); int b = f.getNum(); } catch (Exception ex) { System.out.println(“failed”); } System.out.println(“We made it!”); try 블록이 성공하면, catch문을 실행하지 않고, 그 밑의 코드를 실행합니다.

  5. 어떻게 쓸까요? • try/catch 문 try { Foo f = x.doRiskyTing(); int b = f.getNum(); } catch (Exception ex) { System.out.println(“failed”); } System.out.println(“We made it!”); try 블록이 실행되는 가운데, doRiskyTing()에서 예외가 발생한다고 합시다. 그러면 try 블록의 나머지 코드는 실행되지 않습니다. 그리고 catch문이 실행되고, 그 밑의 코드를 실행합니다.

  6. 어떻게 쓸까요? • try/catch 문 + finally try { Foo f = x.doRiskyTing(); int b = f.getNum(); } catch (Exception ex) { System.out.println(“failed”); } finally { System.out.println(“The end?”); } System.out.println(“We made it!”); 예외발생여부와 관계없이 무조건 실행할 내용을 지정하고자 하면, finally문을 이용합니다.

  7. 예외처리와 다형성 • 예외도 객체이므로 다른 모든 객체와 마찬가지로 Exception도 다형적으로 참조할 수 있습니다. Exception IOException ClothingException PantsException ShirtException TeeShirtException DressShirtException

  8. 예외처리와 다형성 Public void doLaundry() throws ClothingException { try { laundry.doLaundry(); } catch (ClotingExceptioncex) { // 복구코드 } try { laundry.doLaundry(); } catch (ShirtExceptioncex) { // 복구코드 } …

  9. 예외처리와 다형성 Public void doLaundry() throws ClothingException { try { laundry.doLaundry(); } catch (ClotingExceptioncex) { // 복구코드 } try { laundry.doLaundry(); } catch (ShirtException sex) { // 복구코드 } … Pants, TeeShirt, DressShirt에 대한 모든 예외를 잡을 수 있습니다. TeeShirt, DressShirt에 대한 예외만 잡을 수 있습니다.

  10. 예외처리와 다형성 Public void doLaundry() throws ClothingException { try { laundry.doLaundry(); } catch (TeeShirtExceptiontex) { // 복구코드 } catch (PantsExceptionpex) { // 복구코드 } catch (ClothingExceptioncex) { // 복구코드 }

  11. 예외처리와 다형성 Public void doLaundry() throws ClothingException { try { laundry.doLaundry(); } catch (TeeShirtExceptiontex) { // 복구코드 } catch (PantsExceptionpex) { // 복구코드 } catch (ClothingExceptioncex) { // 복구코드 } 따로 처리해야 하는 예외에 대해서는 별도의 catch 블록을 만듭시다. TeeShirtException과 PantsException을 제외한 ClothingException은 여기에서 잡아냅니다.

  12. 예외처리와 다형성 Public void doLaundry() throws ClothingException { try { laundry.doLaundry(); } catch (TeeShirtExceptiontex) { // 복구코드 } catch (ShirtException sex) { // 복구코드 } catch (ClothingExceptioncex) { // 복구코드 } catch 블록을 여러 개 사용할 때는 작은 것부터 큰 것으로 나열합니다. → 상속트리의 아래쪽에 있는 클래스에 대한 catch 블록을 코드를 먼저 나열합니다.

More Related