puvlic void method2() throws Exception{ any code }
其中的throws即用于声明异常,声明异常来表明方法可能会抛出的异常。如果方法可能会抛出多个异常,就可以在关键字throws后添加一个用逗号分隔的异常列表public void myMethod() throws Exception1,Exception2,Exception3,....,ExceptionN
throw new Exception()
其中throw即用于抛出异常,抛出异常有两种表达形式: IllegalArgumentException ex =
new IllegalArgumentException("Wrong Argument");
throw ex;
throw new IllegalArgumentException("Wrong Argument");
catch (Exception1|Exception2|Exception3|...|Exceptionk ex){
some code for handling these Exception
}
try{
statements;
}
catch (TheException ex){
perform operations before exists;
throw ex;
}
File file = new File("Score.txt");
if(file.exists()){
System.out.println("File is already exists");
System.exit(1);
}
PrintWriter output = new PrintWriter(file);
output.print("John T Smith");
output.println(90);
output.print("Eric K Jones ");
output.println(85);
output.close();
import java.util.Scanner;
Scanner input = new Scanner(System.in);
int x = input.nextInt();
import java.io.PrintWriter;
import java.io.File;
File file = new File("scores.txt");
PrintWriter output = new PrintWriter(filename);
output.print("xxx");
output.close();
//运用close()
PrintWriter output = new PrintWriter(file);
output.print("John T Smith");
output.close();
运用try(声明和创建资源){
使用资源处理文件
}
try(PrintWriter output = new PrintWriter(file);){
output.print("lalla");
}
//从键盘读取
Scanner input = new Scanner(System.in);
//从文件读取
Scanner input = new Scanner(new File(filename));
//从网页上读取
Scanner input = new Scanner(url.openStream());
Scanner input = new Scanner(System.in);
int intValue = input.nextInt();
String line = input.nextLine();
//ReplaceText问了方宇聪海
import java.io.*;
import java.util.*;
public class ReplaceText {
public static void main(String[] args) throws Exception{
// TODO Auto-generated method stub
if(args.length != 4){
System.out.println("Usage: java ReplaceText sourceFile targetFile oldStr newStr");
System.exit(1);
}
File sourceFile = new File(args[0]);
if(!sourceFile.exists()){
System.out.println("Sourcefile " + args[0] + "already exists");
System.exit(2);
}
File targetFile = new File(args[1]);
if(targetFile.exists()){
System.out.println("targetfile " + args[1] + "already exists");
System.exit(3);
}
try(
Scanner input = new Scanner(sourceFile);
PrintWriter output = new PrintWriter(targetFile);
)
{
while(input.hasNext()){
String s1 = input.nextLine();
String s2 = s1.replaceAll(args[2], args[3]);
output.println(s2);
}
}
}
}
//未使用instanceof
catch (FileNotFoundException e) {
System.out.println("d:/LOL.exe不存在");
e.printStackTrace();
} catch (ParseException e) {
System.out.println("日期格式解析错误");
e.printStackTrace();
//使用instanceof
catch (FileNotFoundException | ParseException e) {
if (e instanceof FileNotFoundException)
System.out.println("d:/LOL.exe不存在");
if (e instanceof ParseException)
System.out.println("日期格式解析错误");
e.printStackTrace();
- throws 出现在方法声明上,而throw通常都出现在方法体内。
- throws 表示出现异常的一种可能性,并不一定会发生这些异常;throw则是抛出了异常,执行throw则一定抛出了某个异常对象。
public class Hero {
public String name;
protected int hp;
public void attackHero(Hero h)throws EnemyHeroIsDeadException{
if(h.hp == 0){
throw new EnemyHeroIsDeadException (h.name + "已经挂了,不需要再释放技能了");
}
}
public String toString(){
return name;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Hero garen = new Hero();
garen.hp = 616;
garen.name = "盖伦";
Hero teemo = new Hero();
teemo.hp = 0;
teemo.name = "提莫";
try{
garen.attackHero(teemo);
}
catch(EnemyHeroIsDeadException e){
System.out.println("异常的具体原因: "+ e.getMessage());
e.printStackTrace();
}
}
class EnemyHeroIsDeadException extends Exception{
public EnemyHeroIsDeadException(){
super();
}
public EnemyHeroIsDeadException(String msg){
super(msg);
}
}
}