class Exceptiontest
{
public static void main(String[] args)
{
try{
//실행문; - 정상프로그래밍 코드
System.out.println(args[0]); //런타임 에러
}catch(ArrayIndexOutOfBoundsException aioobe){
//실행문; - 예외 발생시 실행
System.out.println("args 배열에서 예외발생...");
}
//실행문;
System.out.println("예외범위 실행 후...");
}
}
=====================================================================
//예외처리하기 : try-catch문
import java.util.Scanner;
import java.io.*;
class Exceptiontest2
{
Scanner scan = new Scanner(System.in);
public Exceptiontest2(){
start();
}
public void start(){
int[] num = new int [3];
try{
System.out.print("수1 = ");
num[0] = scan.nextInt();
System.out.print("수2 = ");
num[1] = scan.nextInt();
num[2] = num[0] / num[1];
System.out.println(num[0]+" / "+num[1]+" = "+num[2]);
//파일의 내용 읽어오기
File f = new File("z:/KOSMO/java/class3/InnerMain.java");
FileInputStream fis = new FileInputStream(f);
byte data[] = new byte[1024];
int cnt = fis.read(data);
System.out.println(new String(data));
}catch(ArithmeticException ae){ // 예외 발생 시
//ae.printStackTrace();
System.out.println(ae.getMessage());
}catch(ArrayIndexOutOfBoundsException aioobe){
System.out.println(aioobe.getMessage());
}catch(FileNotFoundException fnfe){
System.out.println("Stream 객체생성시 에러 발생...");
}catch(IOException ie){
System.out.println("IOException 에러 발생...");
}catch(Exception e){
e.printStackTrace();
}finally{ //예외 발생과 상관없이 무조건 실행되는 곳
System.out.println("finally 실행...");
}
}
public static void main(String[] args)
{
new Exceptiontest2();
}
}
'응용 SoftWare > JAVA' 카테고리의 다른 글
[예외처리] 예외 발생시키기 (0) | 2016.12.05 |
---|---|
[예외처리] 메소드 예외처리 (0) | 2016.12.05 |
[익명 클래스] 팝업창(좌표) 만들기 (0) | 2016.12.05 |
[익명 클래스] 오버라이딩을 이용한 팝업창 만들기 (0) | 2016.12.05 |
내/외부 클래스 (0) | 2016.12.05 |