Java - Difference between throw and throws in Exception handling

转载 2015年07月10日 10:57:22

http://javarevisited.blogspot.de/2012/02/difference-between-throw-and-throws-in.html

Sometimes I can't open this page without proper proxy configuration, so I have to copy the content here.


Main difference between throw and throws is in their usage and functionality. where throws is used in method signature to declare Exception possibly thrown by any method, throw is actually used to throw Exception in Java code.

Here is an example of both throw and throws keyword which makes it easy to understand difference between them.

public void shutdown() throws IOException{
        throw new IOException("Unable to shutdown");
}

1)  You can declare multiple exception thrown by method in throws keyword by separating them in common e.g. throws IOException, ArrayIndexBoundException etc, while you can only throw one instance of exception using throw keyword e.g. throw new IOException("not able to open connection").

2)  throws keyword gives a method flexibility of throwing an Exception rather than handling it. with throws keyword in method signature a method suggesting its caller to prepare for Exception declared in throws clause, specially in case of checked Exception and provide sufficient handling of them. On the other hand throw keyword transfer control of execution to caller by throwing an instance of Exception.

throw keyword can also be used in place of return as shown in below example:

private static boolean shutdown() {
        throw new UnsupportedOperationException("Not yet implemented");
}

3)  throws keyword cannot be used anywhere except method signature while throw keyword can be used inside method or static initializer block as shown in example.

static{
        try {
            throw new Exception("Not able to initialized");
        } catch (Exception ex) {
            Logger.getLogger(ExceptionTest.class.getName()).log(Level.SEVERE, null, ex);
        }
}

4) throw keyword can also be used to break a switch statement without using break keyword as shown in below example:

int number = 5;
switch(number){
            case 1:
                throw new RuntimeException("Exception number 1");
            case 2:
                throw new RuntimeException("Exception number 2");
}


Java异常之throws和throw 以及异常使用注意的原则

一.使用throws抛出异常如果在当前方法不知道该如何处理该异常时,则可以使用throws对异常进行抛出给调用者处理或者交给JVM。JVM对异常的处理方式是:打印异常的跟踪栈信息并终止程序运行。 t...
  • sdr_zd
  • sdr_zd
  • 2017年07月21日 21:20
  • 1419

Difference between throw and throws in Exception handling - Java Example

Difference between throw and throws keyword on Exception handling in Java is a popular core java int...
  • chenyujin999
  • chenyujin999
  • 2013年11月16日 13:40
  • 860

再探java基础——throw与throws

再探java基础——throw与throws 以前虽然知道一些异常的处理,也用过一些,但是对throw和throws区别还是有不太清楚。今天用实例测试一下。 异常处理机制 异常处理是对可能出现的...
  • luoweifu
  • luoweifu
  • 2013年08月31日 14:30
  • 72740

throw throws Throwable 关联于区别

总结:throw 是在方法中抛出一个异常,不能单独使用 。 throws是在方法定义处或者类定义处声明该类或方法可能产生的异常,(一个或者多个).----也可以理解为将方法内部异...
  • younkerjqb
  • younkerjqb
  • 2013年08月20日 10:25
  • 3402

throw和throws用法

1.java处理异常方式在java代码中如果发生异常的话,jvm会抛出异常对象,导致程序代码中断,这个时候jvm在做的操作就是:创建异常对象,然后抛出,比如: int i= 1; int j = 0...
  • wy5612087
  • wy5612087
  • 2015年08月22日 17:11
  • 12825

java异常的捕获及处理 兼论throws与throw

java异常的捕获及处理 兼论throws与throw
  • speedwaycl
  • speedwaycl
  • 2015年09月12日 16:47
  • 1342

java throws exception

这方面主要有以下几种类型的题目: 1 try中throw一个exception,能否直接catch? public void method1(){ try{ throw n...
  • freemindhack
  • freemindhack
  • 2014年06月05日 12:07
  • 546

java异常处理之throw, throws,try和catch

    程序运行过程中可能会出现异常情况,比如被0除、对负数计算平方根等,还有可能会出现致命的错误,比如内存不足,磁盘损坏无法读取文件等,对于异常和错误情况的处理,统称为异常处理。    Java异常...
  • zhouyong80
  • zhouyong80
  • 2007年11月29日 20:19
  • 61560

Java异常处理之throw, throws,try和catch

程序运行过程中可能会出现异常情况,比如被0除、对负数计算平方根等,还有可能会出现致命的错误,比如内存不足,磁盘损坏无法读取文件等,对于异常和错误情况的处理,统称为异常处理。Java异常处理主要通过5个...
  • xx326664162
  • xx326664162
  • 2015年11月04日 14:03
  • 2359

Java Exception Handling -java异常处理

Java  Exception Handling -java异常处理 java中的异常 异常是程序在编译时或者运行时出现的意想不到的场景,比如在书写java代码时候不遵守java语法规则会导致编译...
  • doctor_who2004
  • doctor_who2004
  • 2016年09月29日 00:02
  • 1017
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Java - Difference between throw and throws in Exception handling
举报原因:
原因补充:

(最多只允许输入30个字)