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

Java throw and throws keyword

Java throw keyword

  • In Java, the "throws" keyword plays a vital role in declaring exceptions, serving as a notification to the programmer about potential exceptions that may arise. To ensure the smooth execution of a program, it is crucial for the programmer to incorporate exception handling code.
    • On the other hand, the "throw" keyword in Java empowers us to throw both checked and unchecked exceptions, specifically enabling the creation of custom exceptions. This keyword allows us to define specific conditions and explicitly throw exceptions as needed. For instance, if we encounter a scenario where division by zero occurs, we can throw an ArithmeticException. By setting the condition and utilizing the "throw" keyword, the exception can be thrown effortlessly.
Table Of Content

  • Java throw block
  • Example of throws block
  • Java throws block user define exception


  • By implementing these features effectively, Java developers can enhance the robustness and reliability of their code by appropriately managing exceptions throughout the program execution.




Syntax of Java throw block

throw Instance

Example:
throw new ArithmeticException("/ by zero");

Java throw block


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) 

Java Exception Handling involves the interruption of program execution when a throw statement is executed. Subsequently, the closest try block is examined to determine if it contains a corresponding catch statement for the specific type of exception. If a match is found, control is transferred to that particular statement. However, if no matching catch is discovered, the program will be halted by the default exception handler. This mechanism ensures effective management of exceptions in Java programming.



Java throws block user define exception


class userException extends Exception  
{  
    public userException(String str)  {  
        super(str);  
    }  
}
public class Main {	
  public static void main(String args[]){
      try{  
          throw new userException("User defined exception");  
      }  
      catch (userException userexception){  
          System.out.println("Exception Caught");  
          System.out.println(userexception.getMessage());  
      } 
  }
}



Output:

Exception Caught
User defined exception 


Java exception propagation 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.