자바>이상>>throw,성명 throws,포획 try...catch,finally 코드 블록 던 지기
7787 단어 자바 언어 고급
1.이상 throw 던 지기:
실례:
클래스 정의:
package YiChang;
/*
throw
:
throw 。
:
throw new xxxException(" ");
:
1. throw
2. throw new Exception Exception
3. throw ,
throw RuntimeException RuntimeException , , JVM ( , )
throw ( ), , throws, try...catch
*/
public class Demo01Throw {
public static void main(String[] args) {
//int[] arr = null
int[] arr = new int[3];
int e = getElement(arr, 3);
System.out.println(e);
}
/*
,
:
int[] arr
int index
( )
, , ,
:
NullPointerException , , JVM
ArrayIndexOutOfBoundsException , , JVM
*/
private static int getElement(int[] arr,int index) {
/*
,
arr null
, “ null”
*/
if(arr == null){
throw new NullPointerException(" null");
}
/*
index
index
, “ ”
*/
if(index < 0 || index > arr.length-1){
throw new ArrayIndexOutOfBoundsException(" ");
}
int ele = arr[index];
return ele;
}
}
실행 결과:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException:
at YiChang.Demo01Throw.getElement(Demo01Throw.java:51)
at YiChang.Demo01Throw.main(Demo01Throw.java:20)
2.이상 throws 선언:
실례:
클래스 정의:
package YiChang;
import java.io.FileNotFoundException;
import java.io.IOException;
/*
throws : ,
:
,
throws , ( , ), JVM -->
:
( ) throws AAAException,BBBException...{
throw new AAAException(" ");
throw new BBBException(" ");
...
}
:
1. throws
2. throws Exception Exception
3. , throws
,
4. ,
throws, , JVM
try...catch
*/
public class Demo02Throws {
/*
FileNotFoundException extends IOException
,
*/
//public static void main(String[] args) throws FileNotFoundException,IOException {
//public static void main(String[] args) throws IOException {
public static void main(String[] args) throws Exception {
//readFile("c:\\a.txt");
//readFile("d:\\a.txt");
readFile("c:\\a.tx");
}
/*
,
"c:\\a.txt", ,
:
FileNotFoundException , ,
throws FileNotFoundException ,
*/
public static void readFile(String fileName) throws FileNotFoundException,IOException {
if(!fileName.equals("c:\\a.txt")){
throw new FileNotFoundException(" c:\\a.txt");
}
/*
, .txt
IO , ,
*/
if(fileName.endsWith(".txt")){
System.out.println(" ");
}
System.out.println(" , ");
}
}
실행 결과:
Exception in thread "main" java.io.FileNotFoundException: c:\a.txt
at YiChang.Demo02Throws.readFile(Demo02Throws.java:48)
at YiChang.Demo02Throws.main(Demo02Throws.java:36)
3.캡 처 이상 try...catch:
실례:
클래스 정의:
package YiChang;
import java.io.IOException;
/*
try...catch: ,
:
try{
}catch( , try ){
, ,
,
}
...
catch( ){
}
:
1. try , catch
2. try , catch , catch , try...catch
try , catch , try , try...catch
*/
public class Demo03TryCatch {
public static void main(String[] args) throws Exception{
try{
//
readFile("d:\\a.tx");
}catch(IOException e){ // try ,catch ,
// , ,
System.out.println("catch - .txt");
/*
Throwable 3
String getMessage(): throwable 。
String toString(): throwable 。
void printStackTrace():JVM , ,
*/
//System.out.println(e.getMessage()); //
//System.out.println(e.toString()); //java.io.IOException:
/*
java.io.IOException:
at YiChang.Demo03TryCatch.readFile(Demo03TryCatch.java:53)
at YiChang.Demo03TryCatch.main(Demo03TryCatch.java:27)
*/
//e.printStackTrace();
}
System.out.println(" ");
}
/*
, .txt
IO , ,
*/
public static void readFile(String fileName) throws IOException {
if(!fileName.endsWith(".txt")){
throw new IOException(" ");
}
System.out.println(" , ");
}
}
실행 결과:
catch - .txt
4.finally 코드 블록:
실례:
클래스 정의:
package YiChang;
import java.io.IOException;
/*
finally
:
try{
}catch( , try ){
, ,
,
}
...
catch( ){
}finally{
}
:
1. finally , try
2. finally ( ), , (IO)
*/
public class Demo04Finally {
public static void main(String[] args) {
try {
//
readFile("c:\\a.tx");
} catch (IOException e) {
//
e.printStackTrace();
}finally {
// ,
System.out.println(" ");
}
}
/*
, .txt
IO , ,
*/
public static void readFile(String fileName) throws IOException {
if(!fileName.endsWith(".txt")){
throw new IOException(" ");
}
System.out.println(" , ");
}
}
실행 결과:
java.io.IOException:
at YiChang.Demo04Finally.readFile(Demo04Finally.java:45)
at YiChang.Demo04Finally.main(Demo04Finally.java:29)