try catch finally
try必须搭配catch或finally使用
情况1
try{
//try中可能出现异常的代码块
}catch(Exception e){//Exception异常 e是变量
//catch对try中异常情况进行处理
}
示例代码
public class Test01 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
/**
* try可能会产生异常的代码块
* try不能单独使用 单独使用会报错 必须配合catch或finally使用
*/
try{
System.out.println("请输入被除数:");
int num1=sc.nextInt();
System.out.println("请输入除数:");
int num2=sc.nextInt();
System.out.println(String.format("%d/%d=%d",num1,num2,num1/num2));
}catch (Exception e){
/**
* 把错误信息全部抓出来
* catch只有报错的时候才进来
*/
System.out.println("您输入的信息有误,请输入正确数字");
e.printStackTrace();
}
}
}
运行截图:
情况2
try{
}finally{
//finally中代码 不管出不出错都会执行
}
示例代码
如果我们没有写catch 系统会自动帮我们把异常信息输出到控制台
public class Test02 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
/**
* try可能会产生异常的代码块
*try不能单独使用,必须配合catch 或 finally使用
*/
try{
System.out.println("请输入被除数:");
int num1=sc.nextInt();
System.out.println("请输入除数:");
int num2=sc.nextInt();
System.out.println(String.format("%d/%d=%d",num1,num2,num1/num2));
}finally {
//finally代码块 不管有没有错 都会执行的代码
System.out.println("感谢使用本程序!");
}
//如果我们没有写catch 系统会自动帮我们把异常信息输出到控制台
}
}
运行截图:
情况3
try{
}catch(Exception e){
}finally{
}
示例代码
public class Test03 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
try{
System.out.println("请输入被除数:");
int num1=sc.nextInt();
System.out.println("请输入除数:");
int num2=sc.nextInt();
System.out.println(String.format("%d/%d=%d",num1,num2,num1/num2));
}catch (Exception e){
System.out.println("输入数据有误,请输入正确数据");
e.printStackTrace();
}finally {
System.out.println("感谢使用本程序!");
}
}
}
运行截图:
常见异常
注:Exception是所有异常的父类,即不管什么异常都可接收
多重异常处理
public class Test04 {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
try{
System.out.println("请输入被除数:");
int num1=in.nextInt();
System.out.println("请输入除数:");
int num2=in.nextInt();
System.out.println(String.format("%d/%d=%d",num1,num2,num1/num2));
/**
* 如果想处理多种异常 使用多重异常处理
*/
}catch(InputMismatchException e){//我只处理接收异常
System.out.println("请输入正确的数字:");
e.printStackTrace();
}catch(ArithmeticException e){//只处理算数异常
System.out.println("除数不能为0");
e.printStackTrace();
}catch (Exception e){//Exception是所有异常的父类 儿子没抓到的异常 只能放在最下面的位置
System.out.println("你的程序有异常了");
e.printStackTrace();
}
System.out.println("感谢使用本程序!");
}
}
运行截图:
类转换异常ClassCastException
Pet pet=new Penguin();
Dog dog=(Dog)pet;
空指针异常NullPointerException
String str=null;
System.out.println(str.toString());
数组下标越界ArrayIndexException
int num[]={1,2,3,4};
System.out.println(num[4]);
数字格式异常NumberFormatException
将字符串转为int型数字
String str2="123abc";
Integer num1=Integer.parseInt(str2);
finally
无论何种情况,finally代码块都执行
public class Test06 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
try{
System.out.println("请输入被除数");
int num1=sc.nextInt();
System.out.println("请输入除数");
int num2=sc.nextInt();
System.out.println(String.format("%d/%d=%d",num1,num2,num1/num2));
return;
}catch (Exception e){
e.printStackTrace();
System.out.println("程序出现了错误");
return;
}finally {
System.out.println("感谢使用本程序");
}
}
}
运行截图:
正确情况
出现错误情况
finally代码块唯一不执行的情况
System.exit(0);
运行截图:
面试问答
面试题1
答:try…catch中存在return语句,执行finally程序块。
执行顺序:
出现异常情况:try程序块出现异常语句下面的语句不再执行,开始执行catch代码块,接着执行finally代码块,最后执行catch中的return
未出现异常的情况:先执行try程序块中的语句,再执行finally程序块中的语句,最后执行try程序块中的return
面试题2
finally不执行的情况:System.exit(0);
throws
功能:
声明某个方法可能产生的异常
与throw区别:throws声明异常,throw抛出异常
public void getA() throws Exception{
//该方法可能会产生异常
}
示例代码:
public class Test07 {
public static void main(String[] args) {
//调用下面的方法
try{
getA();
}catch (Exception e){
e.printStackTrace();
System.out.println("程序有异常!");
}
}
//声明异常 告知调用者 我这个方法可能有异常 你必须处理了
private static void getA() throws Exception{
Scanner sc=new Scanner(System.in);
System.out.println("请输入被除数");
int num1=sc.nextInt();
System.out.println("请输入除数");
int num2=sc.nextInt();
System.out.println(String.format("%d/%d=%d",num1,num2,num1/num2));
}
}
运行截图:
调用方法 但不想处理的情况
运行截图:
thows和try…catch的区别:
//throws应用举例
public class Test01{
public static void main(String[] args) throws Exception{
getA();
System.out.println("感谢使用本程序!");
}
public void getA() throws Exception{
Scanner sc=new Scanner(System.in);
System.out.println("请输入被除数:");
int num1=sc.nextInt();
System.out.println("请输入除数:");
int num2=sc.nextInt();
System.out.println(String.format("%d/%d=%d",num1,num2,num1/num2));
}
}
//try...catch...应用举例
public class Test07 {
public static void main(String[] args) throws Exception{
try{
getA();
}catch (Exception e){
e.printStackTrace();
System.out.println("程序有异常");
}
//想调用方法,但不想处理 在main方法抛出异常
//这种情况 如果不处理 java虚拟机会处理
//getA();
System.out.println("感谢使用本程序");
}
//声明异常 告知调用者 我这个方法可能有异常 你必须处理了
private static void getA() throws Exception{
Scanner sc=new Scanner(System.in);
System.out.println("请输入被除数");
int num1=sc.nextInt();
System.out.println("请输入除数");
int num2=sc.nextInt();
System.out.println(String.format("%d/%d=%d",num1,num2,num1/num2));
}
}
FileInputStream()方法可能会产生的错误
处理方式1
Add Exception to method signature
处理方式2
More actions->surround with try/catch
throw
功能:
将异常信息输出
throw new Exception("你的程序有误!");
示例代码
User类
Test09类
方式1:main抛出异常
public class Test09 {
public static void main(String[] args) throws Exception {
//创建对象
User user=new User();
user.setAge(101);
System.out.println("感谢使用本程序");
}
}
方式2:try/catch
public class Test09 {
public static void main(String[] args){
//创建对象
User user=new User();
try {
user.setAge(101);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("感谢使用本程序");
}
}
log4j(一个非常优秀的开源日志记录工具)
记录步骤
1.在项目中加入log4j的JAR文件
选中工程—>右键—>new—>Directory
在New directory中写上lib
将JAR包粘贴在lib目录下—>点击ok
右键添加好的JAR包—>Add as library
2.创建log4j.properties文件
将log4j复制到src文件夹下
3.配置日志信息(日志信息输出的目录)
4.使用log4j记录日志信息