Maruthi Krishna has Published 951 Articles

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

Maruthi Krishna

Maruthi Krishna

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

493 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

805 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

160 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

598 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

294 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

233 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

1K+ 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

How to append data to a file in Java?

Maruthi Krishna

Maruthi Krishna

Updated on 06-Aug-2019 08:31:13

603 Views

In most scenarios if you try to write contents to a file, using the classes of the java.io package, the file will be overwritten i.e. data existing in the file is erased and the new data is added to it.But, in certain scenarios like logging exceptions into a file (without ... Read More

Advertisements