Sitemap
CodeX

Everything connected with Tech & Code. Follow to join our 1M+ monthly readers

Member-only story

Java exception Practices

--

Press enter or click to view image in full size

Introduction

The exception is an unwanted or unexpected event, which occurs during the execution of an application. Exceptions and be handled by programmers or software engineers.

When preparing a new Application, we need to put our hands on exceptions will be happened like database exciption , stream exciption , and other common ways will happen exceptions, and in this article, I will share these practices to see how we can make exceptions, and you can tell me if you have any other best way to handle it.

let's go to see it.

Close your resource using finally keyword

for example if we using inputstream it in java we need to put it inside the block of try/catch , and after finishing the business we need to close the resource. but the common problem here, the developer closes the resource in the try block, and it is a very wrong and bad habit. Because usually the exception thrown before this line, means the close will not execute.

So to avoid this scenario we can use finally block, to close our resources.

look at the following code snippet to see finally use.

public void readResource(){

InputStream resource = null;

try{

resource = new InputStream(){

@Override…

--

--

Abdalrhmanalkraien
Abdalrhmanalkraien

Written by Abdalrhmanalkraien

I am a Java Developer and DevOps Engineer. I used the latest Technology for build completed development or Deployment

CodeX
CodeX

Published in CodeX

Everything connected with Tech & Code. Follow to join our 1M+ monthly readers