Showing posts with label try. Show all posts
Showing posts with label try. Show all posts

Sunday, 9 December 2018

JavaScript: What is the behavior, if finally block returns a value

HelloWorld.js
function get_value(){
  try{
    return 10;
  }catch(e){
    return 20;
  }finally{
    return 30;
  }
}

console.log(get_value());

What is the output of above program?
If a finally block returns a value, then the value returned by finally block is the return value for entire try-catch-finally block.

In the above example, 30 is printed to console.

Previous                                                 Next                                                 Home

Monday, 24 February 2014

Suppressed Exceptions

Exceptions thrown by the try clause suppressed by the exception raised in the finally block.

Example
import java.io.*;

class MyResource{

 void getResource(){
  throw new RuntimeException("I am the RuntimeException");
 }

 public void close(){
  throw new NullPointerException("I am the NullPointer Exception");
 }
}

Class MyResource has two methods, getResource(), which throws RuntimeException , and close(), which throws NullPointerException.

import java.io.*;

class SuppressEx{

 static void callResource()throws Exception{

  MyResource obj = new MyResource();

  try{
   obj.getResource();
  }
  finally{

  }
 }

 public static void main(String args[])throws Exception{
  callResource();
 }
}


Output
Exception in thread "main" java.lang.RuntimeException: I am the RuntimeException
      at MyResource.getResource(MyResource.java:6)
      at SuppressEx.callResource(SuppressEx.java:8)
      at SuppressEx.main(SuppressEx.java:15)

When you tries to run the above program, “ RuntimeException” is thrown. It is as expected. Now will put the close method in the finally block and see the result.
  
import java.io.*;

class SuppressEx{

 static void callResource()throws Exception{

  MyResource obj = new MyResource();

  try{
   obj.getResource();
  }
  finally{
   obj.close();
  }
 }

 public static void main(String args[])throws Exception{
  callResource();
 }
}

Output
Exception in thread "main" java.lang.NullPointerException: I am the NullPointer Exception
   at MyResource.close(MyResource.java:10)
   at SuppressEx.callResource(SuppressEx.java:11)
   at SuppressEx.main(SuppressEx.java:15)

As you observe the output, the exception thrown in the finally clause suppressed the exception thrown in the try clause.




Autocloseable interface                                                 java7 support for suppressed exceptions                                                 Home

Saturday, 22 February 2014

try block

Syntax
try {
   //code
}
catch and finally blocks . . .
The first step to handle the exception is enclose the error prone code in the try block. A try blcok must have at least one catch or finally block. Other wise compiler throws an error.

Example
class ExceptionEx{
 public static void main(String args[]){
  try{
   System.out.println(" I don't have any catch or finally blocks associated with me");
  }
 }
}
   
Above program, doesn't have any catch or finally block associated with it. So when you tries to compile the program, compiler throws the below error.

ExceptionEx.java:4: error: 'try' without 'catch', 'finally' or resource declarations
try{
^
1 error

try nested inside a try
class ExceptionEx{
 public static void main(String args[]){
  try{
   System.out.println("I am enclosing another try inside me");
   try{
    System.out.println(10/0);
   }
   catch(ArrayIndexOutOfBoundsException e){
    System.out.println("inside catch block " + e);
   }
  }

  catch(Exception e){
   System.out.println("Outside catch block " + e);
  }
 }
}
 
Output
I am enclosing another try inside me
Outside catch block java.lang.ArithmeticException: / by zero

As you observe the program, try has try nested in it. “10/0” is called in inside try block, which has catch block associated with it. But the catch block for the inner try block handles ArrayIndexOutOfBoundsException only. So while executing, the catch correspond to the outer block is executed.


Handling Exceptions                                                 catch block                                                 Home

Handling the Exceptions

Java platform provides 3 blocks blocks to handle the exception that may occur in the program.

Those are :
1. try block
2. catch block
3. finally block

Whenever you felt that, there is a possibility of occurring an exception in the code, then place the entire code in the try block. Provide handlers to handle the exception.

After a method throws an exception, the run time system tries to find exception handlers to handle it.

If the Run time system finds a handler to handle the exception, then it executes the block of code in the handler, and Proceed execution from the catch block onwards. Other wise program simply terminates.

Example
class ExceptionEx{
 void print(){
  System.out.println(10/0);
 }

 void callPrint(){
  print();
 }

 public static void main(String args[]){
  ExceptionEx obj = new ExceptionEx();

  try{
   obj.callPrint();
  }

  catch(ArithmeticException e){
   System.out.println("Seems to be an Arithmetic Exception Occurred");
  }
  System.out.println("Finished Execution");
 }
}
 
Output
Seems to be Arithmetic Exception Occurred
Finished Execution

As you observe the output, there is an AirthmeticException occurred, then Java run time system attempts to find out a handler to handle this exception, So it executed the statements inside the catch block. Once the statements in the catch executed, then the statements after the catch statement starts executing.

Exception                                                 try block                                                 Home