每天学点Java知识 ------> throw关键字

上一篇 / 下一篇  2014-07-23 16:58:14 / 个人分类:Java学习

    如果用户想自己手工抛出一个实例化对象,可以通过throw完成
 
例如:throw关键字
public class Test{
  public static void main(String args[]){
    try{
      throw new Exception("抛出异常!");
    }cathc(Exception e){
      e.printStackTrace();
    }
  }
}
 
程序运行结果:
java.lang.Exception:抛出异常!
  at Test.main(Test.java:4)

    由于存在了throw关键字,程序会手工抛出一个异常类的实例化对象,而此时的程序必须使用try...catch语句进行处理,或者在方法上增加一个throws声明,否则程序就会编译出错

TAG:

 

评分:0

我来说两句

日历

« 2024-03-29  
     12
3456789
10111213141516
17181920212223
24252627282930
31      

我的存档

数据统计

  • 访问量: 30040
  • 日志数: 41
  • 建立时间: 2014-07-17
  • 更新时间: 2014-07-30

RSS订阅

Open Toolbar