Difference Between throw throws and throwable In Java

Published on Aug 05,2019 26.8K Views

Difference Between throw throws and throwable In Java

edureka.co

A major problem in Java occurs when we are working on Exception Handling. A common confusion arises amongst throw, throws, and throwable in Java. To clear all your doubts the following pointers will be covered in this “throw, throws and throwable“article:

Moving on with this article on Difference between throw, throws, and throwable in java.

Throw: The throw keyword in Java is used to explicitly throw an exception from a method or any block of code. We can throw either checked or unchecked exception. The throw keyword is mainly used to throw custom exceptions.

Syntax:


throw Instance

//Example:

throw new ArithmeticException("/ by zero");

But this exception i.e, Instance must be of type Throwable or a subclass of Throwable. For example Exception is a sub-class of Throwable and user defined exceptions typically extend Exception class. Unlike C++, data types such as int, char, floats or non-throwable classes cannot be used as exceptions.

Example:

public class GFG {
public static void main(String[] args)
{
// Use of unchecked Exception
try {
// double x=3/0;
throw new ArithmeticException();
}
catch (ArithmeticException e) 
{
e.printStackTrace();
}
}
}

Output:
java.lang.ArithmeticException: / by zero
atUseofThrow.main(UseofThrow.java:8)

Moving on with this article on Difference between throw, throws and throwable in java.

Throws In Java :

Throw is also a keyword in java which is used in the method signature to indicate that this method may throw mentioned exceptions. The caller to such methods must handle the mentioned exceptions either using try-catch blocks or using throws keyword. Below is the syntax for using throws keyword.

return_type method_name(parameter_list) throws exception_list

{ //some statements
}
throws:
import java.io.IOException;  
public class UseOfThrowAndThrows { 
public static void main(String[] args) 
throws IOException
{
}
}


Output:
Exception in thread “main” java.io.IOException
at UseOfThrowAndThrows.main(UseOfThrow.java:7)

Moving on with this article on Difference between throw, throws and throwable in java.

Java.lang.Throwable Class

Throwable is a super class for all types of errors and exceptions in java. This class is a member of java.lang package. Only instances of this class or it’s sub classes are thrown by the java virtual machine or by the throw statement. The only argument of catch block must be of this type or it’s sub classes. If you want to create your own customized exceptions, then your class must extend this class.

Class Declaration

Following is the declaration for java.lang.Throwable class:

Example:

class MyException extends Throwable
{
//Customized Exception class
} 
class ThrowAndThrowsExample
{
void method() throws MyException
{
MyException e = new MyException(); 
throw e;
}
}

Thus we have come to an end of this article on ‘Difference between throw, throws and throwable in java’. If you wish to learn more, check out the Java Training by Edureka, a trusted online learning company. Edureka’s Java J2EE and SOA training and certification course is designed to train you for both core and advanced Java concepts along with various Java frameworks like Hibernate & Spring.

Got a question for us? Please mention it in the comments section of this blog and we will get back to you as soon as possible.

Upcoming Batches For Java Certification Training Course
Course NameDateDetails
Java Certification Training Course

Class Starts on 11th May,2024

11th May

SAT&SUN (Weekend Batch)
View Details
Java Certification Training Course

Class Starts on 1st June,2024

1st June

SAT&SUN (Weekend Batch)
View Details
BROWSE COURSES
REGISTER FOR FREE WEBINAR UiPath Selectors Tutorial