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. string.Format 处理 double 的问题

    出处: http://www.cnblogs.com/albert-struggle/archive/2012/05/22/2512744.html 1.格式化货币(跟系统的环境有关,中文系统默认格式 ...

  2. Educational Codeforces Round 19 A, B, C, E(xjb)

    题目链接:http://codeforces.com/contest/797 A题 题意:给出两个数n, k,问能不能将n分解成k个因子相乘的形式,不能输出-1,能则输出其因子: 思路:将n质因分解, ...

  3. 洛谷P2473 [SCOI2008]奖励关(期望+状压)

    传送门 我数学期望还是太差了…… 先考虑状压模型,设$dp[i][S]$表示第$i$轮,当前宝物状态为$S$,能获得的最大期望分数 然而这个模型有一个问题,第$i$轮不一定能达到状态$S$ 那么考虑转 ...

  4. Mysql用户root密码找回

    1.本试例的环境如下: 2.mysql数据库的版本如下,此数据库运行多实例: mysql Ver 15.1 Distrib 10.2.24-MariaDB, for Linux (x86_64) us ...

  5. VC 中TEXT、_T、L的区别

    http://i.cnblogs.com/EditPosts.aspx?opt=1 对于从VC++6.0转到VS2005编译环境中的程序员.往往会碰到字符集之间的转换. VC6.0采用的是ANSI字符 ...

  6. java的无序机制

    简单说一下上面提到的无序写,这是jvm的特性,比如声明两个变量,String a; String b; jvm可能先加载a也可能先加载b.同理,instance = new Singleton();可 ...

  7. 数据库 | 远程连接centos7上数据库

    用root身份进入远程服务器控制台: 进入Mysql命令: # mysql -uroot -p 或者在本地上连接到远程主机上的MySQL: 假设远程主机的IP为:10.0.0.1,用户名为root,密 ...

  8. Gym - 101810B ACM International Collegiate Programming Contest (2018)

    bryce1010模板 http://codeforces.com/gym/101810 #include <bits/stdc++.h> using namespace std; #de ...

  9. libev 使用

    观察器 IO ev_io_init (ev_io *, callback, int fd, int events) ev_io_set (ev_io *, int fd, int events) I/ ...

  10. java.lang.IndexOutOfBoundsException: Index: 0, Size: 0

    在往数据库添加数据需要判断数据库中是否已有记录,判断的返回结果通常是List.在List为空的情况下,调用其方法需要格外注意,例如:调用get()则会报下标越界的异常. 当然还可以联想到其他情况,当判 ...