我是靠谱客的博主 成就板凳,最近开发中收集的这篇文章主要介绍JavaSE学习总结---异常,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

异常

异常在java中以类的方式存在,也有搞糟方法,可以创建对象

UML图就是软件设计图,就是类与类之间的关系图。对面向对象编程,starUML,rational rose 画图工具

编译时异常,运行时异常都发生在运行阶段。编译阶段异常是不会发生的,因为编译时异常必须在编译(编写)是预先处理,如果不处理编译器就会报错,因此得名。异常的发生就是在new对象,所以所有的异常都在运行时发生

对异常的两种处理方式
1,在方法声明的位置上,使用throws关键字,抛给上一级
2,try-catch语句捕捉

public class Test1 {
public static void main(String[] args) {
/*
程序执行到此处发生了,Exception in thread "main" java.lang.ArithmeticException: / by zero异常
底层new了一个ArithmeticException异常对象,然后抛出了,由于是main方法调用的,所以这个异常抛给了main方法
main方法无法处理,将异常自动抛给JVM,JVM最终终止程序
*/
System.out.println(100/0);
System.out.println("hello world");
}
}

一般不建议在main方法上使用throws,如果方法连续调用,上抛的话要一层一层的抛。
try catch相当于把事拦下了,throws上报,要一层一层报

只要异常没有捕捉,一直上报,此方法后续代码不会执行
try语句中出现异常,该行下面的代码不会执行
try catch捕捉异常之后,后续代码执行
一个方法体当中的代码抛出异常后,如果上报,此方法结束

如果需要调用者处理,则选择上抛
异常对象的两个方法,Exception e
e.getMseeage()和e.printStactTrace,输出异常简短信息

try catch中的finally的语句
finally是最后执行的并且一定执行
通常在finall语句块中执行完成资源的释放和关闭,比如流的关闭,JDBC六大步中的最后一步

public class Test1 {
public static void main(String[] args) {
try{
System.out.println("hello world");
return;
}finally {
System.out.println("1111");
}
System.out.println("2222");//不会执行的,因为return已经结束了此方法,finally里的语句会执行
}
}
public class Test1 {
public static void main(String[] args) {
try{
System.out.println("hello world");
System.exit(0);//退出jvm,这样finall就不会执行了
}finally {
System.out.println("1111");
}
System.out.println("2222");
}
}

面试题

public class Test1 {
public static void main(String[] args) {
int result =m();
System.out.println(result);
}
public static int m(){
int i=100;
try {
return i;
}finally {
i++;
}
}
}/输出结果是100,从上而下执行,return一定是最后执行的

经过反编译软件DJ JAVA查看代码是下面这个

public static int m(){
int i=100;
int j=i;
i++;
return j;
}

final finally finalize的区别
final关键字表示最终的,不可变的
finally和try联合使用,在异常处理机制中,里面的代码一定会执行
finalize()是Object类中的一个方法,finalize是标识符,这个方法是JVM的GC垃圾回收器负责调用

java中自定义异常

/*
第一步,定义一个类继承Exception或者RuntimeException
第二步,提供两个构造方法,一个无参,一个有参
*/
public class MyException extends Exception{//编译时异常,如果继承RuntimeException就是运行时异常
public MyException(){
}
public MyException(String s){
super(s);
}
}

改进之前进栈程序,用异常代替之前的return方法结束程序

