Learn With Reshu is here to help you all to get new Technological ideas and knowledge. I will try to create blogs on all technical topics and new tricks and tips. please do like share and subscribe Learn with Reshu to encourage me more for creating good help to others.
Thanks,
Best Regards
#learnwithReshu
Advanced Java Programming Advanced Java Programming :- Introduction to advance java As most of us already know that if we want to make normal applications it can be easily built using core Java concepts. But, when it we need to develop web applications, advanced Java fundamentals, like JSP, Servlets, JDBC etc. needed, so to add capabilities and features of the application advance java is essential for developers. Through the motive of this blog is to explain about Advanced Java, I will be giving you a complete insight into the fundamental concepts of Advance Java. Figure - 1.2 If you want to see complete video on this please have a look the video below. Learn with Resh u Advanced Java Programming Course ...
Get link
Facebook
X
Pinterest
Email
Other Apps
Exception Handling in Java
Get link
Facebook
X
Pinterest
Email
Other Apps
-
Exception Handling in Java
What is an Exception?
An exception is an unwanted or unexpected event, which occurs during the execution of a program i.e at run time, that disrupts the normal flow of the program’s instructions.
Error vs Exception
Error:An Error indicates serious problem that a reasonable application should not try to catch. Exception:Exception indicates conditions that a reasonable application might try to catch.
Exception Hierarchy
All exception and errors types are sub classes of class Throwable, which is base class of hierarchy.One branch is headed by Exception. This class is used for exceptional conditions that user programs should catch. NullPointerException is an example of such an exception.Another branch,Error are used by the Java run-time system(JVM) to indicate errors having to do with the run-time environment itself(JRE). StackOverflowError is an example of such an error.
How JVM handle an Exception?
Default Exception Handling : Whenever inside a method, if an exception has occurred, the method creates an Object known as Exception Object and hands it off to the run-time system(JVM). The exception object contains name and description of the exception, and current state of the program where exception has occurred. Creating the Exception Object and handling it to the run-time system is called throwing an Exception.There might be the list of the methods that had been called to get to the method where exception was occurred. This ordered list of the methods is called Call Stack.Now the following procedure will happen.
The run-time system searches the call stack to find the method that contains block of code that can handle the occurred exception. The block of the code is called Exception handler.
The run-time system starts searching from the method in which exception occurred, proceeds through call stack in the reverse order in which methods were called.
If it finds appropriate handler then it passes the occurred exception to it. Appropriate handler means the type of the exception object thrown matches the type of the exception object it can handle.
If run-time system searches all the methods on call stack and couldn’t have found the appropriate handler then run-time system handover the Exception Object to default exception handler , which is part of run-time system. This handler prints the exception information in the following format and terminates program abnormally.
Exception in thread "xxx" Name of Exception : Description
... ...... .. // Call Stack
Example :
// Java program to demonstrate how exception is thrown.
classExecptionExample{
publicstaticvoidmain(String args[]){
String str = null;
System.out.println(str.length());
}
}
Output :
Exception in thread "main" java.lang.NullPointerException
at ExecptionExample.main(File.java:8)
How to use try-catch clause
try {
// block of code to monitor for errors
// the code you think can raise an exception
}
catch (ExceptionType1 exOb) {
// exception handler for ExceptionType1
}
catch (ExceptionType2 exOb) {
// exception handler for ExceptionType2
}
// optional
finally {
// block of code to be executed after try block ends
}
How Programmer handles an exception?
Customized Exception Handling :
Java exception handling is managed via five keywords: try, catch, throw, throws, and finally. Briefly, here is how they work. Program statements that you think can raise exceptions are contained within a try block. If an exception occurs within the try block, it is thrown. Your code can catch this exception (using catch block) and handle it in some rational manner. System-generated exceptions are automatically thrown by the Java run-time system. To manually throw an exception, use the keyword throw. Any exception that is thrown out of a method must be specified as such by a throwsclause. Any code that absolutely must be executed after a try block completes is put in a finally block.
package com.exception.examples;
public class Unchecked_Demo {
public static void main(String args[]) {
try {
int num[] = {1, 2, 3, 4};
System.out.println(num[5]);
}catch(ArrayIndexOutOfBoundsException e) {
System.out.println("num[] has exceed limits");
e.printStackTrace();
}
}
}
Points to be Noted:
In a method, there can be more than one statements that might throw exception, So put all these statements within its own try block and provide separate exception handler within own catch block for each of them.
If an exception occurs within the try block, that exception is handled by the exception handler associated with it. To associate exception handler, we must put catch block after it. There can be more than one exception handlers. Each catch block is a exception handler that handles the exception of the type indicated by its argument. The argument, ExceptionType declares the type of the exception that it can handle and must be the name of the class that inherits from Throwable class.
For each try block there can be zero or more catch blocks, but only one finally block.
The finally block is optional.It always gets executed whether an exception occurred in try block or not . If exception occurs, then it will be executed after try and catch blocks. And if exception does not occur then it will be executed after the try block. The finally block in java is used to put important codes such as clean up code e.g. closing the file or closing the connection.
Learn Complete OOPS Concepts in one Project In this Page i will give a example Code for covering all oops concepts in only one single project. Class Model Code Example Explanation:- Suppose there is a Organization which has many departments and many Employees Some Employees are Part of Some Departments So In this Code example What i have created is A Organization Class Which is having Many Employees so One Employee Class and Employee is a part of Departments so We have Created one Departments Class Now Organization, Department and Employee has some task to be done so for that i have created Interfaces for each one where i H...
The “Spring Framework” is a large project. Under the Spring Framework label are actually about 20 different Spring projects. Spring MVC, Spring Security, Spring Integration, etc. So, it would be easy to get confused about what “Spring Core” is. The official Spring documentation, contains this image: “Spring Core” is typically used to refer to the functionality of the Core container. Beans, Core, Context, and SpEL. While there are about 30 Spring Framework projects, they all depend on this core container. Its this core functionality that has been around since the beginning of the Spring Framework. The core container, which has the Spring Context, manages dependency injection for us via Inversion of Control is used by all other Spring projects. Spring Boot Application In Hindi Cheers, Learn with Reshu
Running and Building Gradle with Different JDKs - Sip of Java If you are using Gradle as the build tool for your projects and want to work with the latest JDK releases or early-access builds, you might think you are stuck until Gradle supports those versions of the JDK, which might take a few months. However, that’s not the case, and we will explore in this article how to run Gradle with one JDK version while building and testing with a different JDK version. Managing Multiple JDKs When working with multiple JDKs, using a tool like SDKMan or Jenv (for macOS) is highly recommended. These tools enable you to easily manage and switch between your local JDKs. This article assumes that you have multiple JDKs installed on your system: one for running Gradle, and the other for executing the tasks in the build. Be sure that the JDK installed to run Gradle is supported by Gradle. As of the time of this article, the latest Gradle version is 7.6 which supports JDK 19. Configuring the Toolch...
Comments
Post a Comment
Please do not comment any spam link in the comment box