java异常类
package shb.java.exception;
/**
* 测试异常类
* @Package:shb.java.exception
* @Description:
* @author shaobn
* @Date 2015-9-7上午11:52:42
*/
public class Demo_2 {
public static void main(String[] args) {
testTD_5();
}
public static void testTD_2(){
TDemo_2 td_2 = new TDemo_2();
try {
td_2.getResult(9, 0);//一旦发生异常,函数中后面的方法不再执行即结束
System.out.println("over");//发生异常后,此句已不能再执行。 直接抛出异常对象,由catch块中异常对象的引用来接收。
} /*catch (Exception e) {
// TODO: handle exception
System.out.println(e.getMessage());
}*///如果一开始就用Exception会覆盖子异常类,则会只执行Exception,其余都不能执行。
catch (ArithmeticException ae) {
// TODO: handle exception
System.out.println(ae.getMessage());
System.out.println("算术异常");
}catch (ArrayIndexOutOfBoundsException aiobe) {
// TODO: handle exception
System.out.println(aiobe.getMessage());
System.out.println("数组越界异常");
}
System.out.println("helloworld");
}
//自定义异常类
public static void testTD_3(){
TDemo_3 td_3 = new TDemo_3();
try {
td_3.getResult_3(9, 0);
System.out.println("over");
} catch (Exception e) {
// TODO: handle exception
System.out.println(e.getMessage());
}
System.out.println("hellobeijing");
}
//自定义的继承RuntimeException类的异常(编译时是不报错的,运行时检查异常)
public static void testTD_4(){ TDemo_4 td_4 = new TDemo_4();
td_4.getResult(9, 0);
}
public static void testTD_5(){
TDemo_5 td_5 = new TDemo_5();
try {
td_5.testMethod();
} catch (Exception e) {
// TODO: handle exception
System.out.println("异常已经处理");
}
}
}
class TDemo_2{
private int age = 10;
public int getAge(){
return this.age;
}
//声明两个异常,算术异常,数组越界异常。
public int getResult(int a,int b) throws ArithmeticException,
ArrayIndexOutOfBoundsException{
byte[] arr = new byte[a];
b = a/b;
arr[a] = 9;
return a/b;
}
}
class TDemo_3{
private int height = 90;
public int getHeight(){
return this.height;
}
public int getResult_3(int a,int b) throws TDemo_3Exception{
if(b==0)
//抛出自定义异常,必须在方法上声明异常类。
throw new TDemo_3Exception("自定义的异常类");
return a/b;
}
}
//编译时被检测的异常
class TDemo_3Exception extends Exception{
public TDemo_3Exception(String str){
super(str);
}
//重写getMessage()方法
public String getMessage(){
return super.getMessage();
} }
//编译时不被检测的异常
class TDemo_4Exception extends RuntimeException{
public TDemo_4Exception(String str){
super(str);
}
}
class TDemo_4{
private int with = 90;
public int getWith(){
return this.with;
}
public int getResult(int a,int b){
if(b==0)
throw new TDemo_4Exception("自定义的runtime异常");
return a/b; }
public void testResult(int a,int b){
if(b<0)
throw new TDemo_4Exception("b小于0");
System.out.println("测试成功");
} }
class TDemo_5Exception extends Exception{ public TDemo_5Exception(String str){
super(str);
} }
class TDemo_5{
public void testMethod() throws TDemo_5Exception{
TDemo_4 td_4 = new TDemo_4();
try {
td_4.testResult(9, -1);
} catch (Exception e) {
// TODO: handle exception
throw new TDemo_5Exception("处理失败");
} }
}
java异常类的更多相关文章
- 面试准备(三) Java 异常类层次结构
在Java中,异常分为受检查的异常,与运行时异常. 两者都在异常类层次结构中.这类容易出选择题 考试你是否掌握了异常类并清楚哪些异常类必须捕获 下面的图展示了Java异常类的继承关系. 图1 粉红色的 ...
- Java 异常类层次结构
在Java中,异常分为受检查的异常,与运行时异常. 两者都在异常类层次结构中. 下面的图展示了Java异常类的继承关系. 图1 粉红色的是受检查的异常(checked exceptions),其必须被 ...
- java——异常类、异常捕获、finally、异常抛出、自定义异常
编译错误:由于编写程序不符合程序的语法规定而导致的语法问题. 运行错误:能够顺利的编译通过,但是在程序运行过程中产生的错误. java异常类都是由Throwable类派生而来的,派生出来的两个分支分别 ...
- Java异常类(Throwable)
一.异常类体系 二.异常类由来与定义 [异常类的由来]:Java把程序在运行时出现的各种不正常情况也看成了对象, 提取属性和行为进行描述,比如异常名称,异常信息,异常发生位置,从而形成了各种异常类 [ ...
- Java异常类复习总结
个人理解先行: 异常类是当在程序出现问题时抛出的一个警告.提示你程序设计或者代码有存在错误的地方. 异常类和Error都继承自Throwable, Throwable继承自Object类. Runti ...
- 课堂动手动脑验证以及自定义异常类实现对异常处理——java异常类
异常(exception):发生在程序执行期间,表明出现了一个非法运行的情况.许多JDK中的方法在检测到非法情况时,都会抛出一个异常对象.例如:数组越界和被0除. 代码验证: package test ...
- Java异常类及处理
异常概述:运行时发生的不正常情况 在java中用类的形式对不正常的情况进行了描述和封装对象. 描述不正常的类,称之为异常类. 异常就是java通过面向对象的思想将问题封装成了对象,用异常类对其进行描述 ...
- java -> 异常类与自定义异常
异常 什么是异常?Java代码在运行时期发生的问题就是异常. 在Java中,把异常信息封装成了一个类.当出现了问题时,就会创建异常类对象并抛出异常相关的信息(如异常出现的位置.原因等). 异常的继承体 ...
- java异常类的妙用
异常类的妙用 以往在使用异常时,只是知道通过异常类的构造方法设置一些出错信息,此外最多就是把引起该异常的原因通过Throwable类的子类一同设置进去.今天在分析springSecurity3.0 ...
随机推荐
- 【Android测试】【随笔】获得App的包名和启动页Activity
◆版权声明:本文出自胖喵~的博客,转载必须注明出处. 转载请注明出处:http://www.cnblogs.com/by-dream/p/5157308.html 前言 经常看到一些刚刚接触Andro ...
- (读书笔记)Asp.net Mvc 与WebForm 混合开发
根据项目实际需求,有时候会想在项目中实现Asp.net Mvc与Webform 混合开发,比如前台框架用MVC,后台框架用WebForm.其实要是实现也很简单,如下: (1)在MVC 中使用Webfo ...
- debian linux下配置lnmp环境
用到哪些就安装哪些 安装配置时遇到: deb cdrom:[Debian GNU/Linux 7.1.0 _Wheezy_ - Official amd64 CD Binary-1 20130615- ...
- [转]Android 延迟执行
开启新线程 new Thread(new Runnable(){ public void run(){ Thread.sleep(XXXX); handler.sendMessage(); //告诉主 ...
- Date and Time Pattern
The following examples show how date and time patterns are interpreted in the U.S. locale. The given ...
- js实例代码
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 消息队列MQ
基本版本 Queue 代码: using System.Collections.Generic; using System.Linq; using System.Web; using System.W ...
- hadoop 文件系统API操作
配置参数:-DHADOOP_USER_NAME=hadoop public class HdfsUtils { private static FileSystem fileSystem; @Befor ...
- NSString的截取
// // main.m // 02-字符串的获取 // // Created by apple on 14-3-20. // Copyright (c) 2014年 apple. All r ...
- 异常信息:由于内部错误,服务器无法处理该请求。有关该错误的详细信息,请打开服务器上的 IncludeExceptionDetailInFaults
有方法说找到web.config 文件修改如下(蓝色部分) <behaviors> <serviceBehaviors> <behavior> ...