An exception (or exceptional event) is a problem that arises during the execution of a program. When an Exception occurs the normal flow of the program is disrupted and the program/Application terminates abnormally, which is not recommended, therefore, these exceptions are to be handled.
An exception can occur for many different reasons. Following are some scenarios where an exception occurs.
A user has entered an invalid data.
A file that needs to be opened cannot be found.
A network connection has been lost in the middle of communications or the JVM has run out of memory.
Some of these exceptions are caused by user error, others by programmer error, and others by physical resources that have failed in some manner.
The Throws/Throw KeywordsIf a method does not handle a checked exception, the method must declare it using the throws keyword. The throws keyword appears at the end of a method's signature.
You can throw an exception, either a newly instantiated one or an exception that you just caught, by using the throw keyword.
Try to understand the difference between throws and throw keywords, throws is used to postpone the handling of a checked exception and throw is used to invoke an exception explicitly.
The following method declares that it throws a RemoteException −
Syntaxpublic static void accesElement() throws CustomException { try { int a[] = new int[2]; System.out.println("Access element three :" + a[3]); } catch(Exception e) { throw new CustomException(e); } }Example
In this example, we're calling a method accessElement() method which has declared to throw CustomException. So we need to write a try-catch block over accessElement() method. When program runs, it throws exception and it is printed.
// File Name : ExcepTest.java package com.tutorialspoint; public class ExcepTest { public static void main(String args[]) { try { accesElement(); } catch (CustomException e) { e.printStackTrace(); } System.out.println("Out of the block"); } public static void accesElement() throws CustomException { try { int a[] = new int[2]; System.out.println("Access element three :" + a[3]); } catch(Exception e) { throw new CustomException(e); } } } class CustomException extends Exception{ private static final long serialVersionUID = 1L; CustomException(Exception e){ super(e); } }Output
com.tutorialspoint.CustomException: java.lang.ArrayIndexOutOfBoundsException: 3 at com.tutorialspoint.ExcepTest.accesElement(ExcepTest.java:20) at com.tutorialspoint.ExcepTest.main(ExcepTest.java:8) Caused by: java.lang.ArrayIndexOutOfBoundsException: 3 at com.tutorialspoint.ExcepTest.accesElement(ExcepTest.java:18) ... 1 more Out of the block
Following is another example showcasing use of throws keyword.
ExampleIn this example, we're calling a method accessElement() method which has declared to throw CustomException. So we are not having try-catch block over accessElement() method, the main method declares to throw the exception. When program runs, it throws exception and it is printed.
// File Name : ExcepTest.java package com.tutorialspoint; public class ExcepTest { public static void main(String args[]) throws CustomException { accesElement(); System.out.println("Out of the block"); } public static void accesElement() throws CustomException { try { int a[] = new int[2]; System.out.println("Access element three :" + a[3]); } catch(Exception e) { throw new CustomException(e); } } } class CustomException extends Exception{ private static final long serialVersionUID = 1L; CustomException(Exception e){ super(e); } }Output
Exception in thread "main" com.tutorialspoint.CustomException: java.lang.ArrayIndexOutOfBoundsException: 3 at com.tutorialspoint.ExcepTest.accesElement(ExcepTest.java:16) at com.tutorialspoint.ExcepTest.main(ExcepTest.java:7) Caused by: java.lang.ArrayIndexOutOfBoundsException: 3 at com.tutorialspoint.ExcepTest.accesElement(ExcepTest.java:14) ... 1 more
As exception is not handled, last statement is not executed in this example.
A method can declare that it throws more than one exception, in which case the exceptions are declared in a list separated by commas. For example, the following method declares that it throws a RemoteException and an InsufficientFundsException −
Exampleimport java.io.*; public class className { public void withdraw(double amount) throws RemoteException, InsufficientFundsException { // Method implementation } // Remainder of class definition }
java_basic_syntax.htm
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4