c语言时用if...else...来控制异常,java语言所有的异常都可以用一个类来表示,不同类型的异常对应不同的子类异常,每个异常都对应一个异常类的对象。
java异常处理通过5个关键字try、catch、finally、throw、throws进行管理。基本过程是用try包住要监视的语句,如果在try内出现异常,则异常会被抛出,catch中捕获抛出的异常并做处理,finally一定会完成未尽事宜。
练习:
package com.swift; public class exception1 { public static void main(string args[]){ system.out.println("=========计算开始========="); try{ int i=integer.parseint(args[0]); int j=integer.parseint(args[1]); int temp=i/j; system.out.println("计算结果:"+ temp); }catch(arithmeticexception e){ system.out.println("出现了数学异常 "+ e); }catch(arrayindexoutofboundsexception e){ system.out.println("出现了数组异常 "+ e); }catch(numberformatexception e){ system.out.println("出现了格式异常 "+ e); }catch(exception e){ system.out.println("其他异常 "+e); }finally{ system.out.println("不管是否有异常,我都执行。"); } system.out.println("=========计算结束========="); } }
以上这篇java异常 exception类及其子类(实例讲解)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持移动技术网。
如对本文有疑问, 点击进行留言回复!!
HashMap、Hashtable、ConcurrentHashMap三者间的异同
解决RecycleView 中Item包含Edittext时,滑动view复用导致数据错乱的问题
多线程、同步工作原理、死锁案例、Lock接口、线程的生命周期的讲解及实现
网友评论