Java Exception Keywords

There are 5 keywords which are used in handling exceptions in Java.
try:The "try" keyword is used to specify a block where we should place exception code. The try block must be followed by either catch or finally. It means, we can't use try block alone.

catch:The "catch" block is used to handle the exception. It must be preceded by try block which means we can't use catch block alone. It can be followed by finally block later.

finally:The "finally" block is used to execute the important code of the program. It is executed whether an exception is handled or not.

throw:The "throw" keyword is used to throw an exception.

throws:The "throws" keyword is used to declare exceptions. It doesn't throw an exception. It specifies that there may occur an exception in the method. It is always used with method signature.
Java Exception Handling Example
public class JavaExceptionExample{  
public static void main(String args[]){  
try{  
//code that may raise exception  
int data=100/0;  
}catch(ArithmeticException e){System.out.println(e);}  
//rest code of the program   
System.out.println("rest of the code...");  
}  

Comments

Popular posts from this blog

Flutter : Introduction

Dart: Functions

Firebase Cloud Messaging