Once you have purchased domain and hosting for your website the next thing is to design it the way you like. While WordPress CMS makes it quite easy to design your site independently, it is always best to have knowledge about languages like Java to enjoy a better control. In this guide we will mention a step by step tutorial on one of the major aspects of Java, that is called Try Catch method.

Just like with any other language, java is also susceptible to errors. Some of the common Java errors that interrupt code execution include wrong input, coding mistakes, and other unpredictable issues. Try catch in java detects those errors during execution and stops the code from executing further. With try-catch in Java, you can look for errors in the live code during execution (Try Statement) and define the code block that will be executed in case the error occurs (catch statement). 

What are exceptions in java?

What are exceptions in java?

During an error instance, Java stops running and displays an error message which is technically called “throwing an exception”.

In short, Exception refers to an event happening during execution and interrupts the regular flow of instructions 

Java Try Block: Java Try Block refers to the code that throws an exception. A detailed explanation can be found later in this tutorial.

Syntax of Java Try Block

try{    

// exception-throwing code

}

Java Catch Block: Java Catch Block refers to the block that mentions the name reference of the exception class. It is mentioned in detail elsewhere in this tutorial.

Syntax of Java Catch Block

catch(Exception_class_Name ref){}

Try-Catch Block Syntax is used in a combined form as below:

try{    

//code that may throw an exception    

}catch(Exception_class_Name ref){}    

Java Finally Block: Java Finally block executes key codes like the stream, closing connection, etc. It is effectively executed regardless of whether or not the exception is handled. It comes after try/catch block

Syntax of Java Finally Block

try{    

//code that may throw an exception    

}finally{}    

What does try block refer to in Try Catch in Java? (with syntax)

In Try catch, in Java, the try block enables you to clearly define a code block that may be throwing an exception and should be checked for errors during live execution. The try block stops executing if an exception happens at a particular try block statement. So, avoid placing a code in the Try block that will not throw an exception. Java Try Block needs to be followed by catch or finally block.

Syntax 

Try{

// mention the code that might throw an exception

} catch {Exception_class_Name ref){}

Example

public class Main {

  public static void main(String[] args) {

int[] myNumbers = {1, 2, 3};

    System.out.println(myNumbers[10]);

  }

}

The role of Try in Catch Try method?

When you work with Try Catch in Java you also need to explain how to deal with the exception. The Catch block enables you to handle the exception by defining the exception type within the parameter. An exception you declare must be either a parent exception or generated type of exception. The ideal recommendation is to declare the generated exception type. The catch block must always follow the try block. With the Catch statement, you can specify a code block that should be executed if the try block gets an error.>>

Java comes with a default exception handling mechanism where the normal code is sent to try block while the code for exception handling goes to catch block

What are multiple catch blocks in java? (with example)

While working with Try catch in Java a try block can be followed by multiple catch blocks provided that each of them contains a unique exception handler. Multi-catch block is used for executing diverse tasks in different exception cases.

Only one exception occurs at a time and for that only one catch block gets executed

Multiple catch blocks should be in an arranged order starting from most specific and the most generic. For instance, an Arithmetic exception should be ordered before catch for exception

 Here’s an example of multiple catch blocks while working with Try Catch in Java

public class MultipleCatchBlock1 {

public static void main(String[] args) {

try{  

int a[]=new int[8];  

a[8]=50/0;  

}  

catch(ArithmeticException e)

{

System.out.println(“This is an Arithmetic Exception”);

}  

catch(ArrayIndexOutOfBoundsException e)

{

System.out.println(“ArrayIndexOutOfBounds Exception occurs”);

}  

catch(Exception e)

{

System.out.println(“Parent Exception occurs”);

}   

System.out.println(“Here is rest of the code”);  

}

}

How is exception related to Try Catch?

As the error happens in any method the method produces an object and delegates it to the runtime system for handling. This object includes error information like program state at the time of error instance and the specific type of the error. This process of generating an object and handing it off to the runtime system is called throwing an exception. 

Once the method throws an exception the runtime system starts searching for possible elements for handling the exception. For that, it calls an ordered method list- called Call stack to spot the specific method where the error happened.

It then searches for a specific method in the call stack containing the block code capable of exception handling. Starting from the error method the search proceeds in the reverse order through the stack. It then looks for the appropriate handles i.e. the exception object type matches the type that a handler can tackle. Upon finding the appropriate handler the exception is then passed to the handler. 

This chosen exception handler is called to catch the exception. If the runtime system fails to find the appropriate handler it terminates and programs are also terminated as a consequence.

As one catch statement bypass, the others get bypassed and the code after the try-catch block gets executed.

Conclusion

Whether you want to build a business website or need to start your blog, the knowledge about technical aspects always comes in handy. One such programming language is Java. In today’s guide we discussed an important part of Java that is called Try-Catch method. Try catch in java works in tandem for testing a block for errors during execution (through Try statement) and executing a code block if an error occurs (through Code statement). In this tutorial, we read about the Try-Catch statement and the related details like multiple catch blocks and exceptions. Try Catch in Java allows Java applications to run smoothly.

RELATED ARTICLES

Leave a Reply

Your email address will not be published.