在Java中,throw与throws有什么区别?他们各自用在什么地方?

如题所述

第1个回答  2020-01-03
throw
用来抛出异常throws
用来标识可能抛出的异常
public
class
Person
{
public
void
display()
throws
Exception{
System.out.println("hello
everyone");
}
}
public
class
Test
{
public
static
void
main(String[]args){
Person
person
=
new
Person();
//在Person中的display方法抛出了异常,在调用display方法是需要捕捉
try
{
person.display();
}
catch
(Exception
e)
{
e.printStackTrace();
}
}
}
//在调用方法时,如果有用到用throws标识了异常的方法,程序最终被运行之前必须要被捕捉
相似回答