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("=========计算结束=========");
}
}

throws标识此方法出异常但不处理,谁调谁处理。

package com.swift;

public class Exception3
{
public static void main(String args[]) throws Exception{
Operate o=new Operate();
System.out.println("=============计算之前=============="); int temp=o.div(Integer.parseInt(args[0]),Integer.parseInt(args[1]));
System.out.println("计算结果"+temp);
System.out.println("=============计算之后==========");
}
}
class Operate
{
public int div(int i,int j) throws Exception{
int temp=i/j;
return temp;
}
}

如果main也throws了,异常抛给了JVM,其实你一个throws不写,JVM依然会默默地处理着异常

注意:一旦方法throws,main必须有回应,否则编译不过。main继续抛相当于出异常之后的程序戛然而止(没有"=============计算之后=========="),这时可用catch捕获抛来的异常进行处理。

package com.swift;

public class Exception3
{
public static void main(String args[]) throws Exception{
Operate o=new Operate();
System.out.println("=============计算之前==============");
try {
int temp=o.div(Integer.parseInt(args[0]),Integer.parseInt(args[1]));
System.out.println("计算结果"+temp);
}catch(Exception e){
System.out.println("出现了什么异常情况 "+e);
}
System.out.println("=============计算之后==========");
}
}
class Operate
{
public int div(int i,int j) throws Exception{
int temp=i/j;
return temp;
}
}

异常被处理,计算前后正常输出。

throw与throws不同,throw抛出的不是异常类,而是对象,可以人造的对象。

package com.swift;

public class Exception4 {
public static void main(String args[]) {
try {
throw new Exception("抛着玩呢 "); // 抛出人造异常对象
} catch (Exception e) { // 再捕捉这个对象
System.out.println("你抛的是什么呢?"+e);
}
} }

如果不想抛Exception类的对像,要抛自己的

package com.swift;

public class Exception5 {
public static void main(String args[]) {
try {
throw new MyException("自定义异常。");
} catch (Exception e) {
System.out.println("这个异常是 " + e); // e.printStackTrace();
}
}
}
//通过继承实现自己的异常类
class MyException extends Exception {
public MyException(String msg) {
//把参数传给父类
super(msg);
}
}

Java异常 Exception类及其子类的更多相关文章

  1. Java异常(Exception)

    Java异常:运行期出现的错误 1. Java异常是Java提供的用于处理程序中错误的一种机制: 2. 错误指的是程序运行期间发生的异常事件,如除零溢出.数组下标越界.读取的文件不存在.... 3. ...

  2. 浅谈java异常[Exception]

    学习Java的同学注意了!!! 学习过程中遇到什么问题或者想获取学习资源的话,欢迎加入Java学习交流群,群号码:589809992 我们一起学Java! 一. 异常的定义 在<java编程思想 ...

  3. java异常Exception

    学习笔记: 一.程序的异常:Throwable 严重问题:Error ,我们不处理.这种问题一般很严重,不如内存溢出 问题:Exception 编译问题:不是RuntimeException异常.必须 ...

  4. Java异常 - Exception总结

    这篇blog总结的比较详细了. 如下图所示,在Java中所有异常的基类均为Throwable类.有两个子类,分别为Exception和Error.其中Error主要由JVM处理,比如OutOfMemo ...

  5. java中Collection类及其子类

    1:对象数组(掌握) (1)数组既可以存储基本数据类型,也可以存储引用类型.它存储引用类型的时候的数组就叫对象数组. 2:集合(Collection)(掌握) (1)集合的由来? 我们学习的是Java ...

  6. java异常——Exception、RuntimException

    一.Exception和RuntimeException的区别 Exception是RuntimeException的父类,使用了 Exception 的类都必须对异常进行处理(try / throw ...

  7. Java 异常Exception e中e的getMessage()和toString()以及 e.printStackTrace();方法的区别

    Exception e中e的getMessage()和toString()方法的区别: 示例代码1: public class TestInfo {     private static String ...

  8. Java 异常Exception e中e的getMessage()和toString()方法的区别

    示例代码1: public class TestInfo { private static String str =null; public static void main(String[] arg ...

  9. 基本的数据类型分析----java.lang.Number类及其子类分析

    本文转自http://blog.csdn.net/springcsc1982/article/details/8788345 感谢作者 编写了一个测试程序,如下: int a = 1000, b= 1 ...

随机推荐

  1. 2013 Noip提高组 Day1

    3285 转圈游戏 2013年NOIP全国联赛提高组  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond 题解       题目描述 Description ...

  2. [Xcode 实际操作]二、视图与手势-(8)UIView视图的纹理填充

    目录:[Swift]Xcode实际操作 本文将演示将导入的图片作为纹理,平铺整个屏幕. 往项目中导入一张图片. 点击底部左下角的图标->[Import]->选择需要导入的图片->[O ...

  3. 使用命令动态更新JAR包中的文件

    动态更新JAR包中的文件,经本人实际测试可正常执行! 一.查询jar包中要替换的文件位置 jar  -tvf  gateway.jar  |  grep  topjui.config.js 二.在当前 ...

  4. Java 分析工具汇总

    http://blog.csdn.net/fenglibing/article/details/6411999 jps  jps -mlvV jmap jmap -heap <pid>   ...

  5. 51Nod 1873 初中的算术

    大神的字符串快速幂 #include <iostream> #include <string> #include <algorithm> #include < ...

  6. MFC中的模态对话框与非模态对话框

    模态对话框创建: MyDialog mydlg; mydlg.DoModal() 当前只能运行此模态对话框,且停止主窗口的运行,直到模态对话框退出,才允许主窗口运行. 模态对话框的关闭顺序: OnCl ...

  7. centos下svn的ldap认证配置

    前提:完成svn的基本安装 一.安装sasl相关组件 #yum install -y cyrus-sasl cyrus-sasl-lib cyrus-sasl-plain 二.查看SASL版本和提供的 ...

  8. ZOJ Saddle Point 数学思维题

    http://acm.zju.edu.cn/onlinejudge/showContestProblem.do?problemId=5564   根据它的定义是行最小,列最大. 可以证明鞍点是唯一的. ...

  9. JavaScriptSerializer类序列化日期时需要注意的问题

    1.让我们来看看使用JavaScriptSerializer类序列化日期会出现什么问题? 1)创建用于序列化的测试类,如下: public class Person { public int ID { ...

  10. asp.net网页跳转

    1.Response.Redirect("http://www.bcbbs.net",false);    目标页面和原页面可以在2个服务器上,可输入网址或相对路径.后面的bool ...