- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 9321 Articles for Object Oriented Programming
![karthikeya Boyini](https://www.tutorialspoint.com/assets/profiles/13518/profile/60_31598-1537784993.jpg)
291 Views
While using FileInputStream, FileOutputStream, and RandomAccessFile classes, we need to pass the path of the file to their constructors. In case of a file in the specified path does not exist a FileNotFoundException is raised.Examplepublic class Sample { public static void main(String args[]) throws Exception { File file = new File("myFile"); FileInputStream fis = new FileInputStream(file); System.out.println("Hello"); } }OutputException in thread "main" java.io.FileNotFoundException: myFile (The system cannot find the file specified) at java.io.FileInputStream.open(Native Method) at java.io.FileInputStream.open(Unknown Source) at java.io.FileInputStream.(Unknown Source) ... Read More
![Swarali Sree](https://www.tutorialspoint.com/assets/profiles/13515/profile/60_84115-1512649510.jpg)
348 Views
When you try to access an element of an array at an index which is out of range, an ArrayIndexOutOfBoundsException exception is raised.ExampleLive Demopublic class ArrayIndexOutOfBounds { public static void main(String args[]) { try { int[] a = new int[]{1,2,3,4,5}; int x = 6; a[10] = x; } catch(ArrayIndexOutOfBoundsException ex) { System.out.println("Array size is restricted to 5 elements only"); } } }OutputArray size is restricted to 5 elements only
![Swarali Sree](https://www.tutorialspoint.com/assets/profiles/13515/profile/60_84115-1512649510.jpg)
1K+ Views
When you divide a number by zero an Arithmetic Exception number is thrown.ExampleLive Demopublic class DividedByZero { public static void main(String args[]) { int a, b; try { a = 0; b = 54/a; System.out.println("hello"); } catch (ArithmeticException e) { System.out.println("you cannot divide a number with zero"); } } }Outputyou cannot divide a number with zero
![Lakshmi Srinivas](https://www.tutorialspoint.com/assets/profiles/13528/profile/60_91717-1512651120.jpg)
801 Views
Questions related to Java exception handling are most frequent during interviews for many companies and even in exams. One such question that an interviewer might ask is whether there is a case when the finally block does not execute in Java. We will try to find the answer to this question in the simplest way possible. In general, the finally block is designed to execute regardless of whether an exception is thrown or handled in the try-catch blocks. Is there a case when finally block does not execute in Java? Before moving to the question, it is necessary to discuss ... Read More
![Sharon Christine](https://www.tutorialspoint.com/assets/profiles/13517/profile/60_82215-1512649681.jpg)
295 Views
The throw keyword is used to raise an exception explicitly.Examplepublic class Test { public static void main(String[] args) { throw new NullPointerException(); } }Exception in thread "main" java.lang.NullPointerException at a6.dateAndTime.Test.main(Test.java:5)The throws keywords in Java used to postpone the handling of a checked exception.public class Test { public static void main(String[] args)throws NullPointerException { throw new NullPointerException(); } }
![Swarali Sree](https://www.tutorialspoint.com/assets/profiles/13515/profile/60_84115-1512649510.jpg)
275 Views
A catch statement involves declaring the type of exception you are trying to catch. If an exception occurs in the try block, the catch block (or blocks) that follows the try is checked. If the type of exception that occurred is listed in a catch block, the exception is passed to the catch block much as an argument is passed into a method parameter.Exampleimport java.io.File; import java.io.FileInputStream; public class Test { public static void main(String args[]) { System.out.println("Hello"); try { File file = new File("my_file"); ... Read More
![Samual Sam](https://www.tutorialspoint.com/assets/profiles/13514/profile/60_83486-1512649303.jpg)
411 Views
A try/catch block is placed around the code that might generate an exception. Code within a try/catch block is referred to as protected code, and the syntax for using try/catch looks like the following –Syntaxtry { // Protected code } catch (ExceptionName e1) { // Catch block }The code which is prone to exceptions is placed in the try block. When an exception raised inside a try block, instead of terminating the program JVM stores the exception details in the exception stack and proceeds to the catch block.
![Sai Subramanyam](https://www.tutorialspoint.com/assets/profiles/13512/profile/60_68191-1512710774.jpg)
145 Views
This Exception happens once you associated convert a string variable in an incorrect format to a whole number (numeric format) that's not compatible with one another. Example public class Test { public static void main(String[] args) { int data = Integer.parseInt("hello"); } } Output Exception in thread "main" java.lang.NumberFormatException: For input string: "hello" at java.lang.NumberFormatException.forInputString(Unknown Source) at java.lang.Integer.parseInt(Unknown Source) at java.lang.Integer.parseInt(Unknown Source) at a6.dateAndTime.Test.main(Test.java:5)