이상 체계
1. 이상 한 계승 구조: 기본 클래스 는 Throwable, Error 와 Exception 은 Throwable, Runtime Exception 과 IOException 등 계승 Exception 이 고 구체 적 인 Runtime Exception 은 Runtime Exception 을 계승 합 니 다.2. Error 와 Runtime Exception 과 그 하위 클래스 는 검사 되 지 않 은 이상 (unchecked) 이 되 고 다른 이상 은 검사 되 지 않 은 이상 (checked) 이 됩 니 다.2. 각 유형의 이상 한 특징 1. Error 시스템 Error 류 체 계 는 자바 운영 체제 의 내부 오류 와 자원 이 소 진 된 상황 을 묘사 했다.응용 프로그램 은 이러한 유형의 대상 을 던 져 서 는 안 된다.만약 이런 오류 가 발생 한다 면, 프로그램 을 안전하게 퇴출 시 키 려 고 애 쓰 는 것 외 에는 다른 방면 에 서 는 무력 하 다.따라서 프로 그래 밍 을 할 때 는 Exception 시스템 에 더 관심 을 가 져 야 한다.2. Exception 시스템 Exception 시스템 은 Runtime Exception 시스템 과 다른 비 Runtime Exception 시스템 을 포함한다. 3. Runtime Exception Runtime Exception 시스템 은 잘못된 유형 전환, 배열 의 크로스 오 버 액세스 와 빈 포인터 에 접근 하려 는 시도 등 을 포함한다.Runtime Exception 을 처리 하 는 원칙 은 Runtime Exception 이 발생 하면 프로그래머 의 오류 가 틀림 없다 는 것 이다.예 를 들 어 배열 의 아래 표지 와 배열 경 계 를 검사 함으로써 배열 의 크로스 오 버 접근 이상 을 피 할 수 있다. 4. 기타 (IOException 등) 와 같은 이상 은 일반적으로 외부 오류 입 니 다. 예 를 들 어 파일 끝 에서 데 이 터 를 읽 으 려 는 등 프로그램 자체 의 오류 가 아니 라 응용 환경 에서 발생 하 는 외부 오류 입 니 다.
3. 이상 한 처리 와 던 지기
키워드
throws 이미 존재 하 는 이상 대상 을 던 져 다음 호출 자 에 게 맡 기 겠 다 는 뜻 이다. throw 새 이상 대상 을 만 들 고 다음 호출 자 에 게 맡 기 는 것 을 표시 합 니 다.(사용자 정의 이상 사용) try {} 중간 에 나타 날 수 있 는 이상 캡 처 catch try 캡 처 이상 처리 finally 는 try 에서 오류 가 발생 했 거나 catch 를 실 행 했 든 안 했 든 finally 의 문 구 를 실행 할 것 이 라 고 밝 혔 다.
import java.io.File;
import java.io.IOException;
public class Test2 {
public static void main(String [] args){
Test2 t2 = new Test2();
try{
t2.createNewFile("m.txt");
}catch(Exception e){
e.printStackTrace();
}
}
private void createNewFile(String name) {
File file = new File(name);
try{
file.createNewFile();
System.out.println(" !");
}
catch(IOException e){
//
System.out.println("toString:"+e.toString());
//
System.out.println("getMessage:"+e.getMessage());
// ,
e.printStackTrace();
}finally{
System.out.println(" finally ");
}
}
}
thows
import java.io.File;
import java.io.IOException;
public class Test2 {
public static void main(String [] args){
Test2 t2 = new Test2();
try{
t2.createNewFile("m.txt");
}catch(Exception e){
e.printStackTrace();
}
}
private void createNewFile(String name) throws IOException {
File file = new File(name);
file.createNewFile();
System.out.println(" !");
}
}
thow 강제 이상 던 지기
import java.io.File;
import java.io.IOException;
public class Test2 {
public static void main(String [] args) throws ThreadException{
Test2 t2 = new Test2();
try{
t2.createNewFile("m.txt");
}catch(Exception e){
e.printStackTrace();
throw new ThreadException(" ");
}
}
private void createNewFile(String name) throws IOException {
File file = new File(name);
file.createNewFile();
System.out.println(" !");
}
}
try { } catch (IOException e) { }catch(NullPointerException e){ 빈 포인터 이상 }catch(ArithmeticException e){ 이상 한 연산 조건 이 생 겼 을 때 이 이상 을 던 집 니 다. 예 를 들 어 하나의 정수 가 0 으로 나 눌 때. }finally{ }
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Vagrant 를 사용 하여 Windows 에 개발 환경 을 배치 합 니 다.개발 자 에 게 Vagrant 는 팀 원 들 의 개발 환경 을 통일 시 켜 줄 수 있다.만약 당신 이나 당신 의 동료 가 Vagrantfile 을 만 들 었 다 면, vagrant 를 실행 하기 만 하면 됩 니 다....
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.