0

What is the difference between throw and throws?

throw and throws are the keyword

11th Apr 2017, 10:06 AM
Bineeta Rawat
Bineeta Rawat - avatar
4 odpowiedzi
11th Apr 2017, 10:11 AM
देवेंद्र महाजन (Devender)
देवेंद्र महाजन (Devender) - avatar
+ 1
public class A { public void testA throws Exception ( ){ } public void testB ( ) { throw new Exception ( ) ; } public static void main ( String [ ] args ) { A a = new A ( ) ; // a.testA ( ) ; // compile error. try { a.testA ( ) ; // the method must be inside the try scope. catch ( Exception e ) { e.printStackTrace ( ) ; } } }
11th Apr 2017, 10:29 AM
K.C. Leung
K.C. Leung - avatar
+ 1
you use throw when you want to throw an execption as statement, you use throws when you mark a method that may throw an execption for warning user to handle that exception witg try and catch
11th Apr 2017, 11:42 AM
η=sqrt(μ/ε)
η=sqrt(μ/ε) - avatar
0
Thx. The answers helped me too!
4th May 2017, 8:19 PM
Lisa Türk
Lisa Türk - avatar