try
{
// 程序代码
}catch(ExceptionName e1)
{
//Catch 块
}
import java.io.*;
public class ExcepTest{ public static void main(String args[]){
try{
int a[] = new int[2];
System.out.println("Access element three :" + a[3]);
}catch(ArrayIndexOutOfBoundsException e){
System.out.println("Exception thrown :" + e);
}
System.out.println("Out of the block");
}
}
try{
// 程序代码
}catch(异常类型1 异常的变量名1){
// 程序代码
}catch(异常类型2 异常的变量名2){
// 程序代码
}catch(异常类型2 异常的变量名2){
// 程序代码
}
try {
file = new FileInputStream(fileName);
x = (byte) file.read();
} catch(FileNotFoundException f) { // Not valid!
f.printStackTrace();
return -1;
} catch(IOException i) {
i.printStackTrace();
return -1;
}
import java.io.*;
public class className
{
public void deposit(double amount) throws RemoteException
{
// Method implementation
throw new RemoteException();
}
//Remainder of class definition
}
import java.io.*;
public class className
{
public void withdraw(double amount) throws RemoteException,
InsufficientFundsException
{
// Method implementation
}
//Remainder of class definition
}
try{
// 程序代码
}catch(异常类型1 异常的变量名1){
// 程序代码
}catch(异常类型2 异常的变量名2){
// 程序代码
}finally{
// 程序代码
}
public class ExcepTest{
public static void main(String args[]){
int a[] = new int[2];
try{
System.out.println("Access element three :" + a[3]);
}catch(ArrayIndexOutOfBoundsException e){
System.out.println("Exception thrown :" + e);
}
finally{
a[0] = 6;
System.out.println("First element value: " +a[0]);
System.out.println("The finally statement is executed");
}
}
}
class MyException extends Exception{
}
import java.io.*;

//自定义异常类,继承Exception类
public class InsufficientFundsException extends Exception
{
//此处的amount用来储存当出现异常(取出钱多于余额时)所缺乏的钱
private double amount;
public InsufficientFundsException(double amount)
{
this.amount = amount;
}
public double getAmount()
{
return amount;
}
}
import java.io.*;

//此类模拟银行账户
public class CheckingAccount
{
//balance为余额,number为卡号
private double balance;
private int number;
public CheckingAccount(int number)
{
this.number = number;
}
//方法:存钱
public void deposit(double amount)
{
balance += amount;
}
//方法:取钱
public void withdraw(double amount) throws
InsufficientFundsException
{
if(amount <= balance)
{
balance -= amount;
}
else
{
double needs = amount - balance;
throw new InsufficientFundsException(needs);
}
}
//方法:返回余额
public double getBalance()
{
return balance;
}
//方法:返回卡号
public int getNumber()
{
return number;
}
}
public class BankDemo
{
public static void main(String [] args)
{
CheckingAccount c = new CheckingAccount(101);
System.out.println("Depositing $500...");
c.deposit(500.00);
try
{
System.out.println("\nWithdrawing $100...");
c.withdraw(100.00);
System.out.println("\nWithdrawing $600...");
c.withdraw(600.00);
}catch(InsufficientFundsException e)
{
System.out.println("Sorry, but you are short $"
+ e.getAmount());
e.printStackTrace();
}
}
}

最新文章

  1. angular学习笔记(二十八)-$http(6)-使用ngResource模块构建RESTful架构
  2. Informatica Lookup Transformation组件的Connect 与Unconnected类型用法
  3. TCMalloc:线程缓冲的Malloc
  4. 关于最近Google无法正常访问的变通之法(已经被墙)
  5. Mysql函数instr、locate、position VS like
  6. 一个1年前的T-SQL问题
  7. (centos)linux下访问双系统windows7文件系统
  8. 【转】ubuntu64,ndk-r9 编译 ffmpeg 2.1.1的config文件
  9. JavaScript的事件对象_其他属性和方法
  10. 获取js提交数据
  11. iOS多线程自定义operation加载图片 不重复下载图片
  12. Twitter注册
  13. CF_225B _Well-known Numbers
  14. java导入导出excel常用操作小结及简单示例
  15. bzoj 1196
  16. Mybatis第七篇【resultMap、resultType、延迟加载】
  17. 电子称DIY(贴应变片+写代码)
  18. 关于token登录逻辑分析
  19. pgrep
  20. WinDbg调试 C# dmp

热门文章

  1. tableau创建定量值地图
  2. BZOJ:2815: [ZJOI2012]灾难
  3. EF Core的安装及入门
  4. 二十二、CI框架之模型别名
  5. Essay写作关键:严谨的逻辑关系
  6. 吴裕雄--天生自然TensorFlow2教程:测试(张量)- 实战
  7. Unity3d中渲染到RenderTexture的原理,几种方式以及一些问题
  8. JavaEE--JNDI(下,实现)
  9. 题解P4201: [NOI2008]设计路线
  10. Windb实践之Script Command