try {
예외가 발생할 만한 문장(들)
} catch(예외자료형 변수명) { //여기서 캐치
처리할 문장
} catch(예외자료형 변수명) { //여기서 캐치
처리할 문장
} catch(예외자료형 변수명) { //여기서 캐치
처리할 문장
}
public void pro() throws 예외클래스명1, 예외클래스명2... { //여러개 전파가능
예외가 발생할 만한 명령어(들)
}
public static void calcDiv(int a, int b) throws ArtithmeticException
public FileWriter(String fileName) throws IOException
RuntimeException의 후손이 아니기 때문에 직접 예외처리를 해줘야 한다.
FileWriter의 생성자는 다음과 같이 예외를 포함하고 있다.
public FileWriter(String fileName) throws IOException
throw new 예외클래스이름();
class 예외클래스명 extends Exception {
예외클래스명(String msg) {
super(msg);
}
}
int []data = {10, 20, 30, 40, 50};
public class DivTest07 {
// 두 개의 정수를 매개변수로 전달받아 나누기 한 결과를 출력하는 메소드
public static void calcDiv(int a, int b) throws ArithmeticException { // 호출하는 쪽으로 예외를 전달
int div = a / b;
System.out.println("나누기 결과: " + div);
}
public static void main(String[] args) {
try { //여기서 처리를 해준다.
calcDiv(9, 0);
} catch (ArithmeticException e) {
System.out.println("0으로 나눌 수 없어요!");
}
}
}
import java.io.FileWriter;
import java.io.IOException;
public class FileWriterTest {
public static void main(String[] args) {
try {
FileWriter fw = new FileWriter("c:/temp/hello.txt"); //해당 폴더에 파일을 생성
fw.write("hello java"); //파일 내용에 hello java가 입력되어 있다.
fw.close();
System.out.println("파일을 생성하였습니다.");
} catch (IOException e) {
System.out.println("예외가 발생되었습니다." + e.getMessage()); // e.getMessage를 하면 예외의 메시지를 알 수 있다.
}
}
}
/*
* public FileWriter(String fileName) throws IOException
*/
//FileWriter의 생성자는 다음과 같이 예외를 포함하고 있다.
//public FileWriter(String fileName)
//throws IOException
//IOException은 RuntimeException의 후손이 아니기 때문에 사용자가 반드시 예외처리를 해야 컴파일이 된다.
class NotFoundException extends Exception { //사용자 정의 예외(RuntimeException의 후손이 아니기 때문에 예외처리를 해줘야함)
public NotFoundException(String msg) {
super(msg);
}
}
class SearchArray {
public void search(int []arr, int data) throws NotFoundException {
int i;
for(i=0; i<arr.length; i++) {
if(data == arr[i]) {
break;
}
}
if(i == arr.length) {
throw new NotFoundException("찾고자 하는 " + data + "가 배열에 없습니다.");
} else {
System.out.println("찾고자 하는 " + data + "는 배열에 " + i + "번째에 있습니다.");
}
}}
public class SearchArrayTest {
public static void main(String[] args) {
try {
int []a = {10,20,30,40,50};
SearchArray sa = new SearchArray();
sa.search(a, 20);
sa.search(a, 100);
}catch(NotFoundException e) {
System.out.println("예외발생: " + e.getMessage());
}
}
}
import java.util.HashSet;
public class HashSetTest {
public static void main(String[] args) {
HashSet<String> set = new HashSet<String>(); //중복을 허용하지 않고 순서를 무시한다.
set.add("사과");
set.add("포도");
set.add("수박");
set.add("딸기");
set.add("포도"); //중복된 데이터 허용하지 않는다.
System.out.println(set);
}
}
국가의 이름을 키로 하고 수도를 값으로 하여 Map에 몇 개의 (키, 값)을 저장한다. Map에 저장된 전체 데이터를 출력하고 사용자로부터 국가 이름을 받아서 수도를 찾아서 출력해본다.
import java.util.LinkedHashMap;
import java.util.Scanner;
public class Practice07 {
public static void main(String[] args) {
LinkedHashMap<String, String> map = new LinkedHashMap<String, String>(); // 순서를 유지한다
map.put("USA", "Washington");
map.put("Japan", "Tokyo");
map.put("China", "Beijing");
map.put("UK", "London");
map.put("Korea", "Seoul");
String name;
Scanner sc = new Scanner(System.in);
System.out.print("국가이름을 입력하세요: ");
name = sc.next();
System.out.println(name + "의 수도: " + map.get(name));
}
}