文章目录
- 1.java将如何处理异常?
- 1.1Java中的异常处理机制的简单原理和应用。
- 1.1 可以用以下代码实现:
- 2.如何处理异常
- 2.1 try , catch
- 2.2 finally
- 2.3 语法结构
- 3.面试题
- 3.1 面试题 一
- 3.2 面试题 二
- 3.3 面试题 三 (终极)
- 4.抛出异常
- 5.自定义异常
- 6.DeBug bug 臭虫
- 7. Throwable
1.java将如何处理异常?
1.Java通过面向对象的方法进行异常处理,把各种不同的异常进行分类,并提供了良好的接口。
2.在Java中,每个异常都是一个对象,它是Throwable类或其它子类的实例。
当一个方法出现异常后便抛出一个异常对象,该对象中包含有异常信息,调用这个对象的方法可以捕获到这个异常并进行处理。
3.ava的异常处理是通过5个关键词来实现的:try、catch、throw、throws和finally。
1.1Java中的异常处理机制的简单原理和应用。
- 当JAVA程序违反了JAVA的语义规则时,JAVA虚拟机就会将发生的错误表示为一个异常。违反语义规则包括2种情况。
- 一种是JAVA类库内置的语义检查。例如数组下标越界,会引发IndexOutOfBoundsException;访问null的对象时会引发NullPointerException。
另一种情况就是JAVA允许程序员扩展这种语义检查,程序员可以创建自己的异常,并自由选择在何时用throw关键字引发异常。
所有的异常都是java.lang.Thowable的子类。
- Java通过面向对象的方法进行异常处理,把各种不同的异常进行分类,并提供了良好的接口。
在Java中,每个异常都是一个对象,它是Throwable类或其它子类的实例。
当一个方法出现异常后便抛出一个异常对象,该对象中包含有异常信息,调用这个对象的方法可以捕获到这个异常并进行处理。
Java的异常处理是通过5个关键词来实现的:try、catch、throw、throws和finally。 - 一般情况下是用try来执行一段程序,如果出现异常,
系统会抛出(throws)一个异常,这时候你可以通过它的类型来捕捉(catch)它,或最后(finally)由缺省处理器来处理。
用try来指定一块预防所有“异常”的程序。紧跟在try程序后面,应包含一个catch子句来指定你想要捕捉的“异常”的类型。
throw语句用来明确地抛出一个“异常”。throws用来标明一个成员函数可能抛出的各种“异常”。 - Finally为确保一段代码不管发生什么“异常”都被执行一段代码。
可以在一个成员函数调用的外面写一个try语句,在这个成员函数内部写另一个try语句保护其他代码。每当遇到一个try语句,
“异常”的框架就放到堆栈上面,直到所有的try语句都完成。
如果下一级的try语句没有对某种“异常”进行处理,堆栈就会展开,直到遇到有处理这种“异常”的try语句。
1.1 可以用以下代码实现:
下面展示一些 Test 测试 代码片
。
public class Test {
public static void main(String[] args) throws InterruptedException {
System.out.println("hello");
int a = 10;
int b = a/0; // 运行时异常。 除数不能为零。
int[] arrs = new int[10];
arrs[11] = 10; // 运行时异常。 数组下标越界异常。
System.out.println("正在登陆中");
for(int i=0;i<100;i++){
Thread.sleep(1000); // 请看main方法上的throws 删除它,你就会看见非运行时异常。需要强制处理的异常。
System.out.println("*");
if(i==10){
break;
}
}
System.out.println("登录成功");
}
}
2.如何处理异常
2.1 try , catch
- try 用来定义可能出现异常的代码块。
- catch 用来捕捉异常, 处理异常。
下面展示一些 Test 测试 代码片
。
public class Test2 {
public static void main(String[] args) {
System.out.println("hello1");
System.out.println("hello2");
System.out.println("hello3");
System.out.println("hello4");
System.out.println("hello5");
try{
int a = 10;
int b = a/0;
}catch(ArithmeticException e){
System.out.println("保险公司报销");
}
System.out.println("hello6");
try{
System.out.println("hello7");
System.out.println("hello8");
System.out.println("hello9");
String string = null;
System.out.println(string.charAt(2));
}catch(NullPointerException e){
System.out.println("第二个异常被处理掉");
}
System.out.println("hello10");
}
}
2.2 finally
- finally 无论是否出现异常,都必须执行的代码块。一般用来关闭连接,释放流。
下面展示一些 Test 测试 代码片
。
public class Test3 {
public static void main(String[] args) {
System.out.println("hello1");
System.out.println("hello2");
System.out.println("hello3");
System.out.println("hello4");
System.out.println("hello5");
try{
int a = 10;
int b = a/0;
}catch(ArithmeticException e){
System.out.println("保险公司报销");
}finally{
System.out.println("我继续喝咖啡");
}
System.out.println("hello6");
try{
System.out.println("hello7");
System.out.println("hello8");
System.out.println("hello9");
String string = null;
// System.out.println(string.charAt(2));
}catch(NullPointerException e){
System.out.println("第二个异常被处理掉了");
}finally{
System.out.println("我继续喝咖啡");
}
System.out.println("hello10");
}
}
2.3 语法结构
下面展示一些 Test 测试 代码片
。
public class Test4 {
public static void main(String[] args) {
try{
// 可能出现异常的代码块。
}catch(Exception e){
// Exception 或子类,处理异类。
}finally{
// 无论是否出现异常,都必须执行代码块。 finally不是不想存在的。
}
}
}
3.面试题
3.1 面试题 一
下面展示一些 面试题一 测试 代码片
。
public class Test5 {
public static void main(String[] args) {
// 一个catch只处理一个异常。
// 一个try后面可以有多个catch吗? 可以。
// 要求: 异常的父类必须写在子类之后。
try {
int a = 10/1;
String str = null;
System.out.println(str.charAt(0));
System.out.println("77777777");
int[] ars = new int[3];
ars[3] = 0;
}catch (ArithmeticException e){
System.out.println("除法异常处理");
}catch (NullPointerException e){
System.out.println("空指针异常处理");
}catch (ArrayIndexOutOfBoundsException e){
System.out.println("数组异常处理");
}catch (Exception e){
System.out.println("父类异常");
}
System.out.println("88888888888888888888");
}
}
3.2 面试题 二
- try 能否独立存在。 不能
- catch 能够独立存在。 不能
- finaly 能够独立存在。 不能
- try 后面能够直接写 finally > 可以。
- finally 可以不存在吗: 可以
- try 中可以继续try吗? 可以
- catch 中可以继续try吗? 可以
- finally 中可以继续try吗? 可以
下面展示一些 面试题二 测试 代码片
。
public class Test6 {
public static void main(String[] args) {
try{
try{
}catch(Exception e){
}
}catch(Exception e){
try{
}catch (Exception r){
}
} finally{
try{
}catch(Exception r){
}
}
}
}
3.3 面试题 三 (终极)
下面展示一些 面试题三 测试 代码片
。
public class Test7 {
public static void main(String[] args) {
int a = show("",null,"a");
int b = show(null,"","a");
int c = show("a",null,"");
System.out.println(a+" "+b+" "+c );
}
public static int show(String a,String b,String c){
try{
a.length();
b.length();
c.length();
return 1;
}catch (Exception e){
return 2;
}finally{
return 3;
}
}
}
4.抛出异常
- throw 抛出异常。
- throws 当前位置不处理,交给调用位置处理异常。
下面展示一些 Tast 代码片
。
public class Test8 {
public static void main(String[] args) throws Exception {
try{
System.out.println("hello1");
System.out.println("hello1");
System.out.println("hello1");
System.out.println("hello1");
int a = 10/0;
}catch(Exception e){
throw e;// 抛出异常。
}
// 调用位置。
method();
}
// throws 表示当前位置不处理异常,交给调用位置处理。
public static void method()throws Exception{
int a = 10/0;
}
}
5.自定义异常
- 继承自: Exception。
- 使用throw 抛出自定义异常子类。
下面展示一些 Test 代码片
。
public class Test9 {
public static void main(String[] args) throws Exception {
String name = "张三6";
if(!name.equals("张三")){
throw new Exception("姓名信息错误");
}
}
}
6.DeBug bug 臭虫
Bug : 臭虫。 计算机中的异常,错误。
*
- DeBug 单步调试:
- F6:单步执行。
- F5:进入方法。
- F7:跳出方法。
- F8:结束调试。
下面展示一些 Demo 代码片
。
public class DeBugDemo {
public void show(){
System.out.println("hello1");
System.out.println("hello2");
System.out.println("hello3");
System.out.println("hello4");
System.out.println("hello5");
System.out.println("hello6");
System.out.println("hello7");
System.out.println("hello8");
}
}
下面展示一些 DeBug 代码片
。
public class Test10 {
public static void main(String[] args) {
System.out.println("hello1");
System.out.println("hello2");
System.out.println("hello3");
System.out.println("hello4");
int a = 10;
int b = 20;
System.out.println("hello5");
System.out.println("hello6");
DeBugDemo deb = new DeBugDemo();
deb.show();
System.out.println("hello7");
System.out.println("hello8");
System.out.println("hello9");
System.out.println("hello10");
System.out.println("hello11");
System.out.println("hello12");
}
}
7. Throwable
- 所有异常和错误的父类。
- throwable 有两个子类。
——————————————————————
————————————————————
- Error错误Error表示系统级的错误和程序无法通过修改程序处理的异常,内存泄露。
Exception 异常Exception表示需要捕捉或者需要程序进行处理的异常,如果程序正常执行,将不会出现异常。
Exception: 运行时异常,非运行时异常(检查异常)
运行时异常: 程序运行时出现的异常,由虚拟机捕捉,并抛出到控制台的异常。
非运行时异常:java编译器要求方法必须处理或抛出可能发生的非运行时异常。
——————————————————……0.0……