Exception Handling Tutorials in Java

0
147
Spread the love
  • 59
  • 85
  • 26
  • 89
  •  
  •  
  •  
  • 14
  •  
  •  
    273
    Shares

Exception Handling in Java
Exception Handling in Java

This post will cover exception in java tutorial,runtime exception in java,exception handling in java with examples,exception handling in java best practices,exception in java interview questions,and give us the guidelines that may help us to get the most of java’s Exception handling mechanism. We will be able to write a more powerful, effective java code by taking most of this post.

Exception Handling in Java:

Below guide will help us to understand exception handling in java with examples.Exception handling is a programming technique that can detect things which can go wrong and respond by creating a new Object.(In the official terminology the code is said to be throwing an exception).

The new object an instance of the  exception class is passed.Some program is intended to catch the exception.When the exception is caught,the program executes some recovery code,buries the exception and moves back to normal statement.

The point where the problem occurs,we may not know what to do with it.At that point we may not continue further,we stop and either resolve the problem in the current context or if we do not sufficient information to take a decision,we hand it over to the higher context.

In the next few paragraphs we will learn exception handling in java best practices.

With Exception handling in place,reading writing and debugging code becomes much cleaner as Exceptions clean up error handling code.Instead of checking for a particular error and dealing with it in multiple places,we can handle the Exception in only one place.Whenever an exception is thrown from a code, we need to handle the exception. Handling the exception can be done in two ways:

  1. Using try catch block
  2. Using throws keywords.
READ  Most Important Strengths Of Java Language

Using try-catch block we can handle the exceptions instantly.Any error prone code or sensitive operation should be put inside try catch block. In this post we will look into details of try catch block.

By using throws keyword we pass the exception to the caller method to handle or to pass again to the Java’s default exception handling. Throws keyword is written in details here.

This is the basic exception handling in java with examples.This is very important topic for exception in java interview questions.

what if we do not handle an exception:

This is very important part of exception in java tutorial.If we do not handle the exception,it propagate to the higher context. If there is no exception handling mechanism,again it propagate to the higher context and so on. Finally it reaches to the default exception handler of JVM. It prints the stack trace and terminate the program. This is not exception handling in java best practices.


public class myException{
public static void main(String []args){
myFunction(2,2);
myFunction(2,0);
myFunction(4,2);
}
private static void myFunction(int x,int y){
int z=0;
z=(int)x/y;
System.out.println("result of division"+z);
}
}

The output of the code:
$javac myException.java
$java -Xmx128M -Xms16M myException
result of division1
Exception in thread “main” java.lang.ArithmeticException: / by zero
at myException.myFunction(myException.java:10)
at myException.main(myException.java:5)

At second case the exception has occurred.As there is no exception handling mechanism , the exception is propagated to the calling function main.

Main also does not have any exception handling,as a result the exception is handed over to default handler. Default handler provides the stack trace and terminates the program.so if we handle the exception the execution will not stop in between. This concept may be asked in Exception in java interview questions .

READ  Java String Illustrations - Comparing, Getting, and Extracting

try catch block:

The basic building block for exception in java tutorial is the try catch block.A try statement is an assembly of a try catch clause.Every catch statement is like a little mini method with its own parameter list.The parameter list always has an exception type name and then a parameter.

The scope of try is ordinary and the block is preceded by try keyword .try is the place where exception might occur and catch is the place where we catch those exception and handle it.It is always better to write try block for each risky methods.

A method thus catches an Exception using combination of try and catch block.The code placed inside of the try and catch is referred as protected code. This approach make code lengthy and not read friendly.Second approach is to put all risky code inside a single try block. This will be readable(as per exception handling in java best practices).

It is a developer’s choice.However try-catch tries to handle the Exception in the current context.Below the guidelines for exception in java tutorial or exception handling in java with examples. The exception handling in java best practices are-

  • A try block must immediately followed by one or more catch statements and a finally block..
  • There may be one or more lines of code that will generate the Exception.
  • When an exception occurred or thrown inside of the try block, the control immediately  jump to the catch block and the first matching catch block is executed.The remaining statements in the try block are skipped.
  • When an exception is caught and handled by catch block,the execution control does not jump back to the point where the exception occurred i.e the try block.
  • If no matching catch block is found,the exception is un-handled and will propagate to the calling method to default handler and the program will terminate.
  • If no exception is thrown from try block the catch block never get executed, the control goes to finally block(if available) or next line of code to execute.
READ  Relevance Of DukeScript For Java Builders

Each catch clause is similar to a method that takes one and only one argument of a particular type.Sometimes we never use the identifier as the type of the Exception gives us enough information to deal with the Exception,but the identifier must be there.

The handler catch block must appear directly after the try block.If the exception is thrown,the Exception handling mechanism goes hunting for the first handler with an argument that matches the type of the Exception.

then it enters into the catch block and the Exception is considered handled. If no matching catch block is found, it still hunts for the matching Exception,Only the matching clause executes.

Don't miss out!
Subscribe To Newsletter

Receive top technical news, lesson ideas, travel tips and more!

Invalid email address
Give it a try. You can unsubscribe at any time.

Spread the love
  • 59
  • 85
  • 26
  • 89
  •  
  •  
  •  
  • 14
  •  
  •  
    273
    Shares

LEAVE A REPLY

Please enter your comment!
Please enter your name here