Exceptions And Errors

Filter Course


Exceptions And Errors

Published by: sadikshya

Published date: 17 Jun 2021

Exceptions And Errors photo

Exceptions And Errors

Both Errors and Exceptions are the subclasses of java. lang. Exceptions And Errors are described below:

Exceptions:

An exception (or exceptional event) is a problem that arises during the execution of a program. When an Exception occurs the normal flow of the program is disrupted and the program/Application terminates abnormally, which is not recommended, therefore, these exceptions are to be handled.

An exception can occur for many different reasons. The following are some scenarios where an exception occurs.

  • A user has entered invalid data.
  • A file that needs to be opened cannot be found.
  • A network connection has been lost in the middle of communications or the JVM has run out of memory.

Errors

Errors are mostly caused by the environment in which the program is running. Errors occur at runtime and not known to the compiler. All exceptions occur at runtime but checked exceptions are known to the compiler while unchecked is not. They are defined in java.lang.Error package.

Exceptions vs Errors

Error: An Error indicates a serious problem that a reasonable application should not try to catch.
Exception: Exception indicates conditions that a reasonable application might try to catch.

Exceptions Hierarchy

The possible exceptions in a Java program are organized in a hierarchy of exception classes. The Throwable class, which is an immediate subclass of Object, is at the root of the exception hierarchy. Throwable has two immediate subclasses: Exception and Error.

Types of Exceptions

Checked exceptions − A checked exception is an exception that occurs at the compile time, these are also called as compile-time exceptions. These exceptions cannot simply be ignored at the time of compilation, the programmer should take care of (handle) these exceptions.

Unchecked exceptions − An unchecked exception is an exception that occurs at the time of execution. These are also called as Runtime Exceptions. These include programming bugs, such as logic errors or improper use of an API. Runtime exceptions are ignored at the time of compilation.

Catching Exceptions

A method catches an exception using a combination of the try and catches keywords. A try/catch block is placed around the code that might generate an exception. Code within a try/catch block is referred to as protected code, and the syntax for using try/catch looks like the following −
Syntax,

try {
// Protected code
} catch (ExceptionName e1) {
// Catch block
}

Examples

// File Name : ExcepTest.java
import java.io.*;

public class ExcepTest {

public static void main(String args[]) {
try {
int a[] = new int[2];
System.out.println(“Access element three :” + a[3]);
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println(“Exception thrown :” + e);
}
System.out.println(“Out of the block”);
}
}

Multiple Catching

A try block can be followed by one or more catch blocks. Each catch block must contain a different exception handler. So, if you have to perform different tasks at the occurrence of different exceptions, use java multi-catch block.

Syntax

try {
// Protected code
} catch (ExceptionType1 e1) {
// Catch block
} catch (ExceptionType2 e2) {
// Catch block
} catch (ExceptionType3 e3) {
// Catch block
}

Example

try {
file = new FileInputStream(fileName);
x = (byte) file.read();
} catch (IOException i) {
i.printStackTrace();
return -1;
} catch (FileNotFoundException f) // Not valid! {
f.printStackTrace();
return -1;
}

Finally Clause

The finally block follows a try block or a catch block. A finally block of code always executes, irrespective of occurrence of an Exception.
Using a final block allows you to run any cleanup-type statements that you want to execute, no matter what happens in the protected code.

Syntax

try {
// Protected code
} catch (ExceptionType1 e1) {
// Catch block
} catch (ExceptionType2 e2) {
// Catch block
} catch (ExceptionType3 e3) {
// Catch block
}finally {
// The finally block always executes.
}

Example

public class ExcepTest {

public static void main(String args[]) {
int a[] = new int[2];
try {
System.out.println(“Access element three :” + a[3]);
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println(“Exception thrown :” + e);
}finally {
a[0] = 6;
System.out.println(“First element value: ” + a[0]);
System.out.println(“The finally statement is executed”);
}
}
}

Throwing An Exception

You can throw an exception in Java by using the throw keyword. This action will cause an exception to be raised and will require the calling method to catch the exception or throw the exception to the next level in the call stack.

Throw And Throws

the throw keyword is used to throw an exception explicitly from any method or static block while throws keyword is used in a method declaration, denoted which exception can possibly be thrown by this method.
the throw is followed by an instance while throws are followed by exception class name.
the throw is used within the method whereas throws are used with the method signature.
You cannot throw multiple exceptions using throw but you can declare multiple exceptions using throws one of which may or may not throw by the method.
eg. public void method() throws IOException, SQLException{}
throw statement will create an exception object but throws statement will not create an exception object.
throws keyword is used to make an exception to be propagated explicitly to parent method(caller method) but when we use throw keyword and throw the exception, then it is not necessary that exception will be propagated. It may propagate if it is the unchecked exception and it may not propagate if it is checked exception.
throws keyword not needed to be used for an unchecked exception to make it propagate to parent method(caller method) because they can be propagated by default but throws keyword should and must be used to handle exception in parent method if it is checked exception because checked exception can not be propagated by default.
Using throw keyword you can also break a switch statement or a loop without using break keyword which cannot be performed using throws.

Analyzing Track Trace Elements

Trace elemental analysis can tell you. Determining trace and ultra-trace level chemical impurities, either with or without a full elemental survey analysis can address this question. Trace concentrations are typically categorized as mass fractions from 1 part per million (ppm) to 100 ppm.

Java’s Built-in Exceptions

Java – Built-in Exceptions. Java defines several exception classes inside the standard package java.lang. The most general of these exceptions are subclasses of the standard type RuntimeException. … Java defines several other types of exceptions that relate to its various class libraries.

 

Creating an Exception Subclasses

Although Java’s built-in exceptions handle most common errors, Java’s exception handling mechanism is not limited to these errors. In fact, part of the power of Java’s approach to exceptions is its ability to handle exceptions that you create which correspond to errors in your own code. Creating an exception is easy. Just define a subclass of Exception (which is, of course, a subclass of Throwable). Your subclasses don’t need to actually implement anything—it is their existence in the type system that allows you to use them as exceptions.