Directory Image
This website uses cookies to improve user experience. By using our website you consent to all cookies in accordance with our Privacy Policy.

Exceptions in Java

Author: Infocampus Bangalore
by Infocampus Bangalore
Posted: Nov 18, 2018

What is Exception in Java?

An exception is an unwanted or sudden event that happens throughout the execution of a program, i.e. at runtime that disrupts the normal flow of the program’s directions.

Types of Java Exceptions

There are primarily two types of exceptions: checked and unchecked. Here, an error is taken into account because of the unchecked exception. Consistent with Oracle, there are three forms of exceptions:

  1. Checked Exception
  2. Unchecked Exception
  3. Error

Difference between Checked and Unchecked Exceptions

1. Checked Exception

The categories that directly inherit Throwable type except RuntimeException and Error are referred to as checked exceptions, e.g. IOException, SQLException, etc. Checked exceptions are checked at compile-time.

2. Unchecked Exception

The categories that inherit Runtime Exception are referred to as unchecked exceptions, e.g. Arithmetic Exception, Null Pointer Exception, ArrayIndexOutOfBoundsException, etc. unchecked exceptions aren't checked at compile-time. However, they're tested at runtime.

3. Error

Error is lost e.g. OutOfMemoryError, VirtualMachineError, Asserti"qtiperar" title ="|may be an" id="tip_46">could be a mechanism to handle runtime errors like ClassNotFoundException, IOException, SQLException, Remote Exception, etc.

Exception Handling in Java

The Exception Handling in Java is one among the powerful mechanism to handle the runtime errors, so the normal flow of the appliance is often maintained.

In this page, we are going to study Java exceptions, its kind and also the distinction between checked and unchecked exceptions.

The advantage of Exception Handling

The core advantage of exception handling is to keep up the traditional flow of the appliance. AN exception commonly disrupts the conventional flow of the tool that's why we tend to use exception handling.

Suppose there are ten statements in your program and there happens an exception at statement five, Java Courses in Bangalore the remainder of the code won't be dead, i.e. statement 6 to ten won't be executed. If we tend to perform exception handling, the rest of the report is executed. That's why we tend to use exception handling in Java.

How JVM handle an Exception?

Default Exception Handling: Whenever within a technique, if an exception has occurred, the strategy creates AN Object referred to as Exception Object and hands it off to the run-time system. The exception object contains a name and outline of the exception, and current state of the program wherever exception has occurred. Making the Exception Object and handling it to the run-time system is named throwing an Exception. There can be the list of technique that had been referred to as to urge to the method wherever exception occurred. This ordered list of the strategies is named decision Stack. Currently, the subsequent procedure can happen.

  • The run-time system searches the decision stack to search out the plan that contains a block of code that may handle the occurred exception. The intersection of the code is named Exception handler.
  • The run-time system starts looking from the strategy during which exception occurred issue through decision stack within the reverse order during which procedures were referred to as.
  • If it finds an acceptable handler, then it passes the occurred exception to that. Adequate driver suggests that the sort of the exception object thrown matches the type of the exception object it will handle.
  • If run-time system searches all the strategies on decision stack and couldn’t have found the acceptable handler, then run-time system relinquishing the Exception Object to default exception handler, that is a Java Training Institute in Bangalore Marathahalli element of the run-time system. This handler prints the exception info within the following format and terminates program abnormally.

Java Exception Keywords

Five keywords are utilized in handling exceptions in Java.

Try

The "try" keyword is employed to specify a block wherever we should always place exception code. Either catch should follow the try block or finally. It means we won't use strive block alone.

Catch

The "catch" block is employed to handle the exception. It should be preceded by attempt block which suggests we won't use catch block alone. They are often followed by finally block later.

Finally

The "finally" block is employed to execute the critical code of the program. It's performed whether or not an exception is handled or not.

Throw

The "throw" keyword is employed to throw an exception.

Throws

The "throws" keyword is employed to declare exceptions. It does not throw an exception. It specifies that there might occur an exception within the technique. It's forever used with technique signature.

Java Exception Hierarchy

As expressed earlier, once an exception is raised an exception object is obtaining created. Java Exceptions are stratified, and inheritance is employed to categorize different kinds of exceptions. Throwable Java Training Center Bangalore is that the parent category of Java Exceptions Hierarchy and its two kid objects – Error and Exception. Exceptions are more divided into checked exceptions and runtime exception.

  1. Errors: Errors are exceptional situations that are out of the scope of application, and it’s impossible to anticipate and pass through them, for instance, hardware failure, JVM crash or out of memory error. That’s why we have a separate hierarchy of mistakes and that we mustn't try and handle these things. A number of the common Errors are OutOfMemoryError and Stack Overflow Error.
  2. Checked Exceptions: Checked Exceptions are exceptional situations that we will anticipate in a very program and take a look at to endure it, as an example FileNotFound Exception. We should always catch this exception and supply helpful message to the user and log it properly for debugging purpose. An exception is that the parent category of all Checked Exceptions and if we tend to are throwing a checked exception, we tend to should catch it within the same technique or we've to propagate it to the caller exploitation throws keyword.
  3. Runtime Exception: Runtime Exceptions are caused by bad programming, as an example attempting to retrieve a part from the Array. We should always check the length of array initial before trying to recover the piece. Otherwise, it'd throw Array Index OutOfBoundException at runtime. Runtime Exception is that the parent category of all runtime exceptions. If we have a tendency to be throwing any runtime exception in a very technical, it’s not needed to specify them within the technique signature throws clause. Runtime exceptions are often avoided with higher programming.

Author:

Infocampus is the best organization for Java Training in Bangalore. Head to free demo classes, get prepared by 8yrs skilled mentors and moreover get hands-on live projects.

Infocampus is a standout amongst alternative Best Core Java Training in Bangalore.

Infocampus offers 100% Job help on Java coaching.

Contact: 9740557058

Visit: http://infocampus.co.in/java-training-bangalore.html

About the Author

Searching for center propelled java/j2EE training in Bangalore Marathahalli, a short time later your first plausibility to be an Infocampus.

Rate this Article
Leave a Comment
Author Thumbnail
I Agree:
Comment 
Pictures
Author: Infocampus Bangalore

Infocampus Bangalore

Member since: Dec 28, 2015
Published articles: 81

Related Articles