【Java 입문】Handling Exceptions(예외 처리)
모쿠지
1,Handling Exceptions
2,Another example
3,Using a finally Block
4,Catching FileNotFoundException
5,Throwing Your Own Exceptions
1, Handling Exceptions
Here's a program that divides two number and uses a try/catch statement to catch an exception if the second number turns out to be zero.
Main.javapublic class Main {
public static void main(String[] args) {
int a = 5;
int b = 0;
try {
int c = a / b;
} catch (ArithmeticException e) {
System.out.println("Oops, you can't divide by zero.");
}
}
}
When you run this program, the following is displayed on the console.
콘솔Oops, you can't divide by zero.
2, Another example
This shows a simple example of a program that uses a method to get a valid integer from the user.
Main.javaimport java.util.InputMismatchException;
import java.util.Scanner;
public class Main {
static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
System.out.print("Enter an integer: ");
int i = GetAnInteger();
System.out.println("You entered " + i);
}
public static int GetAnInteger() {
while (true) {
try {
return sc.nextInt();
} catch (InputMismatchException e) {
sc.next();
System.out.print("That's not an integer. Try again: ");
}
}
}
}
Here's what the console might look like for a typical execution of this program.
콘솔Enter an integer: three
That's not an integer. Try again: 3.01
That's not an integer. Try again: 3
You entered 3
3, Using a finally Block
A finally block is a block that appears after any of the catch blocks for a statement.
Main.javapublic class Main {
public static void main(String[] args) {
int answer = 0;
try {
answer = divideTheseNumbers(5, 0);
} catch (Exception e) {
System.out.println("Tried twice, still didn't work!");
}
System.out.println("Answer is: " + answer);
}
public static int divideTheseNumbers(int a, int b) {
int c;
try {
c = a / b;
System.out.println("It worked!");
} catch (Exception e) {
System.out.println("Didn't work the first time.");
c = a / b;
System.out.println("It worked the second time!");
} finally {
System.out.println("Better clean up my mess.");
}
System.out.println("It worked after all.");
return c;
}
}
Here's the console output the program.
콘솔Didn't work the first time.
Better clean up my mess.
Tried twice, still didn't work!
Answer is: 0
4, Catching FileNotFoundException
One way to deal with the FileNotFoundException is to catch it by using an ordinary try statement.
Main.javaimport java.io.FileInputStream;
import java.io.FileNotFoundException;
public class Main {
public static void main(String[] args) {
openFile("C:\\test.txt");
}
public static void openFile(String name) {
try {
FileInputStream f = new FileInputStream(name);
System.out.println(f);
} catch (FileNotFoundException e) {
System.out.println("File not found.");
}
}
}
In this example, the message "File not found."is displayed if the file doesn't exist.
콘솔File not found.
5, Throwing Your Own Exceptions
Here's a program that demonstrates the basic structure of a method that throws an exception.
Main.javapublic class Main {
public static void main(String[] args) {
try {
doSomething(true);
} catch (Exception e) {
System.out.println("Exception!");
}
}
public static void doSomething(boolean t) throws Exception {
if (t) {
throw new Exception();
}
}
}
Here the doSomething method accepts a boolean value as a parameter. If this value is true, it throws an exception.
콘솔Exception!
참고 자료
Chapter 8 Handling Exception
Reference
이 문제에 관하여(【Java 입문】Handling Exceptions(예외 처리)), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다
https://qiita.com/iwasaki-hub/items/c3b8304477e446f2ec1e
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)
Here's a program that divides two number and uses a try/catch statement to catch an exception if the second number turns out to be zero.
Main.java
public class Main {
public static void main(String[] args) {
int a = 5;
int b = 0;
try {
int c = a / b;
} catch (ArithmeticException e) {
System.out.println("Oops, you can't divide by zero.");
}
}
}
When you run this program, the following is displayed on the console.
콘솔
Oops, you can't divide by zero.
2, Another example
This shows a simple example of a program that uses a method to get a valid integer from the user.
Main.javaimport java.util.InputMismatchException;
import java.util.Scanner;
public class Main {
static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
System.out.print("Enter an integer: ");
int i = GetAnInteger();
System.out.println("You entered " + i);
}
public static int GetAnInteger() {
while (true) {
try {
return sc.nextInt();
} catch (InputMismatchException e) {
sc.next();
System.out.print("That's not an integer. Try again: ");
}
}
}
}
Here's what the console might look like for a typical execution of this program.
콘솔Enter an integer: three
That's not an integer. Try again: 3.01
That's not an integer. Try again: 3
You entered 3
3, Using a finally Block
A finally block is a block that appears after any of the catch blocks for a statement.
Main.javapublic class Main {
public static void main(String[] args) {
int answer = 0;
try {
answer = divideTheseNumbers(5, 0);
} catch (Exception e) {
System.out.println("Tried twice, still didn't work!");
}
System.out.println("Answer is: " + answer);
}
public static int divideTheseNumbers(int a, int b) {
int c;
try {
c = a / b;
System.out.println("It worked!");
} catch (Exception e) {
System.out.println("Didn't work the first time.");
c = a / b;
System.out.println("It worked the second time!");
} finally {
System.out.println("Better clean up my mess.");
}
System.out.println("It worked after all.");
return c;
}
}
Here's the console output the program.
콘솔Didn't work the first time.
Better clean up my mess.
Tried twice, still didn't work!
Answer is: 0
4, Catching FileNotFoundException
One way to deal with the FileNotFoundException is to catch it by using an ordinary try statement.
Main.javaimport java.io.FileInputStream;
import java.io.FileNotFoundException;
public class Main {
public static void main(String[] args) {
openFile("C:\\test.txt");
}
public static void openFile(String name) {
try {
FileInputStream f = new FileInputStream(name);
System.out.println(f);
} catch (FileNotFoundException e) {
System.out.println("File not found.");
}
}
}
In this example, the message "File not found."is displayed if the file doesn't exist.
콘솔File not found.
5, Throwing Your Own Exceptions
Here's a program that demonstrates the basic structure of a method that throws an exception.
Main.javapublic class Main {
public static void main(String[] args) {
try {
doSomething(true);
} catch (Exception e) {
System.out.println("Exception!");
}
}
public static void doSomething(boolean t) throws Exception {
if (t) {
throw new Exception();
}
}
}
Here the doSomething method accepts a boolean value as a parameter. If this value is true, it throws an exception.
콘솔Exception!
참고 자료
Chapter 8 Handling Exception
Reference
이 문제에 관하여(【Java 입문】Handling Exceptions(예외 처리)), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다
https://qiita.com/iwasaki-hub/items/c3b8304477e446f2ec1e
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)
import java.util.InputMismatchException;
import java.util.Scanner;
public class Main {
static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
System.out.print("Enter an integer: ");
int i = GetAnInteger();
System.out.println("You entered " + i);
}
public static int GetAnInteger() {
while (true) {
try {
return sc.nextInt();
} catch (InputMismatchException e) {
sc.next();
System.out.print("That's not an integer. Try again: ");
}
}
}
}
Enter an integer: three
That's not an integer. Try again: 3.01
That's not an integer. Try again: 3
You entered 3
A finally block is a block that appears after any of the catch blocks for a statement.
Main.java
public class Main {
public static void main(String[] args) {
int answer = 0;
try {
answer = divideTheseNumbers(5, 0);
} catch (Exception e) {
System.out.println("Tried twice, still didn't work!");
}
System.out.println("Answer is: " + answer);
}
public static int divideTheseNumbers(int a, int b) {
int c;
try {
c = a / b;
System.out.println("It worked!");
} catch (Exception e) {
System.out.println("Didn't work the first time.");
c = a / b;
System.out.println("It worked the second time!");
} finally {
System.out.println("Better clean up my mess.");
}
System.out.println("It worked after all.");
return c;
}
}
Here's the console output the program.
콘솔
Didn't work the first time.
Better clean up my mess.
Tried twice, still didn't work!
Answer is: 0
4, Catching FileNotFoundException
One way to deal with the FileNotFoundException is to catch it by using an ordinary try statement.
Main.javaimport java.io.FileInputStream;
import java.io.FileNotFoundException;
public class Main {
public static void main(String[] args) {
openFile("C:\\test.txt");
}
public static void openFile(String name) {
try {
FileInputStream f = new FileInputStream(name);
System.out.println(f);
} catch (FileNotFoundException e) {
System.out.println("File not found.");
}
}
}
In this example, the message "File not found."is displayed if the file doesn't exist.
콘솔File not found.
5, Throwing Your Own Exceptions
Here's a program that demonstrates the basic structure of a method that throws an exception.
Main.javapublic class Main {
public static void main(String[] args) {
try {
doSomething(true);
} catch (Exception e) {
System.out.println("Exception!");
}
}
public static void doSomething(boolean t) throws Exception {
if (t) {
throw new Exception();
}
}
}
Here the doSomething method accepts a boolean value as a parameter. If this value is true, it throws an exception.
콘솔Exception!
참고 자료
Chapter 8 Handling Exception
Reference
이 문제에 관하여(【Java 입문】Handling Exceptions(예외 처리)), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다
https://qiita.com/iwasaki-hub/items/c3b8304477e446f2ec1e
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)
import java.io.FileInputStream;
import java.io.FileNotFoundException;
public class Main {
public static void main(String[] args) {
openFile("C:\\test.txt");
}
public static void openFile(String name) {
try {
FileInputStream f = new FileInputStream(name);
System.out.println(f);
} catch (FileNotFoundException e) {
System.out.println("File not found.");
}
}
}
File not found.
Here's a program that demonstrates the basic structure of a method that throws an exception.
Main.java
public class Main {
public static void main(String[] args) {
try {
doSomething(true);
} catch (Exception e) {
System.out.println("Exception!");
}
}
public static void doSomething(boolean t) throws Exception {
if (t) {
throw new Exception();
}
}
}
Here the doSomething method accepts a boolean value as a parameter. If this value is true, it throws an exception.
콘솔
Exception!
참고 자료
Chapter 8 Handling Exception
Reference
이 문제에 관하여(【Java 입문】Handling Exceptions(예외 처리)), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다
https://qiita.com/iwasaki-hub/items/c3b8304477e446f2ec1e
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)
Reference
이 문제에 관하여(【Java 입문】Handling Exceptions(예외 처리)), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/iwasaki-hub/items/c3b8304477e446f2ec1e텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)