Java Exception

Ads
 

Java Exception

Explain about checked and unchecked Exceptions with Examples?

View Answers

August 11, 2012 at 10:47 AM

Checked Exceptions - These are the exceptions which occur during the compile time of the program. The compiler checks at the compile time that whether the program contains handlers for checked exceptions or not. These exceptions do not extend RuntimeException class and must be handled to avoid a compile-time error by the programmer. These exceptions extend the java.lang.Exception class These exceptional conditions should be anticipated and recovered by an application. Furthermore Checked exceptions are required to be caught. Remember that all the exceptions are checked exceptions unless and until those indicated by Error, RuntimeException or their subclasses.

For example if you call the readLine() method on a BufferedReader object then the IOException may occur or if you want to build a program that could read a file with a specific name then you would be prompted to input a file name by the application. Then it passes the name to the constructor for java.io.FileReader and opens the file. However if you do not provide the name of any existing file then the constructor throws java.io.FileNotFoundException which abrupt the application to succeed. Hence this exception will be caught by a well-written application and will also prompt to correct the file name.

Unchecked Exceptions - Unchecked exceptions are the exceptions which occur during the runtime of the program. Unchecked exceptions are internal to the application and extend the java.lang.RuntimeException that is inherited from java.lang.Exception class. These exceptions cannot be anticipated and recovered like programming bugs, such as logic errors or improper use of an API. These type of exceptions are also called Runtime exceptions that are usually caused by data errors, like arithmetic overflow, divide by zero etc.

Lets take the same file name example as described earlier. In that example the file name is passed to the constructor for FileReader. However, the constructor will throw NullPointerException if a logic error causes a null to be passed to the constructor. Well in this case the exception could be caught by the application but it would rather try to eliminate the bug due to which the exception has occurred. You must have encountered the most common exception in your program i.e. the ArithmeticException. I am sure you must be familiar with the reason of its occurrence that is when something tries to divide by zero. Similarly when an instance data member or method of a reference variable is to be accessed that hasn't yet referenced an object throws NullPointerException.

Ads









Related Tutorials/Questions & Answers:
Exception Java
Exception Java  Hi, What are the Exception Java? Thanks   Hi, Read it at Java Exception. Thanks
java exception
java exception   define an exception called no match exception tat z... { public static void main(String[] args) throws Exception { String z="Hello"; if(!z.equals("India")){ throw new Exception
Advertisements
Java Exception
Java Exception  Explain about checked and unchecked Exceptions... this exception will be caught by a well-written application and will also prompt... to the constructor. Well in this case the exception could be caught by the application
null pointer exception in java
null pointer exception in java  What is null pointer exception in Java and when it occurs? Show it with an example.Thanks!   Java Null Pointer Exception
java exception handle
java exception handle  How error can be handled in the exception class?? can we handle fatal error in java
java question on exception
java question on exception  can we use inbuilt exception variable with throw keyword in java
check null exception java
check null exception java  check null exception java - How to check the null exception on Java?   The null pointer exception in java occurs... on it. See the example null pointer exception
List of Java Exception
List of Java Exception       Exception in Java are classified on the basis of  the exception handled by the java compiler. Java consists of the following type of built
Java Stream Write Exception
Java Stream Write Exception  I am doing socket programming. I have...(Exception e) { System.out.println("Exception :: "+e); } When the line out.writeUTF(data1) is executed and catch catches it and shows the exception
what is a null pointer exception in java
what is a null pointer exception in java  what is a null pointer exception in java
numberformat exception - Java Beginners
Exception. How can i handle
java radio button and exception
java radio button and exception  Dear Sir/Madam, I have problem with my addBagelRadioButton and addCheeseRadioButton. There should be only 1 radio button can be selected, but somehow 2 can be selected the same time. Also, I
Java null pointer exception
Java null pointer exception  Hi I've made an array of Book objects that I made, the array is called library and it can hold 100 books, but currently there are 6. The Book objects contain three private strings; an author, a title
Java null pointer exception handling
Java null pointer exception handling   What is a java.lang.NullPointerException? Why do we use this exception class and when it occurs in any java program? Is this extends from exception class of Java?   Java null
Exception handling in java
Exception handling in java We are going to discus about Exception handling in java. Java program many provides exception. We are handle of error in program when during execution in a program .we are generate of exception try() block
exception
exception  chek in and check out exception in java   Please visit the following link: Checked and Unchecked Exception
Exception handling in Java Programming Examples
Exception handling in Java Programming with many Examples In this section we will explain you about the exception handling in Java and teach you how to write programs to handle exception in Java. Java programming language comes
Exception handling in Java Programming Examples
Exception handling in Java Programming with many Examples In this section we will explain you about the exception handling in Java and teach you how to write programs to handle exception in Java. Java programming language comes
Java : Exception Handling
Java : Exception Handling In this section we will discuss about  JAVA Exception Handling with simple example. Exception Handling : An exception... such file which doesn't exist. By default it is handled by java runtime
deffernce between checked and unchecked exception in java
deffernce between checked and unchecked exception in java  What is deffernce between checked and unchecked exception in java please explain by example
Java Illegal State Exception
Java Illegal State Exception   ... of exception. while some other classes are checked. Java Illegal State Exception... Exception with Example In this example we want to show you the java illegal state
exception
;Please visit the following links: http://www.roseindia.net/java/java-exception/user-defined-exception.shtml http://www.roseindia.net/java/exceptions/making
Exception
Exception  whis is the Arithmetic Exception in java? or define Arithmetic Exception with exp?   Arithmetic Exception occurs, when you divide a number by zero. Example public class TryCatch { public static void main
How to create custom exception in Java?
How to create custom exception in Java? In this tutorial we... custom exception class. Custom exceptions are used by Java developers to create.... Lets start developing our custom exception class.In Java API all exception
exception
exception  wHEN RUNTIME EXCEPTION CLASS IS SUB OF EXCEPTION CLASS HOW CAN'T HANDLE UNCHECKED EXCEPTION
Class Cast Exception Example in java
Class Cast Exception Example in java We are going to describe class cast exception in java. The class cast exception is a converting one data type into another data type. A Class cast exception is a thrown by java. In this example
Example to show double exception in java
Example to show double exception in java  ... exception in java. The given below program tells about the occurrence of double... in an object. Double exception occurs in java when the string or primitive data type
exception
arguments are not equalto two,throw a user defined exception "invalid parameter exception" ,otherwise display the two parameters.   Here is an example... is not equal to two then invalid parameter exception is thrown ,otherwise display the two
exception
exception  what is the use of catch block even though we have predefined exception object
exception
defined checked exception ââ?¬Å?InvalidCharcterFoundExceptionââ?¬Â? and creater a block of codes that will handle the exception
Exception
Exception  public class FooException extends Exception { public..."); } public void calculate() throws FooException, Exception { try { int.... ex.printStackTrace(); System.exit(1); } catch(Exception ex

Ads