Author - StudySection Post Views - 86 views
Errors and Exceptions

Errors and Exceptions Introduction

Error

Errors occurs when a program abnormally terminates.

Exception

Exceptions occur at compile time or runtime when there is an error in the program.

Java.lang.Error is the superclass of any Error.

There are two types of exceptions-

  • Checked Exceptions
  • Unchecked Exceptions

Checked Exceptions

Exceptions that occur at compile time of the program is called
checked exceptions.

Examples

Arithmeticexception – This type of exception occurs when we try to divide a number by zero in the denominator.

Arrayindexoutofboundexception – This type of exception occurs when we try to access an item in an array by an index that is not present in the array.

Classcastexception – This type of exception occurs when a class is not a subclass of another class.
E.g.- Trying to cast Integer class to String class. Here String is not a subclass of the Integer class.

Indexoutofboundexception – This type of exception occurs when we try to access indexes that do not exist in collections.

Nullpointerexception – This type of exception occurs when we try to access an object that has null value.

Numberformatexception – This type of exception occurs when we try to convert an invalid string number to a string.
E.g.- String s=”123ss”; Integer number=(Integer)s;

Unchecked Exceptions

Exceptions that occur at the runtime of the program are called Unchecked exceptions. All Errors are types of unchecked exceptions.

Examples

Outofmemoryerror – Out of memory Error occurs when heap storage exceeds the maximum heap storage assigned to JVM. All objects are stored in heap memory.

Stackoverflowerror – Stackoverflow error occurs when stack storage exceeds the maximum stack storage specified to JVM. All primitive variables and object references are stored in stack memory.

Handling Exceptions

TRY-CATCH BLOCK

We can handle exceptions with the help of a try-catch block. By putting code that will throw exceptions inside try block we can catch it in the catch block.
E.g.-

try {
//code that can throw exceptions
} catch (Exception ex) {
// Exception handler
}

We can also specify multiple catch blocks by appending one more catch block to the previous catch block.

try {
//code that can throw exceptions
} catch (ExceptionType1 ex1) {
// exception handler for ExceptionType1
} catch (ExceptionType2 ex2) {
// Exception handler for ExceptionType2
}

If the exception is not caught inside the first catch block then it will try to catch exceptions inside the next catch block until exceptions are caught or not inside all catch blocks.

FINALLY BLOCK

A finally block will always be executed regardless of whether an exception occurs or not.
Finally block is used to perform cleanup operations.
E.g.- We want to close database connections whether an exception occurs or not.

try {
//code that can throw exceptions
} catch (ExceptionType1 ex1) {
// exception handler for ExceptionType1
} finally {
// finally block always executes
}

TRY-WITH-RESOURCES

In try-with-resources each resource is automatically closed at the end of the statement. Any object that implements java.lang.AutoClosable interface can be used with try-with resources.
E.g.-

try (BufferedReader br = new BufferedReader(new FileReader(path))){
return br.readLine();
}

Here we don’t have to use the finally block to close connections at the end. While try-with-resources can have both the catch and finally block just like normal try-catch-finally.

Throwing an exception

We can throw exceptions somewhere to catch exceptions later with the help of a throw statement

E.g.-
throw new EmptyRequestException();

Here we can define our own custom class exception that is the base class of java.lang.throwable class or can throw inbuilt Exceptions like null pointer exceptions.

Advantages of Exceptions

  1. Separate Error-prone code from normal code and error handling.
  2. Use of exceptions to report errors.

jQuery allows the user to create powerful and dynamic webpages that run without any hiccup. With StudySection, you have the liberty to choose among beginner or expert level jQuery certification exams to help you excel in this career field.

Leave a Reply

Your email address will not be published. Required fields are marked *

fiteesports.com rivierarw.com cratosroyalbet betwoon grandpashabet grandpashabet giriş deneme bonusu veren siteler casino siteleri