mysql数据库建设网站,新站点seo联系方式,如何建立网站链接,邯郸市哪里有做网站的软件设计之Java入门视频(13)
视频教程来自B站尚硅谷#xff1a; 尚硅谷Java入门视频教程#xff0c;宋红康java基础视频 相关文件资料#xff08;百度网盘#xff09; 提取密码#xff1a;8op3 idea 下载可以关注 软件管家 公众号 学习内容#xff1a;
该视频共分为1-7…软件设计之Java入门视频(13)
视频教程来自B站尚硅谷 尚硅谷Java入门视频教程宋红康java基础视频 相关文件资料百度网盘 提取密码8op3 idea 下载可以关注 软件管家 公众号 学习内容
该视频共分为1-717部分 本次内容涉及360-389 在写代码时总是需要来回切换界面来看代码要求这里推荐Snipaste可以把截图以窗口形式放在屏幕上 记录内容
内部类异常
1、内部类 1Java中允许将一个类A声明在另一个类B中则类A就是内部类类B称为外部类 2内部类的分类成员内部类(静态、非静态)与局部内部类(方法内、代码块、构造器内) 3成员内部类 作为外部类的成员 a可以调用外部类的结构 b可以被static修饰 c可以被四种不同权限修饰(public、缺省、protected、private) 作为类的成员 a可以定义属性、方法、构造器 b可以被final、abstract修饰 package test;public class InnerClassTest {public static void main(String[] args) {//创建Dog类(静态成员内部类)Person.Dog dog new Person.Dog();dog.show();//创建Bird类(非静态成员内部类)Person person new Person();Person.Bird bird person.new Bird();bird.sing();}}class Person{int age;public void eat(){System.out.println(吃饭);}public void method(){//局部内部类class AA{}}//静态成员内部类static class Dog{int age;public void show(){System.out.println(叫);}}//非静态成员内部类class Bird{int age;public Bird(){}public void sing(){System.out.println(唱歌);Person.this.eat(); //调用外部类的非静态属性}public void display(int age){System.out.println(age); //方法形参System.out.println(this.age);//内部类属性System.out.println(Person.this.age);//外部类属性}}
}2、异常 Error:Java虚拟机无法解决的严重问题。如JVM系统内部错误、资源耗尽等严重情况。比如StackOverflowError和OOM。一般不编写针对性的代码进行处理。 Exception:其它因编程错误或偶然的外在因素导致的一般性问题可以使用针对性的代码进行处理。例如 空指针访问、试图读取不存在的文件、网络连接中断、数组角标越界 运行时异常是指编译器不要求强制处置的异常。一般是指编程时的逻辑错误是程序员应该积极避免其出现的异常。 编译时异常是指编译器要求必须处置的异常。即程序在运行时由于外界因素造成的一般性异常。编译器要求Java程序必须捕获或声明所有编译时异常 Error举例
public class ErrorTest {public static void main(String[] args) {// main(args); 栈溢出 StackOverflowErrorInteger[] arr new Integer [1024*1024*1024] //堆溢出 OOM OutOfMemoryError}
}异常处理:抓抛模型 过程1抛程序在正常执行的过程中一旦出现异常就会在异常代码处生成一个对应异常类的对象。并将此对象抛出。一旦抛出对象以后其后的代码就不再执行。 过程2:“抓”异常的处理方式 1try-catch-finally 2) throws try-catch-finally 1finally是可选的 2使用try将可能出现异常代码包装起来在执行过程中一旦出现异常就会生成一个对应异常类的对象根据此对象类型去catch中进行匹配 3一旦try中的异常对象匹配到某一个catch时就进入catch中进行异常处理一旦处理完成跳出当前try-catch结构(在没有写finally的情况) 4catch中的异常类型如果没有子父类关系声明在上/下不影响 5catch中的异常类型满足子父类关系则要求子类一定要声明在父类之上否则报错unreachalbe 6常用的异常对象处理的方式String getMessage(); printStackTrace(); 7finally中声明的是一定会被执行的代码。即使catch中又出现异常、try/catch中有return语句等情况 8像数据库连接、输入输出流、网络编程Socket等资源JVM是不能自动的回收的我们需要自己手动的进行资源的释放。此时的资源释放就需要声明在finally中。 package test;import org.junit.Test;public class ExceptionTest1 {Testpublic void test1(){String str 1223;str abc;try{int num Integer.parseInt(str);System.out.println(----1);}catch (NumberFormatException e){System.out.println(出现数值转换异常);}System.out.println(----2);}
}throws 1throws 异常类型写在方法的声明处。指明此方法执行时可能会抛出的异常类型。一旦当方法体执行时出现异常仍会在异常代码处生成一个异常类的对象此对象满足throws后异常类型时就会被抛出。异常代码后续的代码就不再执行! 2try-catch-finally:真正的将异常给处理掉了 3throw只是将异常抛给了方法的调用者 4 重写方法异常抛出的规则 子类重写的方法抛出的异常类型不大于父类被重写的方法抛出的异常类型 如何选择try-catch与throws 1如果父类中被重写的方法没有throws方式处理异常则子类重写的方法也不能使用throws意味着如果子类重写的方法中有异常必须使用try-catch-finally方式处理。 2执行的方法a中先后又调用了另外的几个方法这几个方法是递进关系执行的。我们建议这几个方法使用throw的方式进行处理。而执行的方法a可以考虑使用try-catch-finally方式进行处理。 手动抛出异常
package test;public class StudentTest {public static void main(String[] args) {Student student new Student();try {student.regist(-10);System.out.println(student);} catch (Exception e) {System.out.println(e.getMessage());}}
}class Student{private int id;public void regist (int id) throws Exception {if(id 0){this.id id;}else {//手动抛出异常对象// throw new RuntimeException(非法数据); //此处是运行时异常所以不会编译报错throw new Exception(非法数据);}}Overridepublic String toString() {return Student{ id id };}
}
自定义异常类 1继承于现有的异常结构 Exception、RuntimeException 2提供全局常量serialVersionUID 3提供重载的构造器 public class MyException extends Exception{static final long serialVersionUID -702321312412L;public MyException(){}public MyException(String msg){super(msg);}
}
练习 在idea中想要设置main方法的args[]实参 ) package test;public class EcmDef {public static void main(String[] args) {try {int i Integer.parseInt(args[0]);int j Integer.parseInt(args[1]);int result ecm(i,j);System.out.println(result);} catch (NumberFormatException e){System.out.println(数据类型不一致);} catch (ArrayIndexOutOfBoundsException e){System.out.println(缺少命令行参数);} catch (ArithmeticException e){System.out.println(算数异常);} catch (EcDef e) {System.out.println(e.getMessage());}}public static int ecm (int i , int j) throws EcDef {if(i 0 || j 0){throw new EcDef(分子或分母为负数);}return i/j;}
}