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");
}


相关文章推荐

Difference between start and run method in Thread – Java Tutorial

Difference between start and run method in Thread – Java Tutorial Why do one call start method of t...
  • aqzwss
  • aqzwss
  • 2016年04月29日 09:45
  • 307

Difference between Externalizable and Serializable in Java

One obvious difference that Serializable is a marker interface and doesn't contain any methods whe...

What is difference between HashMap and Hashtable in Java?

HashMap vs Hashtable in Java Though both Hashtable and HashMap are data-structure based upon hashin...
  • e891377
  • e891377
  • 2017年05月23日 23:14
  • 169

Difference between ArrayList and Vector In java

Difference between ArrayList and Vector In java ArrayList and Vector both use Array as a data struc...

Difference between Vector and ArrayList in java?

面试的时候也总是问的一个问题,说一下Vector与ArrayList的区别? 这个问题以前没太注意过,所以查询资料的时候,看到网上对这个问题的解释几乎是相同(应该都是copy的),千篇一律且比较啰嗦...

Difference between Enumeration and Iterator in Java

深入比较Enumeration和Iterator的差别

Java Exception系列一:关于throws和throw

throws 异常说明,表示某方法可能 会抛出异常,用于某个方法后,后面接潜在的异常列表,如void func() throws AException,BException {//...。 ...

Java异常处理( try catch finally throw throws exception error)

Java异常处理机制当程序中抛出一个异常后,程序从程序中导致异常的代码处跳出,java虚拟机检测寻找和try关键字匹配的处理该异常的catch块,如果找到,将控制权交到catch块中的代码,然后继续往...

JAVA 异常throwable exception error throws throw

1.如何理解Exception,Error和Throwable Throwable是Exception和Error的父类. Error表示错误,一般是系统级的错误! Exception一般是程...

What's the difference between “local” and “my” in Perl

There are two kinds of variable scopes in Perl: Global variables: They reside in the current pa...
  • ThinkHY
  • ThinkHY
  • 2012年08月30日 12:55
  • 1404
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Java - Difference between throw and throws in Exception handling
举报原因:
原因补充:

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