????throw??throws????
????1??throws??????????????????throw??????????塣
????2??throws?????????????????????????????????Щ????throw????????????????throw???????????????????
????3???????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????
?????????????
????1.??д?????????????????????????????try{...}catch{...}???????????????д????
????2.??try{...}catch{...}??????????????????catch{...}?ж?????д?????????????????????????????e.printStackTrace();
????3.???????IO??????????е???????????try{...}catch{...}???finally{...}???????????????
????4.??????????????throw???????????????????????м?throws???????????????????????????????д????
?????????
????????????
????try{
????……
????}catch(Exception e){
????……
????}finally{
????……
????}
????try{……}?з?????????????????飬??????????????????????????????????catch(){……}??????????(Exception e)??Exception?????????????????Exception(Exception??????????????)??????{}???嵱?????????????????finally{……}???????????????????y???finally{}?е?????
????????????try{...}?????У????????????????????(???????????)?????????????У?????????catch{...}??????????????????磺
public static void function1() throws NumberFormatException{
System.out.println(Double.parseDouble("abc"));
System.out.println("????????");
}
public static void main(String[] args) {
try {
function1();
} catch (Exception e) {
System.err.println(e.getMessage());
//e.printStackTrace();
}
}
??????????£???????????????????
????For input string: "abc"
????System.out.println("????????");δ??С?
???????????????????throws???????????????ú????????????????????????
public static void function() {
String s = "abc";
System.out.println(Double.parseDouble(s));
}
public static void main(String[] args) {
try {
function();
} catch (Exception e) {
System.err.println("??????????????????");
//e.printStackTrace();
}
}
?????????????£?
??????????????????????
????????????????????γ???鯔??????????????????????????try{...}catch{...}????????
???????????
????????????????????????????????????Exception???Exception??????????????throw???????????????????磺
public static void function() throws ParenthesisMatchingException{
String s = "((a+b)";
ParenthesisMatchingException e = new ParenthesisMatchingException("???????????");
if(s.charAt(0)=='(' && s.charAt(1)=='(') {
throw e;
}
System.out.println(s);
}
public static void main(String[] args) {
try {
function();
} catch (Exception e) {
System.out.println(e.getMessage());
//e.printStackTrace();
}
}
??????????? ??
???????????????