throw keyword in java

Solutions on MaxInterview for throw keyword in java by the best coders in the world

showing results for - "throw keyword in java"
Caoimhe
28 Nov 2019
1throw new java.lang.Error("this is very bad");
2throw new java.lang.RuntimeException("this is not quite as bad");
Swann
27 May 2018
1public static void main(String[] args) {
2	Scanner kb = new Scanner(System.in);
3    System.out.println("Enter a number");
4    try {
5    	double nb1 = kb.nextDouble();
6    	if(nb1<0)
7        	throw new ArithmeticException();
8        else System.out.println( "result : " + Math.sqrt(nb1) );
9    } catch (ArithmeticException e) {
10        System.out.println("You tried an impossible sqrt");
11    }
12}
Jana
26 Jan 2018
1Generally JVM throws the exception and
2we handle the exceptions by 
3using try catch block. But there are
4situations where we have to throw 
5userdefined exceptions or runtime exceptions.
6  In such case we use throw keyword 
7to throw exception explicitly.
8
9  Syntax : throw throwableInstance;
10
11
Ilse
14 Sep 2017
1Generally JVM throws the exception and we handle the exceptions by 
2using try catch block. But there are situations where we have to throw 
3userdefined exceptions or runtime exceptions. In such case we use throw keyword 
4to throw exception explicitly.
5
6  Syntax : throw throwableInstance;
7
8Throwable instance must be of type throwable or any of its subclasses.
9After the throw statement execution stops and subsequent statements are not 
10executed. Once exception object is thrown JVM checks is there any catch 
11block to handle the exception. If not then the next catch statement till it 
12finds the appropriate handler. If appropriate handler is not found, 
13then default exception handler halts the program and prints the description 
14and location of exception. In general we use throw keyword for throwing 
15userdefined or customized exception.
similar questions
queries leading to this page
throw keyword in java