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. Unity3D教程:无缝地形场景切换的解决方法

    http://www.unitymanual.com/6718.html 当我们开发一个大型项目的时候-会遇到这样的问题(地形场景的切换)这个只是字面意思-并不是重场景1的100  100 100坐标 ...

  2. 2016 Noip提高组

    2557. [NOIP2016]天天爱跑步 ★★☆   输入文件:runninga.in   输出文件:runninga.out   简单对比时间限制:2 s   内存限制:512 MB [题目描述] ...

  3. 一个Java语言所写的shop网站框架明细

    核心框架Spring Framework :作为一个优秀的开源框架,是为了解决企业应用程序开发复杂性而创建的.框架的主要优势之一就是其分层架构,分层架构允许您选择使用哪一个组件,同时为 J2EE 应用 ...

  4. java基础第一篇

    1.JDK:Java Development kit 能对Java程序编译,运行 包含JRE JRE:Java Runtime Environment 能对Java程序运行 包含JVM和一些核心类库 ...

  5. Response.Redirect 产生的“正在中止线程”错误

    Response.Redirect 产生的“正在中止线程”错误 今天在开发调试过程中,出现在一个 "正在中止线程"异常信息. 调用Response.Redirect()方法产生的, ...

  6. python3错误之TypeError: 'dict_items' object is not callable

    这种错误出现在循环结构中套循环结构,而循环时内部循环为字典,外部循环为该字典调用items方法后取到的值,内部循环调用外部循环中遍历的结果: 解决方案: 将外部循环的items()方法调用改为.key ...

  7. Codeforces 1137B(kmp next数组构造)

    为了物尽其用,Next求出最多有哪部分能重复使用,然后重复使用就行了-- const int maxn = 5e5 + 5; char s[maxn], t[maxn]; int cnts0, cnt ...

  8. 最大xor,and,or

    http://210.33.19.103/contest/998 and,or部分 并不用01trie,题目&题解:https://blog.csdn.net/dreaming__ldx/ar ...

  9. C#项目中的bin目录和obj目录的区别

    C#项目中的bin目录和obj目录的区别 1.关于bin目录和obj目录 Bin目录用来保存项目生成后程序集,它有Debug和Release两个版本,分别对应的文件夹为bin/Debug和bin/Re ...

  10. C#操作Windows用户

    首先需要引入System.DirectoryServices.dll using System; using System.Collections.Generic; using System.Dire ...