Maruthi Krishna has Published 952 Articles

What changes has been introduced in JDK7 related to Exception handling in Java?

Maruthi Krishna

Maruthi Krishna

Updated on 07-Aug-2019 09:17:57

52 Views

Since Java 7 try-with resources was introduced. In this we declare one or more resources in the try block and these will be closed automatically after the use. (at the end of the try block)The resources we declare in the try block should extend the java.lang.AutoCloseable class.ExampleFollowing program demonstrates the ... Read More

Are the instances of Exception checked or unchecked exceptions in java?

Maruthi Krishna

Maruthi Krishna

Updated on 07-Aug-2019 09:16:03

479 Views

An exception is an issue (run time error) occurred during the execution of a program. When an exception occurred the program gets terminated abruptly and, the code past the line that generated the exception never gets executed.There are two types of exceptions in Java.Unchecked Exception − An unchecked exception is ... Read More

Why we can't initialize static final variable in try/catch block in java?

Maruthi Krishna

Maruthi Krishna

Updated on 07-Aug-2019 09:11:01

762 Views

In Java you can declare three types of variables namely, instance variables, static variables and, local variables.Local variables − Variables defined inside methods, constructors or blocks are called local variables. The variable will be declared and initialized within the method and the variable will be destroyed when the method has ... Read More

Can a method throw java.lang.Exception without declaring it in java?

Maruthi Krishna

Maruthi Krishna

Updated on 06-Aug-2019 11:39:12

141 Views

No, for that matter to throw any exception explicitly you need to create an object of that exception and throw it using the throw keyword.Without creating an object you cannot throw an exception explicitly, you might create a scenario that causes the respective exception.ExampleFollowing Java program throws a NullPointerExceptionpublic class ... Read More

How can I add condition in custom exceptions in java?

Maruthi Krishna

Maruthi Krishna

Updated on 06-Aug-2019 11:37:29

575 Views

While reading values from the user in the constructor or any method you can validate those values using if condition.ExampleIn the following Java example we are defining two custom exception classes verifying name and age.import java.util.Scanner; class NotProperAgeException extends Throwable{    NotProperAgeException(String msg){       super(msg);    } } ... Read More

How to loop the program after an exception is thrown in java?

Maruthi Krishna

Maruthi Krishna

Updated on 06-Aug-2019 11:33:47

3K+ Views

Read the inputs and perform the required calculations within a method. Keep the code that causes exception in try block and catch all the possible exceptions in catch block(s). In each catch block display the respective message and call the method again.ExampleIn the following example we have an array with ... Read More

When do IllegalStateException and IllegalArgumentException get thrown? in java?

Maruthi Krishna

Maruthi Krishna

Updated on 06-Aug-2019 11:11:11

1K+ Views

IllegalStateException:This exception is thrown when you call a method at illegal or inappropriate time an IlleagalStateException is generated.For example, the remove() method of the ArrayList class removes the last element after calling the next() or previous methods.After removing the element at the current position you need to move to the ... Read More

Handling IllegalComponentStateException in java.

Maruthi Krishna

Maruthi Krishna

Updated on 06-Aug-2019 09:11:49

284 Views

It is the sub class of IllegalStateException, This indicates that AWT component is not in an appropriate state i.e. if you are working with components but, not using them properly leads to this exception. There are several scenarios where this exception occursExampleIn the following example we are trying to build ... Read More

Different scenarios that cause NoSuchElementException in Java.

Maruthi Krishna

Maruthi Krishna

Updated on 06-Aug-2019 08:47:46

217 Views

An exception is an issue (run time error) occurred during the execution of a program. When an exception occurs the program gets terminated abruptly and, the code past the line that generated the exception never gets executed. Each exception is represented by its respective class.NosuchElement ExceptionThis is a Runtime exception ... Read More

How to create an immutable set in Java?

Maruthi Krishna

Maruthi Krishna

Updated on 06-Aug-2019 08:34:07

986 Views

Whenever you need to create an object which cannot be changed after initialization you can define an immutable object. There are no specific rules to create immutable objects, the idea is to restrict the access of the fields of a class after initialization.A Set is an interface in collection framework, ... Read More

Advertisements