赞
踩
try {
//可疑代码
//将异常生成对应的异常对象,传递给catch块
}catch(异常){
//对异常的处理
}
//如果没有finally,语法是可以通过
public static void main(String[] args) {
int num1 = 10;
int num2 = 0;
try {
int res = num1 / num2;
} catch (Exception e) {
System.out.println("出现异常的原因=" + e.getMessage());//输出异常信息
}
}
快速调出try-catch :选中代码 Ctrl+Alt+t
catch
块.public static void main(String[] args) {
try {
String str = "兮动人";
int a = Integer.parseInt(str);
System.out.println("数字" + a);
} catch (NumberFormatException e) {
System.out.println("异常信息:" + e.getMessage());
}
System.out.println("程序继续执行...");
}
try
的代码块,不会进入到catch
.public static void main(String[] args) {
try {
String str = "123";
int a = Integer.parseInt(str);
System.out.println("数字:" + a);
} catch (NumberFormatException e) {
System.out.println("异常信息:" + e.getMessage());
}
System.out.println("程序继续执行...");
}
finally { }
try{
//可疑代码
}catch(异常){
//...
}finally{
//释放资源
}
public static void main(String[] args) {
try {
String str = "123";
int a = Integer.parseInt(str);
System.out.println("数字:" + a);
} catch (NumberFormatException e) {
System.out.println("异常信息:" + e.getMessage());
} finally {
System.out.println("finally代码被执行...");
}
System.out.println("程序继续执行...");
}
public static void main(String[] args) {
try {
String str = "兮动人";
int a = Integer.parseInt(str);
System.out.println("数字:" + a);
} catch (NumberFormatException e) {
System.out.println("异常信息:" + e.getMessage());
} finally {
System.out.println("finally代码被执行...");
}
System.out.println("程序继续执行...");
}
catch
语句,捕获不同的异常(进行不同的业务处理),要求父类异常在后,子类异常在前,比如(Exception
在后,NullPointerException
在前),如果发生异常,只会匹配一个catch
,案例演示:NullPointerException
和ArithmeticException
异常进行分开捕获public class TryCatchDetail02 { public static void main(String[] args) { //1.如果代码块可能有多个异常 //2.可以使用多个catch分别捕获,相应处理 //3.要求之类异常写在前面,父类异常写在后面 try { Person person = new Person(); person = null; System.out.println(person.getName()); //NullPointerException int n1 = 10; int n2 = 0; int res = n1 / n2;// } catch (NullPointerException e) { System.out.println("空指针异常:" + e.getMessage()); } catch (ArithmeticException e) { System.out.println("算术异常:" + e.getMessage()); } catch (Exception e) { System.out.println(e.getMessage()); } finally { } } } class Person { private String name = "xdr"; public String getName() { return name; } }
person = null;
时try-finally
配合使用,这种用法相当于没有捕获异常,因此程序会直接崩掉/退出。应用场景:就是执行一段代码,不管是否发生异常,都必须执行某个业务逻辑。try{
//代码
}finally{
//总是执行
}
throws
来捕获异常public class TryCatchDetail03 {
public static void main(String[] args) {
try {
int n1 = 10;
int n2 = 0;
System.out.println(n1 / n2);
}finally {
System.out.println("执行finally...");
}
System.out.println("程序继续执行...");
}
}
分析:数组并没有给赋值,所以都为 null ,这时候就报 空指针异常,被 catch 捕获(此时返回3),但 finally 最后还是要执行的,所以 返回 4
public class TryCatchExercise01 { public static int method() { try { String[] names = new String[3];//String[]数组 if (names[1].equals("tom")) {//NullPointerException System.out.println(names[1]); } else { names[3] = "xdr"; } return 1; } catch (ArrayIndexOutOfBoundsException e) { return 2; } catch (NullPointerException e) {//捕获 return 3; } finally { //必须执行 return 4; //返回4 } } public static void main(String[] args) { System.out.println(method()); //4 } }
public class TryCatchExercise02 {public static int method() { int i = 1; try { i++; //i = 2 String[] names = new String[3]; if (names[1].equals("tom")) {//空指针 System.out.println(names[1]); } else { names[3] = "xdr"; } return 1; } catch (ArrayIndexOutOfBoundsException e) { return 2; } catch (NullPointerException e) { return ++i; //i = 3 } finally {//必须执行 return ++i; //i = 4 } } public static void main(String[] args) { System.out.println(method()); } }
public class TryCatchExercise03 { public static int method() { int i = 1;//i = 1 try { i++;// i=2 String[] names = new String[3]; if (names[1].equals("tom")) { //空指针 System.out.println(names[1]); } else { names[3] = "xdr"; } return 1; } catch (ArrayIndexOutOfBoundsException e) { return 2; } catch (NullPointerException e) { return ++i; // i = 3 => 保存临时变量 temp = 3; } finally { ++i; //i = 4 System.out.println("i=" + i);// i = 4 } } public static void main(String[] args) { System.out.println(method());// 3 } }
try
块中所有语句,不执行catch
块中语句,如果有finally,最后还需要执行finally
里面的语句try
块中异常发生后,try
块剩下的语句不再执行。将执行catch
块中的语句,如果有finally
,最后还需要执行finally
里面的语句如果用户输入的不是一个整数,就提示他反复输入,直到输入一个整数为止
思路
1. 创建Scanner对象
2. 使用无限循环,去接收一个输入
3. 然后将该输入的值,转成一个int
4. 如果在转换时,抛出异常,说明输入的内容不是一个可以转成int的内容
5. 如果没有抛出异常,则break 该循环
public class TryCatchExercise04 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int num = 0; String inputStr = ""; while (true) { System.out.println("请输入一个整数:"); inputStr = scanner.next(); try { num = Integer.parseInt(inputStr); //这里可能抛出异常 break; } catch (NumberFormatException e) { System.out.println("你输入的不是一个整数:"); } } System.out.println("你输入的值是=" + num); } }
输入一个整数:
输入一个非整数就会一直提示,直到你输入一个整数才会结束循环
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。