Programming Articles - Page 2495 of 3366

Is there any way to skip finally block even if some exception occurs in exception block using java?

Maruthi Krishna
Updated on 03-Jul-2020 08:07:01

5K+ 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.Try, catch, finally blocksTo handle exceptions Java provides a try-catch block mechanism.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.Syntaxtry {    // Protected code } catch (ExceptionName e1) {    // Catch block }When an exception raised inside a try block, instead of terminating the program JVM stores ... Read More

Can We handle the RuntimeException in java?

Maruthi Krishna
Updated on 03-Jul-2020 08:08:02

152 Views

A Run time exception or an unchecked exception is the one which occurs at the time of execution. These include programming bugs, such as logic errors or improper use of an API. Runtime exceptions are ignored at the time of compilation.IndexOutOfBoundsException, ArithmeticException, ArrayStoreException and, ClassCastException are the examples of run time exceptions.ExampleIn following Java program, we have an array with size 5 and we are trying to access the 6th element, this generates ArrayIndexOutOfBoundsException.public class ExceptionExample {    public static void main(String[] args) {       //Creating an integer array with size 5       int inpuArray[] = ... Read More

What is InputMisMatchException in Java how do we handle it?

Maruthi Krishna
Updated on 07-Aug-2019 10:47:22

4K+ Views

From Java 1.5 Scanner class was introduced. This class accepts a File, InputStream, Path and, String objects, reads all the primitive data types and Strings (from the given source) token by token using regular expressions.To read various datatypes from the source using the nextXXX() methods provided by this class namely, nextInt(), nextShort(), nextFloat(), nextLong(), nextBigDecimal(), nextBigInteger(), nextLong(), nextShort(), nextDouble(), nextByte(), nextFloat(), next().Whenever you take inputs from the user using a Scanner class. If the inputs passed doesn’t match the method or an InputMisMatchException is thrown. For example, if you reading an integer data using the nextInt() method and the value ... Read More

Importance of isDaemon() method in Java?

raja
Updated on 23-Nov-2023 10:29:10

572 Views

A daemon thread is a low-priority thread in java which runs in the background and mostly created by JVM for performing background tasks like Garbage Collection(GC). If no user thread is running then JVM can exit even if daemon threads are running. The only purpose of a daemon thread is to serve user threads. The isDaemon() method can be used to determine the thread is daemon thread or not. Syntax Public boolean isDaemon() Example class SampleThread implements Runnable { public void run() { if(Thread.currentThread().isDaemon()) System.out.println(Thread.currentThread().getName()+" is daemon thread"); ... Read More

Can we define a package after the import statement in Java?

Vivek Verma
Updated on 28-Aug-2025 16:13:17

662 Views

No, we cannot define a package after the import statement in Java. The compiler will throw an error if we try to insert a package after the import statement. A package is a group of similar types of classes, interfaces, and sub-packages. To create a class inside a package, declare the package name in the first statement in our program. Important! If you use the package statement in an online compiler, you may still get an error even if it is defined correctly. This is because many online compilers do not support custom package structures. Example In the following example, ... Read More

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

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

108 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 try-with-resources in Java.import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; public class FileCopying {    public static void main(String[] args) {       try(FileInputStream inS = new FileInputStream(new File("E:\Test\sample.txt"));       FileOutputStream outS = new FileOutputStream(new File("E:\Test\duplicate.txt"))){          byte[] buffer = new byte[1024];     ... Read More

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

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

608 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 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.Checked Exception − A checked exception is an exception that occurs at ... Read More

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

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

1K+ 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 completed.Class (static) variables − Class variables are variables declared within a class, outside any method, with the static keyword.Static methods in try-blockIn the same way, static variables belong to the class and can be accessed anywhere within the class, which contradicts with the definition of the local variable. Therefore, declaring ... Read More

How can we decide that custom exception should be checked or unchecked in java?

Maruthi Krishna
Updated on 03-Jul-2020 08:02:42

6K+ 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.User defined exceptionsYou can create your own exceptions in Java and they are known as user defined exceptions or custom exceptions.To create a user defined exception extend one of the above mentioned classes. To display the message override the toString() method or, call the superclass parameterized constructor by passing the message in String format.MyException(String msg){    super(msg); } Or, public String toString(){    return ... Read More

float() in Python

Pradeep Elance
Updated on 07-Aug-2019 08:55:11

232 Views

Float method is part of python standard library which converts a number or a string containing numbers to a float data type. There are following rules when a string is considered to be valid for converting it to a float.The string must have only numbers in it.Mathematical operators between the numbers can also be used.The string can represent NaN or infThe white spaces at the beginning and end are always ignored.ExampleThe below program indicates how different values are returned when float function is applied.n = 89 print(type(n)) f = float(n) print(type(f)) print("input", 7, " with float function becomes ", float(7)) ... Read More

Advertisements