GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||||||||||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||||||||||||||||||||||||||||||||||||
GeezerVison is showing www.tutorialspoint.com — Java - Built-in Exceptions
Selected Reading
Java - Built-in ExceptionsBuilt-in Exceptions in JavaJava defines several exception classes inside the standard package java.lang. The most general of these exceptions are subclasses of the standard type RuntimeException. Since java.lang is implicitly imported into all Java programs, most exceptions derived from RuntimeException are automatically available. Types of Java Built-in ExceptionsBuilt-in Exceptions in Java are categorized into two categories Checked Exceptions and Unchecked Exceptions.
Common Built-in Exceptions in JavaJava defines several other types of exceptions that relate to its various class libraries. Following is the list of Java Unchecked and Checked RuntimeException.
Examples of Java Built-in ExceptionExample 1: Demonstrating Arithmetic Exception Without try-catchIn this example, we're creating an error by dividing a value by 0. In this case, an unchecked exception will be raised. Being unchecked, compiler won't complain and program will compile successfully. Once program runs, the exception will be thrown and JVM will intercepts the same and terminate the program before printing the last statement. package com.tutorialspoint;
public class ExcepTest {
public static void main(String args[]) {
int b = 0;
int c = 1/b;
System.out.println("c :" + c);
}
}
OutputException in thread "main" java.lang.ArithmeticException: / by zero at com.tutorialspoint.ExcepTest.main(ExcepTest.java:8) Example 2: Demonstrating Arithmetic Exception With try-catchIn this example, we're handling unchecked exception. As first step, we're generating an error by dividing a value by 0. In this case, an unchecked exception will be raised. We're handling via ArithmeticException. Once program runs, the exception will be thrown and catch block will intercepts the same and print the last statement. package com.tutorialspoint;
public class ExcepTest {
public static void main(String args[]) {
try {
int b = 0;
int c = 1/b;
System.out.println("c :" + c);
}
catch (ArithmeticException e) {
System.out.println("Exception thrown :" + e);
}
System.out.println("Out of the block");
}
}
OutputException thrown :java.lang.ArithmeticException: / by zero Out of the block Example 3: Demonstrating No Such Method ExceptionIn this example, we're showcasing that a checked exception is to be handled by code otherwise compiler will complain. Whenever a method throws a checked exception, it has to either handle the exception or declare throws exception statement as we're doing for getName() method. When we try to run the method, JVM complains the compilation problem as shown in output listed below: package com.tutorialspoint;
public class ExcepTest {
public static void main(String args[]) {
ExcepTest excepTest = new ExcepTest();
excepTest.getName();
}
private String getName() throws NoSuchMethodException {
throw new NoSuchMethodException();
}
}
OutputException in thread "main" java.lang.Error: Unresolved compilation problem:
Unhandled exception type NoSuchMethodException
at com.tutorialspoint.ExcepTest.main(ExcepTest.java:7)
java_exceptions.htm
Advertisements
| ||||||||||||||||||||||||||||||||||||||||||||||
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |