[Java/자바] [예제] 1 ~ 100까지의 소수 구하기
·
JAVA/예제
1 ~ 100까지의 소수 구하기 Decimal.java package javamin; public class Decimal { public static void main(String[] args) { for(int i = 2; i
[Java/자바] [예제] 1 ~ 100까지의 3의 배수 구하기
·
JAVA/예제
1 ~ 100까지의 3의 배수 구하기 Drainage.java package javamin; public class Drainage { public static void main(String[] args){ int count = 1; for(int i = 1 ; i
[Java/자바] [예제] 1 ~ 100까지의 약수 구하기
·
JAVA/예제
1 ~ 100까지의 약수 구하기 Measure.java package javamin; public class Measure { public static void main(String[] args) { for(int i = 1; i
[JAVA] ch08-15. 예외 처리 15
·
JAVA/예제
public class Ex15 { public static void main(String[] args) { method1(); } static void method1(){ try{ method2();//throw new Exception(); Exception은 try catch 구문 안에서 작동해야 하므로 try catch 구문을 써줘야 한다. }catch(Exception e){} method3();//throw new RuntimeException(); RuntimeException은 try catch 구문이 필요없으므로 compile error가 안남. } static void method2() throws Exception{ throw new Exception(); } static void m..
[JAVA] ch08-13. 예외 처리 13
·
JAVA/예제
public class Ex13 { public static void main(String[] args) { try{//Exception이 발생하면 실행이 안될 수도 있다. startInstall(); copyFiles(); }catch(Exception e){//Exception이 발생하지 않으면 실행이 안될 수도 있다. e.printStackTrace(); }finally{ //반드시 실행시키고 싶은 명령이 있을 때 finally를 쓴다. 쓰는 순서는 try catch catch ..... finally 순으로. deleteTempFiles(); } } static void startInstall(){} static void copyFiles(){} static void deleteTempFiles(..
[JAVA] ch08-09. 예외 처리 9
·
JAVA/예제
public class Ex09 { public static void main(String[] args){ System.out.println(1); System.out.println(2); try{ System.out.println(3); System.out.println(0/0); System.out.println(4); }catch(ArithmeticException ae) { //산술연산예외처리 System.out.println("ArithmeticException."); //catch를 여러번 사용할 때 자식 클래스의 exception 먼저 써준다. 처음부터 Exception 객체를 쓸 경우 다음 catch들을 실행 안하기 때문에. }catch(Exception e){ System.out.prin..
[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으로 나눌 수 없습니다."); } } } }