NumberFormatException in Java

What is NumberFormatException in Java ? NumberFormatException is an unchecked/runtime exception which occurs when you attempt to convert a string to one of the numeric types, but that string does not have the appropriate format. NumberFormatException is thrown to indicate that the application has attempted to Convert String to int where the String is not well formatted for the conversion. For ex. String “12B” is converted to int. Let’s see NumberFormatException with an example. NumberFormatException with an Example:

Output:

In the above program, we have tried to convert String “45C” to…

Read More

Custom Exception in Java

In earlier tutorials, we have seen how to do Exception Handling in Java, throw an exception, how to use finally block, throws keyword etc.. In this tutorial we will learn about how  to create Custom Exception in Java. Inside the java.lang package, Java defines several built-in exception classes for ex. IOException, NullPointerException etc. Although Java’s built-in exceptions handle most common errors, you will probably want to create your own exception types to handle situations specific to your applications. To create your own Custom Exception in Java, you just define a subclass…

Read More

Catch Multiple Exceptions in Java

In this tutorial, we will learn about how to Catch Multiple Exceptions in Java. In Java, we handle the Exceptions using try-catch block. A method might throw multiple exceptions depending on the code in it. Prior to Java 7, to handle this situation we need to specify more than 1 catch block where each catch block is handling different type of exception. When an exception is thrown, each catch statement is inspected in order, and the first one whose type matches that of the exception is executed. After one catch statement executes, the others…

Read More

Multiple Catch Blocks in Java

In this tutorial, we will learn about Multiple Catch Blocks in Java. In Java, we handle the Exceptions using try-catch block. A method might throw multiple exceptions depending on the code in it. To handle this situation, we need to specify more than 1 catch block where each catch block is handling different type of exception. When an exception is thrown, each catch statement is inspected in order, and the first one whose type matches that of the exception is executed. After one catch statement executes, the others are bypassed,…

Read More

Finally block in Java

What is Finally block in Java ? When an Exception occurs in a program in Java, the programs execution gets terminated abruptly. Depending upon how the method is coded, it is even possible for an exception to cause the method to return prematurely. This could be a problem in some methods. For example, if a method has a file handling code and closes the file upon exit, then you will not want the code that closes the file to be bypassed by the exception-handling mechanism. The finally block in Java is designed to…

Read More

Try Catch in Java

Why we need try catch block in Java ? When an Exception occurs in a program in Java, the programs execution gets terminated abruptly. We don’t want the program should get terminated due to the exception and want proper exception message to be displayed in case of any exception and continue the execution of program. So, how to avoid this abrupt termination of the program in Java ? Java provides try catch block to avoid the abrupt termination of the program. Let’s see the usage of try catch block with…

Read More

Difference between Exception and Error

In this tutorial, you will learn about Difference between Exception and Error. Before we know the Difference between Exception and Error, lets learn what is an Exception and Error first. What is an Exception ? An Exception is an abnormal condition that arises in a code sequence at run time. In other words, an exception is a run-time error. A Java Exception is an object that describes an exceptional condition that has occurred in a piece of code. When an exceptional condition arises, an object representing that exception is created and thrown…

Read More

Throw keyword in Java

If we don’t write proper code, don’t handle the exception properly then Java program throws an Exception. If exception occurs, we can handle it using try-catch-finally block. Many times we want to throw an exception explicitly. For ex. The system generated exceptions will not understand to the user and we want to give proper message to the user by explicitly throwing the exception/custom exception with proper message. So if we want to throw an exception, then we have Throw keyword in Java. Throw keyword in Java is used to explicitly throw…

Read More

Throws keyword in java

Throws keyword in Java is used to declare that a method may throw one or more exceptions. The caller of this method has to catch these exceptions. The catching of these exceptions is optional if the exceptions are of type unchecked exceptions or error. If the exceptions are not caught, a compile time error will be thrown. throws keyword is used to handle Checked Exceptions. The throws clause lists the types of exceptions that a method might throw. When to use throws keyword ? When you don’t want to handle the…

Read More

Checked and Unchecked Exceptions in Java

The Exception class in Java is further divided into 2 types namely Checked and Unchecked Exceptions. What are Checked Exceptions ? Checked exceptions are the exceptions which are known at compile time. These are the exceptions that are checked at compile time. They are also called as compile time exceptions. Checked and unchecked exceptions need to handle using try-catch block or declare the exception using throws clause. If the exceptions are not handled properly, the program will throw compile time error. All sub classes of java.lang.Exception except sub classes of RunTimeException…

Read More