try catch block in Java vs Javascript

In both Java and JavaScript, the try-catch block is used to handle exceptions that may occur in the code. The basic structure of a try-catch block in both languages is similar: a block of code is put in a “try” block, and any exceptions that are thrown in that block are caught and handled in a separate “catch” block.

And finally you have a finally block which will be executed irrespective of whether there was an exception or not.

However, there are a few key differences between the way try-catch blocks work in the two languages:

Exception vs Error:

In Java, exceptions are objects that are instances of classes that extend the “Throwable” class. The catch block takes a parameter that specifies the type of exception that it can handle. For example, the catch block might catch a “FileNotFoundException” or a “SQLException”.

try {
    // code that might throw an exception
} catch (FileNotFoundException e) {
    // handle the exception
} catch (SQLException e) {
    // handle the exception
}

In JavaScript, exceptions are represented as error objects that are instances of the “Error” class . The catch block takes an argument that is the error object.

try {
    // code that might throw an exception
} catch (err) {
    console.log(err);
    // handle the exception
}

Throw statement

In Java, the “throw” statement is used to throw an exception object that is an instance of a class that extends the “Throwable” class. The exception object is typically created by calling the constructor of one of the built-in exception classes, such as “IllegalArgumentException” or “IOException”.

if (someCondition) {
    throw new IllegalArgumentException("some message");
}

In Javascript you can throw an “Error” object or even a string or a number:

if (someCondition) {
    throw new Error("some message");
}

if (someCondition) {
    throw "some message";
}

Advertisements


Checked Exceptions vs Unchecked Errors:

Java’s exception is checked, meaning that the compiler checks that the exception has been handled or declared to be thrown by the method.

If you throw an exception in Java you either :

  • catch it in catch block or
  • Add”throws” statement at the end of method declaration so that the exception gets propagated. This need to be handled again by the method which invokes the method which threw the exception.
  • throw it again using “throw” statement (in this case you have to add throws statement as well )

public void test() throws Exception{

   try{
   .....
   //logic here
    }catch(Exception e){
             throw new Exception("Throwing the exception again");
   }

}

JavaScript errors are unchecked and it’s not mandatory to handle the errors.

If a code throws error and you don’t include try catch block surrounding that code ,Javascript won’t complain whereas Java will force you to add the statement or add throws statement.

throws keyword:

Java allows you to propagate an exception using “throws” keyword if you don’t want to deal with it in the current method.

Javascript has no such provision of “throws” keyword.

In JavaScript, errors propagate up the call stack automatically

When an error is thrown, the JavaScript engine will look for the nearest error-handling code, such as a try-catch block to handle the error.

If no error-handling code is found, the error will continue to propagate up the call stack until it is handled or until it reaches the top level of the stack, at which point the script will terminate and the error will be logged to the console or an error-logging service if available.

It’s worth noting that in JavaScript, you can throw any type of value (not just errors) and it will propagate in the same way. This means that any value that is thrown, including strings, numbers, or objects, will be treated as an error and will propagate up the call stack.

That’s it!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s