public class Exception {
public static void main(String[] args) {
System.out.println("开始");
method();
System.out.println("结束");
}
public static void method(){
try{int[] arr={1,2,3};
System.out.println(arr[3]);
// System.out.println(arr[3]);
//new ArrayIndexOutOfBoundsException();
}catch (ArrayIndexOutOfBoundsException e){
// System.out.println("你访问的数组索引不存在");
e.printStackTrace();//使用这个还是显示异常,不过程序还会往下执行
}
}
}
/*
开始
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
at Exception.method(Exception.java:10)
at Exception.main(Exception.java:5)
*/
package heima;
public class abc {
public static void main(String[] args) {
System.out.println("第一段代码");
int i=10;
String str=null;
int[] arr=new int[3];
try{
System.out.println(i/0);
System.out.println(str.length());
arr[10]=100;
}catch (ArithmeticException e){
System.out.println("除数为0");
}catch (NullPointerException e){
System.out.println("空指针异常");
}catch (ArrayIndexOutOfBoundsException e){
System.out.println("数组下标越界异常");
}
System.out.println("第二段代码");
}
}
package heima;
public class abc {
public static void main(String[] args) {
System.out.println("第一段代码");
int i=10;
String str=null;
int[] arr=new int[3];
try{
System.out.println(i/0);
System.out.println(str.length());
arr[10]=100;
}
catch (ArithmeticException e){
e.printStackTrace();
System.out.println("除数为0");
}
catch (NullPointerException e){
System.out.println("空指针异常");
}catch (ArrayIndexOutOfBoundsException e){
System.out.println("数组下标越界异常");
}
catch (Exception e){
e.printStackTrace();
}
System.out.println("第二段代码");
}
}
public class Exception {
public static void main(String[] args) {
System.out.println("开始");
method();
System.out.println("结束");
}
public static void method(){
try{int[] arr={1,2,3};
System.out.println(arr[3]);
// System.out.println(arr[3]);
//new ArrayIndexOutOfBoundsException();
}catch (ArrayIndexOutOfBoundsException e){
// System.out.println("你访问的数组索引不存在");
//使用这个还是显示异常,不过程序还会往下执行
System.out.println(e.toString());
e.printStackTrace();
}
}
}
/*
开始
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
at Exception.method(Exception.java:10)
at Exception.main(Exception.java:5)
*/
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Exception {
public static void main(String[] args) {
//method();
method2();
}
public static void method2(){
try{
String s="2048-08-09";
SimpleDateFormat sdf=new SimpleDateFormat();
Date d=sdf.parse(s);
System.out.println(d);
}catch (ParseException e){
e.printStackTrace();
}
}
public static void method() {
int[] arr={1,2,3};
System.out.println(arr[3]);
}
}
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Exception {
public static void main(String[] args) {
System.out.println("开始");
method();
System.out.println("结束");
}
public static void method()throws ArrayIndexOutOfBoundsException{
int[] arr={1,2,3};
System.out.println(arr[3]);
}
}
package heima;
import java.text.ParseException;
import java.text.SimpleDateFormat;
public class abc {
public static void main(String[] args) {
try{
calc();
}catch (Exception e){
e.printStackTrace();
}
System.out.println("*********");
}
public static int calc()throws ArithmeticException,NullPointerException,Exception{
int a=10;
int b=0;
int result=a/b;
return result;
}
}
自定义异常
package heima;
public class SoreException extends Exception{
public SoreException(){}
public SoreException(String message){
super(message);
}
}
package heima;
public class Teacher {
public void checkScore(int score) throws SoreException{
if(score<0||score>100){
throw new SoreException("你给的分数有误");
}else{
System.out.println("分数正常");
}
}
}
package heima;
import java.util.Scanner;
public class TeacherTest {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("请输入分数");
int score= sc.nextInt();
Teacher t=new Teacher();
try {
t.checkScore(score);
} catch (SoreException e) {
e.printStackTrace();
}
}
}
finally
package heima;
import java.text.ParseException;
import java.text.SimpleDateFormat;
public class abc {
public static void main(String[] args) {
int a=10;
int b=0;
if(a==10)return;
//只要程序执行了try块中的代码,finally就都会执行 前提是没有执行System.exit();
try{
System.out.println(a/b);
}catch (Exception e){
System.out.println("除数不能为0");
}finally {
System.out.println("finally......");
}
System.out.println("**********");
}
}