Home Java Docker
Home     Java

Java Topic

What is Java
History of Java
Freature of Java
Difference Between Java & C++
Java Environment Set Up
Java Hello World Program & its Internal Process
Java Hello World Program
JDK, JRE and JVM
Java Variables
Java Data Types & Unicode System
Java Operators
Java Keywords
Java Control Statements
Java if else
Java switch
Java for loop
Java While loop
Java Do While loop
Java break
Java continue
Java Oops Concept
Java Object & Class
Java Method
Java Constructor
Java Static Keyword
Java this Keyword
Java Inheritance
Java Hybrid Inheritance
Aggregation(HAS-A)
Java Polymorphism
Java method overloading
Java method overriding
Java Runtime polymorphism
Java Dynamic Binding
Super keyword
Final keyword
Difference Between method overloading and method overriding
Java Abstraction
Java Interface
Abstract class vs Interface
Java Encapsulation
Java Package
Java Access Modifiers
covariant return type
Instance initializer block
Java instanceof operator
Object Cloning in Java
Wrapper classes in Java
Java Strictfp Keyword
Recursion in Java
Java Command Line Arguments
Difference between object and class
Java String
Java String Class
Java Immutable String
Java Immutable Class
String Buffer
String Builder
String Buffer vs String
String Builder vs String Buffer
String Tokenizer in Java
Java Array
Java Exceptions Handling
Java Try-Catch block
Java Multiply Catch Block
Java Finally Block
Java Throws Keyword
Java Throw Keyword
Java Exception Propagation
Java Throw vs Throws
Final vs Finally vs Finalize
Exception Handling With Method Overridding
Java Multithreading
Lifecycle and States of a Thread in Java
How to create a thread in Java
Thread Scheduler in Java
Sleeping a thread in Java
Calling run() method
Joining a thread in Java
Naming a thread in Java
Thread Priority
Daemon Thread
Thread Pool
Thread Group
Shutdown hook
Multitasking vs Multithreading
Garbage Collection
RunTime Class
Java Synchronization
Synchronized block in Java
Static Synchronization in Java
Deadlock in Java
Inter Thread Communication in Java
Interrupting Thread in Java
Reentrant Monitor in Java
Java Applet
Animation in Applet
EventHandling in Applet
Display image in Applet
Displaying Graphics in Applet
Parameter in Applet
Java 8 Features
Java Lambda Expressions
Method References
Functional Interfaces
Java 8 Stream
Base64 Encode Decode
Default Method
for Each() Method
Collectors class
String Joiner Class
Optional Class
JavaScript Nashron
Parallel Array Sort
Type Interface
Parameter Reflection
Type and Repeating Annotations
JDBC Improvements

Difference Between throw and throws in Java

Java throw

    • The throws keyword in Java serves as a declaration of potential exceptions that might occur within a program. By utilizing this keyword, programmers can be alerted to the possibility of exceptions and take necessary steps to handle them, ensuring the smooth execution of the program.
    • On the other hand, the throw keyword in Java enables the throwing of both checked and unchecked exceptions. It empowers programmers to create and raise custom exceptions when specific conditions are met.
    • Moreover, the throw keyword grants the flexibility to define our own conditions and explicitly throw exceptions. For instance, when performing a division operation between two numbers, an ArithmeticException can be thrown by establishing the appropriate condition and utilizing the throw keyword.






Java throws


  • The throws keyword in Java is used to declare an exception. It alerts the programmer to the possibility of an exception occurring. As a result, it is preferable for the programmer to provide exception handling code so that the program's normal flow can be maintained.

  • Exception Handling is used primarily to handle checked exceptions. If an unchecked exception occurs, such as a NullPointerException, it is the fault of the programmer for not checking the code before it is used.

  • The throws keyword provides information about the exception to the method's caller.





Example of throw keyword


public class Main {	
    void fun(){
    	try {
            System.out.println("fun() method ");
            throw new NullPointerException();
		}
    	catch (NullPointerException  e){
            throw e;  
		}
  }
  public static void main(String args[]){
      try{
          Main main=new Main();
          main.fun();
      }
      catch(NullPointerException e){
          System.out.println("Caught in the main method");
      }
  }
}




Output:

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
	Unhandled exception type InterruptedException

	at HELLO/DockerTpoint.Main.main(Main.java:5) 

The program's execution comes to a halt right after the throw statement is executed. It then checks the closest try block to see if there is a catch statement that matches the type of exception. If a match is found, control is transferred to that catch statement. If no match is found, it moves on to the next enclosing try block and repeats the process. This continues until a matching catch is found or, if none is found, the default exception handler stops the program.




Example of throws Keyword


Example of throws keyword in java
package DockerTpoint;
public class Main {	
    public static void main(String args[])throws InterruptedException{    	  		  
        Thread.sleep(1000);
        System.out.println("Hello DockerTpoint");
   } 
}

Output:

Hello DockerTpoint 

We handled the InterruptedException in the preceding program by using the throws keyword, and the output is Hello DockerTpoint.






Difference Between throw and throws in Java

Difference Between throw and throw in Java are mention below


Basis of Differences throw throws
Usage Used to explicitly throw an exception Used to declare that a method may throw an exception
Syntax

throw <exception>

'methodSignature throws <exception1>, <exception2>, ...'

Placement Inside a method body In the method declaration
Exception Type Can throw any subclass of Throwable Declares the checked exceptions that can be thrown
Handling Immediate handling required in catch blocks. Exceptions can be handled by caller methods.
Single/Multiple Throws a single exception at a time Can declare multiple exceptions using comma-separated list
Checked/Unchecked Can throw both checked and unchecked exceptions Mainly used for checked exceptions


Key Difference throw and throws in Java

  • The throw keyword is used to declare checked exceptions that a method may throw, while throws is used in the method declaration to explicitly throw an exception. throw is used within a method body, while throws is used in the method signature.
  • The throw keyword is typically used for immediate handling of exceptions within the method itself, while the throws keyword allows the method to pass the responsibility of handling the exception to its caller method.
  • Additionally, the throw keyword can throw both checked and unchecked exceptions, while the throws keyword is mainly used for declaring and propagating checked exceptions.

Final vs Finally vs Finalize Next »
« Perv Next »


Post your comment





Read Next Topic
Java Tutorial - Topic
Java Exceptions Handling
Java Try-Catch block
Java Multiply Catch Block
Java Finally Block
Java Throws Keyword
Java Throw Keyword
Java Exception Propagation
Java Throw vs Throws
Final vs Finally vs Finalize
Exception Handling With Method Overridding
custom-exception

Read Other Java Chapter
Java Topic
Java Basic Tutorial
Java Control Statements
Java Classes & Object
Java Inheritance
Java Polymorphism
Java Abstraction
Java Encapsulation
Java OOPs Miscellaneous
Java Array
Java String
Java Exception Handling
Java Multithreading
Java Synchronization
Java Applet
Java 8 Features
Java 9 Features
Java Collection
Java Mcq
Java Interview Question
Tools
  

Useful Links

  • Home
  • Blog
  • About us
  • Contact Us
  • Privacy policy

Contact Us

Police Colony
Patna, Bihar
India

Email:

About DockerTpoint


India's largest site for Programming Tutorial as well as BANK, SSC, RAILWAY exam
and Campus placement preparation.