Java/J2EE and SOA (325 Blogs) Become a Certified Professional
AWS Global Infrastructure

Programming & Frameworks

Topics Covered
  • C Programming and Data Structures (41 Blogs)
  • Comprehensive Java Course (2 Blogs)
  • Java/J2EE and SOA (324 Blogs)
  • Spring Framework (8 Blogs)
SEE MORE

MI-new-launch

myMock Interview Service for Real Tech Jobs

myMock-widget-banner-bg

Difference Between throw throws and throwable In Java

Published on Aug 05,2019 872 Views

MI-new-launch

myMock Interview Service for Real Tech Jobs

myMock-mobile-banner-bg

myMock Interview Service for Real Tech Jobs

  • Mock interview in latest tech domains i.e JAVA, AI, DEVOPS,etc
  • Get interviewed by leading tech experts
  • Real time assessment report and video recording

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.

eeption handling - throw throws and throwableThrow: 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:

  • public class Throwable
  • extends Object
  • implements Serializable

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.

Comments
0 Comments

Browse Categories

webinar REGISTER FOR FREE WEBINAR
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.