Found 34462 Articles for Programming

Does finally always execute in Java?

Sai Subramanyam
Updated on 30-Jul-2019 22:30:20

308 Views

The finally block follows a try block or a catch block. A finally block of code always executes, irrespective of occurrence of an Exception.

Is there a case when finally block does not execute in Java?

Lakshmi Srinivas
Updated on 10-Aug-2023 13:52:43

827 Views

Questions related to Java exception handling are most frequent during interviews for many companies and even in exams. One such question that an interviewer might ask is whether there is a case when the finally block does not execute in Java. We will try to find the answer to this question in the simplest way possible. In general, the finally block is designed to execute regardless of whether an exception is thrown or handled in the try-catch blocks. Is there a case when finally block does not execute in Java? Before moving to the question, it is necessary to discuss ... Read More

What is the difference between throw and throws keywords in Java?

Sharon Christine
Updated on 20-Feb-2020 06:39:20

300 Views

The throw keyword is used to raise an exception explicitly.Examplepublic class Test {    public static void main(String[] args) {       throw new NullPointerException();    } }Exception in thread "main" java.lang.NullPointerException at a6.dateAndTime.Test.main(Test.java:5)The throws keywords in Java used to postpone the handling of a checked exception.public class Test {    public static void main(String[] args)throws NullPointerException {       throw new NullPointerException();    } }

What is the catch block in Java?

Swarali Sree
Updated on 25-Feb-2020 10:42:48

278 Views

A catch statement involves declaring the type of exception you are trying to catch. If an exception occurs in the try block, the catch block (or blocks) that follows the try is checked. If the type of exception that occurred is listed in a catch block, the exception is passed to the catch block much as an argument is passed into a method parameter.Exampleimport java.io.File; import java.io.FileInputStream; public class Test {    public static void main(String args[]) {       System.out.println("Hello");       try {          File file = new File("my_file");       ... Read More

What is the try block in Java?

Samual Sam
Updated on 25-Feb-2020 10:44:22

420 Views

A try/catch block is placed around the code that might generate an exception. Code within a try/catch block is referred to as protected code, and the syntax for using try/catch looks like the following –Syntaxtry {    // Protected code } catch (ExceptionName e1) {    // Catch block }The code which is prone to exceptions is placed in the try block. When an exception raised inside a try block, instead of terminating the program JVM stores the exception details in the exception stack and proceeds to the catch block.

What are number format exceptions in Java?

Sai Subramanyam
Updated on 30-Jul-2019 22:30:20

146 Views

This Exception happens once you associated convert a string variable in an incorrect format to a whole number (numeric format) that's not compatible with one another. Example public class Test { public static void main(String[] args) { int data = Integer.parseInt("hello"); } } Output Exception in thread "main" java.lang.NumberFormatException: For input string: "hello" at java.lang.NumberFormatException.forInputString(Unknown Source) at java.lang.Integer.parseInt(Unknown Source) at java.lang.Integer.parseInt(Unknown Source) at a6.dateAndTime.Test.main(Test.java:5)

What is the base class for errors and exceptions in Java?

Monica Mona
Updated on 25-Feb-2020 12:25:55

2K+ Views

All exception classes are subtypes of the java.lang.Exception class. The Exception class is a subclass of the Throwable class. Other than the Exception class there is another subclass called Error which is derived from the Throwable class.

What is the difference between checked and unchecked exceptions in Java?

karthikeya Boyini
Updated on 25-Feb-2020 10:45:28

735 Views

A checked exception is an exception that occurs at the compile time, these are also called as compile time exceptions. These exceptions cannot simply be ignored at the time of compilation; the programmer should take care of (handle) these exceptions.ExampleIf you use FileReader class in your program to read data from a file, if the file specified in its constructor doesn't exist, then a FileNotFoundException occurs, and the compiler prompts the programmer to handle the exception.import java.io.File; import java.io.FileReader; public class FilenotFound_Demo {    public static void main(String args[]) {       File file = new File("E://file.txt");   ... Read More

What are unchecked exceptions in Java?

Sharon Christine
Updated on 16-Jun-2020 12:45:47

2K+ Views

An unchecked exception is the one which occurs at the time of execution. These are also called as Runtime Exceptions. These include programming bugs, such as logic errors or improper use of an API. Runtime exceptions are ignored at the time of compilation. If you have declared an array of size 5 in your program, and trying to call the 6th element of the array then an ArrayIndexOutOfBoundsExceptionexception occurs.ExampleLive Demopublic class Unchecked_Demo {    public static void main(String args[]) {       int num[] = {1, 2, 3, 4};       System.out.println(num[5]);    } }OutputException in thread "main" java.lang.ArrayIndexOutOfBoundsException: ... Read More

What are checked exceptions in Java?

Swarali Sree
Updated on 30-Jul-2019 22:30:20

753 Views

A checked exception is an exception that occurs at the time of compilation, these are also called as compile time exceptions. These exceptions cannot simply be ignored at the time of compilation; the programmer should take care of (handle) these exceptions. if you use FileReader class in your program to read data from a file, if the file specified in its constructor doesn't exist, then a FileNotFoundException occurs, and the compiler prompts the programmer to handle the exception. Example import java.io.File; import java.io.FileReader; public class FilenotFound_Demo { public static void main(String args[]) { ... Read More

Advertisements