public class
Shuzumonizhan {
public static void main(String[] args) {
Object[] a=new Object[4];
Stack s=new Stack(a);
Cat c=new Cat();
Cat c2=new Cat();
try {
s.push("SSS");
s.push(1);
s.push(2);
s.push(c);
s.push(5);
} catch (MyException e) {
e.printStackTrace();
}
for (int j = a.length-1; j >= 0; j--) {
System.out.println("栈的数据是: "+a[j]);
}
for (int j = a.length-1; j >= 0; j--) {
System.out.println("栈的数据是: "+a[j]);
}
}
}
class Stack{
Object [] objects;
int index=0;
public Stack(Object[] objects) {
this.objects = objects;
}
public void push(Object o) throws MyException {
if(index<objects.length){
objects[index]=o;
index++;
System.out.println(o +" 进栈成功");
System.out.println("index的值是 "+index);
}else {
//System.out.println("栈满了 "+o+" 进栈失败");
//return;
//创建异常对象
MyException e =new MyException("栈已满,压栈失败");
//手动抛出去
throw e;//有异常报错,要么抓要么抛,自己new自己抓没意思,所以抛出去,让调用者知道
//抛异常就是终止程序了
}
}
public void pop (){
if(index<=0){
System.out.println("栈空了");
}else{
System.out.println(objects[index-1]+" 出栈成功");
objects[index-1]=null;
System.out.println(objects[index-1]);
}
index--;
System.out.println("index的值是 "+index);
}
}
class Cat{
}

重写方法是不能比父类方法抛出更多的异常

异常作业

public class ZhuCe {
public static void main(String[] args) {
System.out.println("请输入用户名,密码");
UserService user1=new UserService("123433333333333333333335678","123");
try {
user1.register();
} catch (MyException e) {
e.printStackTrace();
}
}
}
class UserService{
private String id;
private String password;
public UserService(String id, String password) {
this.id = id;
this.password = password;
}
public void register() throws MyException {
if(id==null||(id.length()<=14&&id.length()>=6)){
System.out.println("注册成功,用户名为:"+id+"密码为: "+password);
}else {
MyException e =new MyException("用户名的长度不在规定范围之内");
throw e;
}
}
}

数组武器作业
有向下转型,转型实现的方法是接口里面的方法

public class Test {
public static void main(String[] args) {
Army army =new Army(4);
//创建武器对象
Tank tank1 =new Tank();
Tank tank2 =new Tank();
Tank tank3 =new Tank();
Tank tank4 =new Tank();
Tank tank5 =new Tank();
try {
army.addWeapon(tank1);
army.addWeapon(tank2);
army.addWeapon(tank3);
army.addWeapon(tank4);
army.addWeapon(tank5);
} catch (MyException e) {
e.printStackTrace();
}
}
}
class Army{
private Weapon [] weapons;
public Army(int i) {
weapons=new Weapon[i];//构造出来一个i的数组,数组值为null
}
public void addWeapon(Weapon weapon) throws MyException {
for (int i = 0; i <weapons.length ; i++) {
if(weapons[i]==null){
weapons[i]=weapon;
return;
}else {
MyException e =new MyException("数组满了,加不进去了");
throw
e;
}
}
}
public void attackAll(){
for (int i = 0; i <weapons.length ; i++) {
if(weapons[i] instanceof
Shootable){//调用子类特有的方法,向下转型,weapon类是父类调用子类坦克的方法
Shootable shootable= (Shootable) weapons[i];
shootable.shoot();
}
}
}
public void moveAll(){
for (int i = 0; i <weapons.length ; i++) {
if(weapons[i] instanceof
Shootable){
Moveable moveable = (Moveable) weapons[i];
moveable.move();
}
}
}
}
class Weapon{
}
class Tank extends Weapon implements Moveable,Shootable{
@Override
public void move() {
System.out.println("坦克在移动");
}
@Override
public void shoot() {
System.out.println("坦克在射击");
}
}
class Gaoshepao extends Weapon implements
Shootable {
@Override
public void shoot() {
System.out.println("高射炮在射击");
}
}
class Wuziche extends Weapon implements Moveable{
@Override
public void move() {
System.out.println("物资车在移动");
}
}
class Zhandouji extends
Weapon implements Moveable,Shootable{
@Override
public void move() {
System.out.println("战斗机在移动");
}
@Override
public void shoot() {
System.out.println("战斗机在射击");
}
}
interface Moveable{
void move();
}
interface Shootable{
void shoot();
}

最后

以上就是成就板凳为你收集整理的JavaSE学习总结---异常的全部内容,希望文章能够帮你解决JavaSE学习总结---异常所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(64)

评论列表共有 0 条评论

立即
投稿
返回
顶部