B:空指针异常
C:数组下标越界异常
package xie.i;
publicclass Test5 {
publicstaticvoid main(String[] args) {
String []s=new String[10];
try{
s[11]="5";
}catch(Exception e){
System.out.println("数组下标越界!");
}finally{
System.out.println(3);
}
}
}
D:定义时异常
package xie.i;
publicclass Test6 {
publicstaticvoid main(String[] args) {
try{
int[]a=newint[-2];
}catch(Exception e){
System.out.println("定义时出现异常");
}
}
}
E:IO异常
try{
FileInputStream fis=new FileInputStream("f:/13.doc");
}catch(FileNotFoundException e){
System.out.println("找不到文件");
}
}
}
六:自定义异常类
package xie.i;
publicclass Test7 {
publicstaticvoid main (String[]args){
B b=new B();
try{
b.f();
}catch(TestqException e){
e.printStackTrace();
}
}
}
classTestqExceptionextends Exception {}
class B{
publicvoid f() throws TestqException{
System.out.println(123);
}
}
异常关键字用法注意点:
package xie.i;
publicclass Test3 {
publicvoid f() throws Exception{
thrownew Exception("不能为零!");
//throw一定抛出异常,在抛出异常后,后面的语句就不被执行;
}
publicstaticvoid main(String[] args){
Test3 t=new Test3();
try {
t.f();
} catch (Exception e) {//一个try里面可以有多个catch语句,一个try里面可以再有try
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
练习(1):请输入一个数,范围在1到4,如果不满足,就请重新输入(用正则表达式方法编写)
方法(一)
package xie.i;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
publicclass Test10 {
publicstaticvoid main(String[] args) {
Scanner s=new Scanner(System.in);
System.out.println("请输入数字:");
String a=s.nextLine();
Pattern p=Pattern.compile("[0-4]");
Matcher m=p.matcher(a);
boolean b=m.matches();//试着尝试整个去区匹配
while(!b){
System.out.println("请重新输入:");
a=s.nextLine();
m=p.matcher(a);
b=m.matches();
}
}
}
方法(二):
package xie.i;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
publicclass Test10 {
publicstaticvoid main(String[] args) {
Scanner s=new Scanner(System.in);
while(true){
System.out.println("请输入数字:");
String a=s.nextLine();
Pattern p=Pattern.compile("[0-4]");
Matcher m=p.matcher(a);
if(m.find()){
break;
}else{
System.out.println("请重新输入:");
}
}
}
}
练习(2):
已知字符串:”123,,,.....**//789aaa”;
(1)请用正则表达式输出字符串中的符号;
(2)请用正则表达式输出字符串的数字和字母;
第一小题:
package xie.i;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
publicclass Test11 {
publicstaticvoid main(String[] args) {
String s="123,,,....**//789aaa";
Pattern p=Pattern.compile("\\p{Punct}");
Matcher m=p.matcher(s);
while(m.find()){
System.out.print(m.group()+" ");
}
}
}
第二小题:
package xie.i;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
publicclass Test11 {
publicstaticvoid main(String[] args) {
String s="123,,,....**//789aaa";
Pattern p=Pattern.compile("\\p{Alnum}");
Matcher m=p.matcher(s);
while(m.find()){
System.out.print(m.group()+" ");
}
}
}
练习(三):
输出大前天的时间;精确定几分几秒星期几?
package xie.i;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
publicclass QianT {
publicstaticvoid main(String[] args) {
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss E");
System.out.println(sdf.format(new Date()));
Calendar c=Calendar.getInstance();
c.add(c.DAY_OF_MONTH,-3);
System.out.println(c.get(c.YEAR)+"年"+(c.get(c.MONTH)+1)+"月"+c.get(c.DAY_OF_MONTH)+"日");
System.out.println(sdf.format(c.getTime()));
}
}