Question 5: Can we have try without catch block in java ?

Answer:
Yes, we can have try without catch block by using finally block. You can use try with finally. As you know finally block always executes even if you have exception or return statement in try block except in case of System.exit()..

Question 6: What is RunTime exception in java?

Answer:

RunTime exception is the exception which is thrown at run time. These exceptions occur due to programmatic errors and need to be corrected. Compiler is not aware of any such exception.

Example:

If you are working in java for quite some time, you might have got NullPointerException.

Let’s create a simple method which calculates if String’s length is odd or even.

1

2

3

4

5

6

7

8

9

publicstaticStringcheckOddEvenLenghtString(Stringstr){

if(str.length()%2==0){

return"Even";

}else{

return"Odd";

}

}

If you call above method with checkOddEvenLenghtString(null), then it will throw NullPointerException as we are calling length method on null object.

Question 7: What is checked exception or compile time exception?

Answer:
Checked exceptions are those exceptions which are checked at compile. If you do not handle them , you will get compilation error.It forces you to handle this exception in try-catch block.

Question 8: Can you put other statements between try,catch and finally block?

Answer:
No, You can not put any statements between try, catch and finally block.

Question 9: How do you create custom exception in java?

Answer:

You just need to extend Exception class to create custom exception.If yo want to create Unchecked Exception, you need to extend RuntimeException.

Let’s understand this with example.You have list of employee and if anyemployee’s age in list of employee is less than 18, then you need to throw invalidAgeException(Our custom exception).Example:
Create InvalidAgeException.java as below

1

2

3

4

5

6

7

8

9

10

11

packageorg.arpit.java2blog;

publicclassInvalidAgeExceptionextendsException{

InvalidAgeException(Stringmessage)

{

super(message);

}

}

Create POJO class called Country.java

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

packageorg.arpit.java2blog;

publicclassEmployee{

privateStringname;

privateintage;

Country(Stringname,intage){

this.name=name;

this.age=age;

}

publicStringtoString(){

returnname;

}

publicStringgetName(){

returnname;

}

publicvoidsetName(Stringname){

this.name=name;

}

publicintgetAge(){

returnage;

}

publicvoidsetName(intage){

this.age=age;

}

}

Let’s create EmployeeCheckMain.java. This class will have main method.

Explanation:
If you notice we have return statement in try block, so before returning from exceptionTest() method, finally block will be executed. When you have return statement in try block, JVM will take note of value of i and this value will be returned by exceptionTest method.

Question 13 : Predict output of below program:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

publicclassExceptionTest{

publicstaticvoidmain(String[]args){

System.out.println(exceptionTest());

}

publicstaticintexceptionTest()

{

inti=6;

try{

thrownewNullPointerException();

}

catch(Exceptione)

{

i=10;

returni;

}

finally

{

i=20;

System.out.println("In finally block");

}

}

}

Output:

1

2

3

4

Infinallyblock

10

Explanation:
Flow of the program will be as below.

Value of variable i will be set to 6.

NullPointerException will be thrown from try block.

Flow will go to catch block and value of i will be set to 10. JVM will make note of value of i and this will be returned by exceptionTest method.

Before returning from exceptionTest method, finally block will be executed and “In finally block” will be printed on console.

In the end, return value of exceptionTest method will be 10.

Question 14: Predict output of below program:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

publicclassExceptionTest{

publicstaticvoidmain(String[]args){

System.out.println(exceptionTest());

}

publicstaticintexceptionTest()

{

inti=6;

try{

thrownewNullPointerException();

}

catch(NullPointerExceptione)

{

i=10;

throwe;

}

finally

{

i=20;

System.out.println("In finally block");

returni;

}

}

}

Output:

1

2

3

4

Infinallyblock

20

Explanation:
Flow of the program will be as below.

Value of variable i will be set to 6.

NullPointerException will be thrown from try block.

Flow will go to catch block and value of i will be set to 10. We are throwing NullPointerException from catch block.

finally will get excuted and value of i will be set to 20.”In finally block” will be printed on console.

In the end, return value of exceptionTest method will be 20.

If you notice here, return statement from finally block actually suppressed the NullPointerException.

Question 15: Predict output of below program:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

publicclassExceptionTest{

publicstaticvoidmain(String[]args){

System.out.println(exceptionTest());

}

publicstaticintexceptionTest()

{

inti=6;

try{

i=50;

returni;

}

finally

{

i=20;

System.out.println("In finally block");

returni;

}

}

}

Output:

1

2

3

4

Infinallyblock

20

Explanation:
Flow of the program will be as below.

Value of variable i will be set to 6.

Value of variable i will be set to 10 and JVM will make note of return value of i as 10.

finally will get excuted and value of i will be set to 20.”In finally block” will be printed on console.

In the end, return value of exceptionTest method will be 20. It will override value returned by try block.