What is Exception Handaling | how to throw exception in java try catch finally block in java
source: internet



This Tutorial will cover how to throw exceptions in java try to catch the finally block in java and how to create custom exceptions in java with all examples. let's start with tutorials

  • What is an exception?

The exception is an unwanted or unexpected event, which occurs during the execution of the program i.e. during operation, which disrupts the normal flow of system instructions.

If the verified exceptions are not dealt with, the catch block must be indicated. You can implement the handling of one or more exception types in the catch () block. 

The catch statement includes the explanation of the type of exception you are trying to catch. As you can see in the following snippet of code, the catch clause takes the exception as a parameter. 

  • What is Exception Handling

If an exception occurs in the protected code, the catch block is a block followed by an exception check. The block contains crucial statements that are executed when the exception occurs or not. If no statement is present, the block will only be executed if the exception has occurred in the try block or not, for example when closing the connection to the stream. 

If the block is not executed, the JVM the try-catch block exits, and the code executed in System. exit () is executed to control the range of the block so that the thread that executing the try-catch code can pause or exit the block to prevent, close or open resource leaks. When the block is executed under try control, the block exits regardless of whether the exception is thrown or not.

If in the try block an exception occurs in a particular statement, the rest of the code block is not executed. If an exception is not handled by providing a catch block, JVM has an exception handler by default that handles the exception. If the exception occurs outside the try block, the JVM executes the catch block instead of the try-catch block. 

It is recommended not to keep any code inside the try block that does not trigger an exception. Syntax of Java try/catch.  try _ throw exception: try to throw exception catch (exception, class name) Ref Syntax of try block: try throw exception Java catch block is used to handle the exception by mentioning the type of exception as a parameter.

Normally, if there is no exception in the try block, the final block is executed after the try block. In this case, regardless of whether an exception occurs, the try block is not executed. However, if the exception occurs, the catch block is executed after the attempt. 

The control flow depends on whether the exception occurs in the try block or not. If such exceptions are not recorded and handled, the block is abruptly terminated. The abrupt termination will cause the exception thrown by the final block to be lost and prevent possible recovery methods to address the specific problem.

The transfer of control associated with the exception prevents the execution of the expression or statement that occurred at the time of blocking when the exception was thrown. The thread that executes the try-catch code is interrupted or terminated, the terminating block is not executed, and the application as a whole is resumed.

In the whitelist method try block, you are working on opening a PrintWriter. It is. In the .NET try block there is a WriteList < WriteList > method to open the PrintWriter. 

If a method is about to return, it is in the try/catch block and the normal execution flow of the uncaptured exception block is executed after the return of the method. This is a complicated problem in programs where the try block can be terminated in one of three ways. In this example, the return value of the methods in the return statement of the try-catch block is ignored. 

This is a hard-to-detect bug, and we should avoid using the return block. The block in which the return statement is overwritten by one of the try blocks or return blocks, the return method is printed on the console. This is a return statement in the try or catches block that can be overridden.

As we can see, in the case of a successful trial block, the return statement of the final block overrides the return statement of the catch block. Finally, the result returned by the final block is returned by the method and printed to the console. For example, if the value of the parameter is counted, the JVM executes the last block and prints it. 

We can use the last block to execute clean code, close connections, close files, run free threads, or it can be executed after an exception. If the method has a return statement, the JVM executes the final block and passes control over the method. 

  • Try-catch block

try {
  //  Block of code to try
}
catch(Exception e) {
  //  Block of code to handle errors
}


If we throw an exception into the try block, the catch block handles the exception. In the case of backup servers, if an exception is triggered, the code is executed in the last block. 

If the data type of the thrown exception matches ExceptionType.1, it is caught. If it returns nothing, no value is returned. The badNullException parameter in the catch clause indicates the thrown exception and shares the method of the thrown exception. 

When an exception is thrown into the try block of an instance of the method Divide, the program flow of the call to the method Calldivide () itself is interrupted. The execution of the program continues and the catch block is called on the stack to catch the exception. If an exception is thrown before the method call statement is executed, the try/catch block will do nothing and will be ignored. 

The last block is executed when the instantiation of FileInputStream triggers FileNotFoundException, or the processing of the file content triggers any other exception. If your code has a return statement in the try/catch block, the code in the final clause is executed before the return method. The code in this clause is only executed if an exception is thrown into the final block. 

  • Simple try-catch block program

public class Main {
  public static void main(String[ ] args) {
    try {
      int[] myNumbers = {1, 2, 3};
      System.out.println(myNumbers[10]);
    } catch (Exception e) {
      System.out.println("Something went wrong.");
    }
  }
}

Since Java 7 it is a good practice to place cleanup code within the final block. The final block in Java helps provide an exception handling mechanism for cleanup. 

Note that eventually block in Java will not run if an exception is thrown. The code within the last block is executed when the try-catch block is completed. It is executed immediately after the throw of the exception block and executed after completion of the try-catch block. 

  • Simple Try-catch and finally block program

public class Main {
  public static void main(String[] args) {
    try {
      int[] myNumbers = {1, 2, 3};
      System.out.println(myNumbers[10]);
    } catch (Exception e) {
      System.out.println("Something went wrong.");
    } finally {
      System.out.println("The 'try catch' is finished.");
    }
  }
}

Java Try/catch block helps write application code that triggers an exception at runtime and gives us a chance to recover from the exception and execute alternative application logic to handle the exception reported by the user. The try block contains application code that should work under normal conditions. Optional catch block following the try block handle checked exceptions and threw try block possible unchecked exceptions. 

Post a Comment

Previous Post Next Post