Exception Handling in Java

Java Exception Handling Program

- Advertisement -

In this tutorial, I will explain Java Exceptional Handling with real time program example. Actually, exceptions are like Errors, which occur
when a program is executed. There are two types of errors that will be there in Java exceptional handling. They are,
1. Compile-time Error
2. Run Time Error.

Finally, we can see a brief explanation of compile-time errors and run-time errors. Before that, we learn what is exception why we
use and see the Catch and Finally block in java tutorial.

What is Exception?
Actual Exception work interrupts the flow of the program. When the exception occurs program execution gets terminate from the flow.
Exceptions occur for many different types of reasons. That’s like,

1. When users enter invalid data.
2. Specified files are not on the disk.
3. Lost the Network Connection.

Why Use Exception
The exception is some kind of event in the application program. Once runtime receives the exception handler object, it tries to find the object exception. Java Exception handling used to handle only runtime errors and the compile-time errors are not handled in exception.

Exception Handling Example Java

import java.io.IOException;

public class MyException extends Exception {
private static final long filmVersionId = 87557;
private String errorcode = “Unknown_Exception”;
public MyException (String message, String errorCode) {
super(message);
this.errorCode = errorCode;
}
public String getErrorCode() {
return this.errorCode;
}
}

Java Exception Handling Keywords

These 5 keywords are mostly used in all java exception handling example. So here I’ll give a brief explanation about every keyword with one example.

1. try block Keyword

  •  The try block contains the code that might throw an exception.
  •  The try block must followed by either catch block or finally block.
  •  Otherwise, compile time error thrown for invalid sequence of method.
  • The try block must wrapped in inside a curly braces, even if it’s just single line of code.

Example

class TryCatchDemo {
public static void main (String [] args); {
int x = 25, y = 35, z = 45;

int result1, result2;
try {
result1 = x / (y-z); }
catch(ArithmeticException ae) {
System.out.println(“Divide by Zero:” + ae); }
result2 = x / (y+z);
System.out.println(“Result2 is:” + result2);
}
}

Output

- Advertisement -

Divide by Zero: java.lang.ArithmeticException: / by Zero
Result2 = 12

2. catch block Keyword

  • The catch block must be declared after try block.
  • It contains an error-handling code.
  • The catch block executed if an exception generates  try block.
  • The catch block takes one argument only which should be
  • sub-class of a variable.

Example

class TryCatchDemo {

public static void main (String [] args); {
int a = 30, b = 35, c = 60;

int result1, result2;
try {
result1 = a / (b-c); }

catch(ArithmeticException ae) {
System.out.println(“Divide by Zero:” + ae); }
result2 = a / (b+c);
System.out.println(“Result2 is:” + result2);
}
}

Output

 

Divide by Zero: java.lang.ArithmeticException: / by Zero
Result2 = 20

 

3. Finally Block Keyword

  • When access try block, the finally block get always executed.
  • Mostly used for execute some important object codes.
  • Finally block keyword must be followed by try block or catch block.
  • Finally block executes after try and catch block.

Example

class Allocate {
public static void main (String[] args) {
try {
long data [] = new long [30000];
}
Catch (Exception e) {
System.out.println(e);

}
finally {
System.out.println(“Finally block executed”);
}
}

}

Output

 

Finally block executed.
Exception in thread “main” java.lang.OutOfMemoryError: java heap
space
At Allocate.main(Allocate.java:5)

Main Example

import java.io.FileNotFoundException;
import java.io.IOException;

public class ExceptionHandling {
public static void main (String [] args) throws

FileNotFoundException, IOException {

try {
testException(-2);
testException(-15);
}catch(FileNotFoundException e) {

e.printStackTrace();
}catch(IOException e) {
e.printStackTrace();
}finally {
System.out.println(“Releasing Resources”);
}
testException(20);

}

Output

Releasing Resources

Leave A Reply

Your email address will not be published.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. AcceptRead More