'Java > Exception Handing' 카테고리의 다른 글
| 사용자 정의 예외 (0) | 2012.04.11 |
|---|---|
| 예외처리 throw사용법 (0) | 2012.04.11 |
| 예외처리 throws 사용법 (0) | 2012.04.11 |
| 예외처리 다중 catch문 사용하기 (0) | 2012.04.11 |
| 예외처리 finally 사용법 (0) | 2012.04.11 |
| 사용자 정의 예외 (0) | 2012.04.11 |
|---|---|
| 예외처리 throw사용법 (0) | 2012.04.11 |
| 예외처리 throws 사용법 (0) | 2012.04.11 |
| 예외처리 다중 catch문 사용하기 (0) | 2012.04.11 |
| 예외처리 finally 사용법 (0) | 2012.04.11 |
package com.basic;//사용자 정의 예외
class UserException extends Exception{
public UserException(String str){
super(str);
}
}
public class ExcepTest09 {
public static void main(String[] args) {
try{
int a= -11;
if(a<=0){
//사용자가 정의한 예외를 인위적으로 발생시킴
throw new UserException("양수가 아닙니다.");
}
}
catch(UserException e){
System.out.println(e.getMessage());
}
}
}양수가 아닙니다.
| 예비 (0) | 2012.04.11 |
|---|---|
| 예외처리 throw사용법 (0) | 2012.04.11 |
| 예외처리 throws 사용법 (0) | 2012.04.11 |
| 예외처리 다중 catch문 사용하기 (0) | 2012.04.11 |
| 예외처리 finally 사용법 (0) | 2012.04.11 |
package com.basic;//예외처리 throw 사용법(예외를 강제적으로)
public class ThrowEx1 {
public void methodA(String[] n)throws Exception{
if(n.length>0){
//개선된 루프 (jdk5.0이상에서만 사용가능)
//n은 인덱스 주소값을 모두 출력해서 s에 저장(?)
for(String s : n)
System.out.println(s);
}else
throw new Exception("배열에 요소가 없습니다1.");
}
public static void main(String[] args){
ThrowEx1 te = new ThrowEx1();
try{
te.methodA(args);
}catch(Exception e){//11라인 출력
System.out.println(e.getMessage());
//e.printStackTrace();//부가 적으로 자세히 나옴 jsp에 많이 사용
}
}
}배열에 요소가 없습니다1.
| 예비 (0) | 2012.04.11 |
|---|---|
| 사용자 정의 예외 (0) | 2012.04.11 |
| 예외처리 throws 사용법 (0) | 2012.04.11 |
| 예외처리 다중 catch문 사용하기 (0) | 2012.04.11 |
| 예외처리 finally 사용법 (0) | 2012.04.11 |
package com.basic; //예외처리 throws 사용법
import java.io.*; //모든 클래스를 사용하겠다는 것임(좋은 표현은 아님)
public class ThrowsEx1 {
private void printDate()
throws NumberFormatException,IOException{
System.out.print("단 입력 :");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int dan = Integer.parseInt(br.readLine());
System.out.println(dan+"단");
System.out.println("---------");
for(int i=1;i<=9;i++)
System.out.println(dan+"*"+i+"="+(dan*i));
}
public static void main(String[] args){
//throws에 에러 값을 가지고 있다가 밑에 출력부분에서 try catch에서 수행됨
ThrowsEx1 t1 = new ThrowsEx1();
try{
t1.printDate();
}catch(Exception e){
System.out.println("숫자가 아닙니다.");
}
}
}
//readLine은 IOException 예외가 발생하기 때문에 의무적으로 꼭 예외처리를 해야됨단 입력 :4
| 사용자 정의 예외 (0) | 2012.04.11 |
|---|---|
| 예외처리 throw사용법 (0) | 2012.04.11 |
| 예외처리 다중 catch문 사용하기 (0) | 2012.04.11 |
| 예외처리 finally 사용법 (0) | 2012.04.11 |
| 예외처리 5 (0) | 2012.04.11 |
package com.basic;//예외처리 5
public class ExceptionEx5 {
public static void main(String[] args) {
int var =50;
/*다중 catch문 사용하기
catch 블럭에 전달되는 객체의 타입을 명시할때
하위 타입은 위로 상위타입은 아래로*/
try{
//String -> int
int date = Integer.parseInt(args[0]);
System.out.println(var/date);
System.out.println("----------");
}
catch(NumberFormatException e){
System.out.println("숫자가 아닙니다.");
}
catch(ArrayIndexOutOfBoundsException e){
System.out.println("입력한 데이터가 없습니다.");
}
catch(ArithmeticException e){
System.out.println("0으로 나눌 순 없죠?");
}
catch(Exception e){
System.out.println("모든 예외처리는 여기서");
}
System.out.println("프로그램 종료!");
}
}
50
| 예외처리 throw사용법 (0) | 2012.04.11 |
|---|---|
| 예외처리 throws 사용법 (0) | 2012.04.11 |
| 예외처리 finally 사용법 (0) | 2012.04.11 |
| 예외처리 5 (0) | 2012.04.11 |
| 예외처리 기본4 (0) | 2012.04.11 |
package com.basic;//예외처리 finally의 사용방법
public class ExceptionEx6 {
public static void main(String[] args){
System.out.println("==예외가 발생하지 않은 경우==");
System.out.println("1");
try{
System.out.println("2");
System.out.println("3");
}catch(Exception e){
System.out.println("4");
}finally{//예외가 있던 없던 무조건 실행됨
System.out.println("5");
}
System.out.println("프로그램 종료");
}
}==예외가 발생하지 않은 경우==
package com.basic;//예외처리 finally의 사용방법
public class ExceptionEx7 {
public static void main(String[] args){
System.out.println("==예외가 발생한 경우==");
System.out.println("1");
try{
System.out.println("2");
System.out.println(7/0);
System.out.println("3");
}catch(Exception e){
System.out.println("4");
}finally{//예외가 있던 없던 무조건 실행됨
System.out.println("5");
}
System.out.println("프로그램 종료");
}
}
==예외가 발생한 경우==
| 예외처리 throws 사용법 (0) | 2012.04.11 |
|---|---|
| 예외처리 다중 catch문 사용하기 (0) | 2012.04.11 |
| 예외처리 5 (0) | 2012.04.11 |
| 예외처리 기본4 (0) | 2012.04.11 |
| 예외처리 기본3 (0) | 2012.04.11 |
package com.basic;//예외처리 5
public class ExceptionEx5 {
public static void main(String[] args) {
int var =50;
/*다중 catch문 사용하기
catch 블럭에 전달되는 객체의 타입을 명시할때
하위 타입은 위로 상위타입은 아래로*/
try{
//String -> int
int date = Integer.parseInt(args[0]);
System.out.println(var/date);
System.out.println("----------");
}
catch(NumberFormatException e){
System.out.println("숫자가 아닙니다.");
}
catch(ArrayIndexOutOfBoundsException e){
System.out.println("입력한 데이터가 없습니다.");
}
catch(ArithmeticException e){
System.out.println("0으로 나눌 순 없죠?");
}
catch(Exception e){
System.out.println("모든 예외처리는 여기서");
}
System.out.println("프로그램 종료!");
}
}| 예외처리 다중 catch문 사용하기 (0) | 2012.04.11 |
|---|---|
| 예외처리 finally 사용법 (0) | 2012.04.11 |
| 예외처리 기본4 (0) | 2012.04.11 |
| 예외처리 기본3 (0) | 2012.04.11 |
| 예외처리 기본2 (0) | 2012.04.11 |
package com.basic;//예외 처리4
public class ExceptionEx4 {
public static void main(String[] args) {
System.out.println("==예외가 발생한 경우==");
System.out.println("1");
try{
System.out.println("2");
System.out.println(6/0); //예외 발생
System.out.println("3");
}catch(Exception e){
//예외가 발생하면 catch 블럭으로 이동
System.out.println("4");
}
System.out.println("5");
}
}==예외가 발생한 경우==
| 예외처리 finally 사용법 (0) | 2012.04.11 |
|---|---|
| 예외처리 5 (0) | 2012.04.11 |
| 예외처리 기본3 (0) | 2012.04.11 |
| 예외처리 기본2 (0) | 2012.04.11 |
| 예외처리 기본 (0) | 2012.04.11 |
package com.basic;//예외 처리3
public class ExceptionEx3 {
public static void main(String[] args) {
System.out.println("==예외가 발생하지 않고 정상수행==");
System.out.println("1");
try{
System.out.println("2");
System.out.println("3");
}catch(Exception e){
//예외가 발생하지 않으면 catch블럭은 호출되지 않음
System.out.println("4");
}
System.out.println("5");
}
}==예외가 발생하지 않고 정상수행==
| 예외처리 finally 사용법 (0) | 2012.04.11 |
|---|---|
| 예외처리 5 (0) | 2012.04.11 |
| 예외처리 기본4 (0) | 2012.04.11 |
| 예외처리 기본2 (0) | 2012.04.11 |
| 예외처리 기본 (0) | 2012.04.11 |
package com.basic;//예외처리
public class ExceptionEx2 {
public static void main(String[] args) {
int[] var = {10,20,30};
for(int i=0; i<=var.length; i++){
//예외가 발생하면 프로그램이 비정상 종료되는데
//비정상 종료되는 것을 막고 프로그래머가 원하는 수행문들이
//수행되면서 정상적으로 종료처리를 함
try{
//예외가 발생할 가능성이 있는 수행문
System.out.println(var[i]);
}catch(ArrayIndexOutOfBoundsException ae){
//예외가 발생하면 catch 블럭으로 이동해서 수행문 수행
//일반적으로 예외문구 표시
System.out.println("호출한 배열의 인덱스가 없습니다.");
}
}
System.out.println("프로그램 끝~~");
}
}10
| 예외처리 finally 사용법 (0) | 2012.04.11 |
|---|---|
| 예외처리 5 (0) | 2012.04.11 |
| 예외처리 기본4 (0) | 2012.04.11 |
| 예외처리 기본3 (0) | 2012.04.11 |
| 예외처리 기본 (0) | 2012.04.11 |
package com.basic;//예외처리
public class ExceptionEx1 {
public static void main(String[] args) {
int[] var = {10,20,30};
//예외처리를 안 한 상태에서 예외가 발생하면
//예외가 발생한 라인에서 프로그램이 정지(비정상 종료) 되고
//예외 정보를 갖는 예외 객체가 생성됨
for(int i=0; i<=var.length; i++){
System.out.println(var[i]);
}
System.out.println("프로그램 끝~~");
}
}10
| 예외처리 finally 사용법 (0) | 2012.04.11 |
|---|---|
| 예외처리 5 (0) | 2012.04.11 |
| 예외처리 기본4 (0) | 2012.04.11 |
| 예외처리 기본3 (0) | 2012.04.11 |
| 예외처리 기본2 (0) | 2012.04.11 |