{
public static void main(String args[])
{
int i = 0;
int j = 10;
System.out.println("程序代码执行之前~~~");
System.out.println(i/j);
System.out.println("程序代码执行之后~~~");
}
}
{
public static void main(String args[])
{
int i = 0;
int j = 10;
System.out.println("程序代码执行之前~~~");
System.out.println(j/i);
System.out.println("程序代码执行之后~~~");
}
}
面试问题:RuntimeException与 Exception的区别:
・ RuntimeException表示异常可以由JVM进行处理
・ Exception:表示用户可以自行处理
・ 实际上两个异常并没有太多的区别,用户都是可以处理的。
・ Error:不能由用户自行处理的异常。
{
public static void main(String args[])
{
int i = 0;
int j = 10;
System.out.println("程序代码执行之前~~~");
try
{
//下面跟上可能发生错误的代码段
System.out.println(j/i);
System.out.println("-------------------");
}
catch (ArithmeticException ae)
{
//进行异常处理
System.out.println("运算发生了异常~~~");
}
System.out.println("程序代码执行之后~~~");
}
}
注意:一个新的操作:将字符串的内容变为数字。 public class Demo03
{
public static void main(String args[])
{
String str = "123";
//将字符串变为数字
int i = Integer.parseInt(str);
System.out.println(i*2);
}
}
{
public static void main(String args[])
{
String str = "123a";
//将字符串变为数字
int i = Integer.parseInt(str);
System.out.println(i*2);
}
}
要求可以在JAVA运行程序的后面加入运行时参数,参数为两个数相除的内容。 public class Demo04
{
public static void main(String args[])
{
//从运行时处接收参数
int i = Integer.parseInt(args[0]);
int j = Integer.parseInt(args[1]);
System.out.println("程序代码执行之前~~~");
try
{
//下面跟上可能发生错误的代码段
System.out.println(i/j);
System.out.println("-------------------");
}
catch (ArithmeticException ae)
{
//进行异常处理
System.out.println("运算发生了异常~~~");
}
System.out.println("程序代码执行之后~~~");
}
}
・ 如果没有输入参数的时候,则会有错误:ArrayIndexOutOfBoundsException(数组下标越界)
・ 如果输入的参数的内容不是数字,则会有错误:NumberFormatException(数字格式化异常)
・ 如果输入的被除数为零,则会有错误:ArithmeticException(算术异常)
则此程序代码只有一个catch肯定是不够的,需要处理多个异常。 public class Demo05
{
public static void main(String args[])
{
//从运行时处接收参数
int i = 0;
int j = 0;
System.out.println("程序代码执行之前~~~");
try
{
i=Integer.parseInt(args[0]);
j=Integer.parseInt(args[1]);
//下面跟上可能发生错误的代码段
System.out.println(i/j);
System.out.println("-------------------");
}
catch(ArithmeticException ae)
{
//进行异常处理
System.out.println(ae);
System.out.println("1.运算发生了异常~~~");
}
catch(NumberFormatException ne)
{
System.out.println(ne);
System.out.println("2.输入的内容不是数字~~~");
}
catch(ArrayIndexOutOfBoundsException ae)
{
System.out.println(ae);
System.out.println("3.输入的参数个数不正确~~~");
}
System.out.println("程序代码执行之后~~~");
}
}
{
public static void main(String args[])
{
//从运行时处接收参数
int i = 0;
int j = 0;
System.out.println("程序代码执行之前~~~");
try
{
i=Integer.parseInt(args[0]);
j=Integer.parseInt(args[1]);
//下面跟上可能发生错误的代码段
System.out.println(i/j);
System.out.println("-------------------");
}
catch(ArithmeticException ae)
{
//进行异常处理
System.out.println(ae);
System.out.println("1.运算发生了异常~~~");
}
catch(NumberFormatException ne)
{
System.out.println(ne);
System.out.println("2.输入的内容不是数字~~~");
}
catch(ArrayIndexOutOfBoundsException ae)
{
System.out.println(ae);
System.out.println("3.输入的参数个数不正确~~~");
}
catch(Exception e)
{
System.out.println("异常处理");
}
System.out.println("程序代码执行之后~~~");
}
}
在异常处理中,对于catch语句要求处理更粗的异常要放在处理更细的异常之后。 我们验证下效果哈~ public class Demo07
{
public static void main(String args[])
{
//从运行时处接收参数
int i = 0;
int j = 0;
System.out.println("程序代码执行之前~~~");
try
{
i=Integer.parseInt(args[0]);
j=Integer.parseInt(args[1]);
//下面跟上可能发生错误的代码段
System.out.println(i/j);
System.out.println("-------------------");
}
catch(Exception e)
{
System.out.println("异常处理");
}
catch(ArithmeticException ae)
{
//进行异常处理
System.out.println(ae);
System.out.println("1.运算发生了异常~~~");
}
catch(NumberFormatException ne)
{
System.out.println(ne);
System.out.println("2.输入的内容不是数字~~~");
}
catch(ArrayIndexOutOfBoundsException ae)
{
System.out.println(ae);
System.out.println("3.输入的参数个数不正确~~~");
}
System.out.println("程序代码执行之后~~~");
}
}
{
public static void main(String args[])
{
//从运行时处接收参数
int i = 0;
int j = 0;
System.out.println("程序代码执行之前~~~");
try
{
i=Integer.parseInt(args[0]);
j=Integer.parseInt(args[1]);
//下面跟上可能发生错误的代码段
System.out.println(i/j);
System.out.println("-------------------");
}
//只要是程序运行时产生的异常肯定可以使用Exception进行接收
catch(Exception e)
{
System.out.println("异常处理");
}
System.out.println("程序代码执行之后~~~");
}
}
public int div(int i,int j){
return i/j;
}
}
public class Demo10{
public static void main(String args[]){
System.out.println(new Math().div(1,1));
}
} 这个代码现在没问题哈~
//表示此方法必须被处理异常,必须在调用处处理
public int div(int i,int j) throws Exception{
return i/j;
}
}
public class Demo10{
public static void main(String args[]){
System.out.println(new Math().div(1,1));
}
}
//表示此方法必须被处理异常,必须在调用处处理
public int div(int i,int j) throws Exception{
return i/j;
}
}
public class Demo10{
public static void main(String args[]){
try{
System.out.println(new Math().div(1,1));
}catch(Exception e){
System.out.println(e);
}
}
}
//表示此方法必须被处理异常,必须在调用处处理
public int div(int i,int j) throws Exception{
return i/j;
}
}
public class Demo10{
public static void main(String args[]){
try{
System.out.println(new Math().div(1,0));
}catch(Exception e){
System.out.println(e);
}
}
} 出现异常时成功捕获异常
//表示此方法必须被处理异常,必须在调用处处理
public int div(int i,int j) throws Exception{
return i/j;
}
}
public class Demo11{
//此处的错误交给JVM进行处理了
public static void main(String args[]) throws Exception{
System.out.println(new Math().div(1,0));
}
}
public static void main(String args[]){
throw new Exception("自己抛出的异常~~~");
}
}
public static void main(String args[]){
try{
throw new Exception("自己抛出的异常~~~");
}catch(Exception e){
System.out.println(e);
}
}
} 这就是throw关键字的作用哈~~~
定义一个除法操作,要求进入方法之前必须有开始进行除法操作的提示,操作完之后要求有除法操作完成的提示。 class Math{
public int div(int i,int j){
System.out.println("开始除法操作~~~");
int temp=0;
temp=i/j;
System.out.println("开始除法操作~~~");
return temp;
}
}
public class Demo13{
public static void main(String args[]){
System.out.println(new Math().div(1,1));
}
}
public int div(int i,int j){
System.out.println("开始除法操作~~~");
int temp=0;
temp=i/j;
System.out.println("开始除法操作~~~");
return temp;
}
}
public class Demo13{
public static void main(String args[]){
System.out.println(new Math().div(1,0));
}
} 这个就出现异常了哈,结束语句也就不出来了
//所有的异常肯定交给调用处处理
public int div(int i,int j) throws Exception{
System.out.println("开始除法操作~~~");
int temp=0;
temp=i/j;
System.out.println("结束除法操作~~~");
return temp;
}
}
public class Demo13{
public static void main(String args[]){
try{
System.out.println(new Math().div(1,0));
}catch(Exception e){
System.out.println(e);
}
}
}
//所有的异常肯定交给调用处处理
public int div(int i,int j) throws Exception{
System.out.println("开始除法操作~~~");
int temp=0;
//所有的异常不能自己处理,必须交给被调用处处理
try{
temp=i/j;
}catch(Exception e){
//需要把异常往上抛
throw e;
}finally{
System.out.println("结束除法操作~~~");
}
return temp;
}
}
public class Demo13{
public static void main(String args[]){
try{
System.out.println(new Math().div(1,0));
}catch(Exception e){
System.out.println(e);
}
}
}
public MyException(String msg){
super(msg);
}
public String toString(){
return super.getMessage();
}
}
public class Demo14{
public static void main(String args[]){
try{
throw new MyException("自己定义的异常~~~");
}catch(Exception e){
System.out.println(e);
}
}
}
异常的捕获:try{}catch:try捕获异常,之后把异常类的实例化对象与catch中的类型相比较,如果合适,则进行异常的处理。
Finlly关键字
面试题:
请说出:final和finally关键字的区别
throw和throws关键字 ##############################################################
本文出自 “王乾De技术博客” 博客,谢绝转载!