BogoToBogo
  • Home
  • About
  • Big Data
  • Machine Learning
  • AngularJS
  • Python
  • C++
  • go
  • DevOps
  • Kubernetes
  • Algorithms
  • More...
    • Qt 5
    • Linux
    • FFmpeg
    • Matlab
    • Django 1.8
    • Ruby On Rails
    • HTML5 & CSS

Exception Handling II - 2020

Duke 512




Bookmark and Share





bogotobogo.com site search:




Error and Exception

Every exception is an instance of a class that has class Exception in its inheritance hierarchy. In other words, exceptions are always a subclass of java.lang.Exceptions.

All exception classes are subtypes of class Exception which derives from the class Throwable which is derives from the class Object.


ExceptionClasses

As we see, there are two subclasses that derive from Throwable: Exception and Error.

  1. Error
    1. This represents unusual situations which are not from program errors, and indicate things that would not normally happen during the execution, such as the running out of memory.
    2. In general, our application won't be able to recover from an Error, so we're not required to handle them.
    3. Errors are technically not exceptions because they do not derive from class Exception.
  2. Exception
    1. This represents something that happened not as a result of a programming error, but rather because some resource is not available or some other condition required for execution in not present.
    2. For example, if our application is supposed to communicate with another program that's not answering, this is an exception that is not caused by a bug.
    3. RuntimeExceptions are a special case because they sometimes do indicate program errors.




Checked and Unchecked Exceptions

Except for RuntimeException, Error, and their subclasses, all exceptions are called checked exceptions. The compiler ensures that if a method can throw a checked exception, directly or indirectly, the method explicitly deal with it. The method must either catch the exception and take the appropriate action, or pass the exception onto ins caller.




Exceptions defined by Error and RuntimeException classes and their subclasses are known as unchecked exception, meaning that a method is not obliged to deal with these kinds of exceptions. They are either irrecoverable (Error class) and the program should not attempt to deal with them, or they are programming errors (RuntimeException class) and should usually be dealt with as such, and not as exceptions.

  1. Checked Exceptions
    1. Exceptions that are not RuntimeException and Error
    2. All Exceptions the compiler cares about are called checked exceptions which really mean compiler-checked exceptions.
    3. Caused by a condition that fails at runtime in ways that we cannot predict or prevent.
    4. We cannot guarantee the server is up.
    5. The method should do "handle or declare"
    6. Use try/catch blocks to try to recover from situations we can't guarantee will succeed.
    7. At the very least, print out a message to the user and a stack trace, so somebody can figure out what happened.
  2. Unchecked Exceptions
    1. Most RuntimeExceptions come from a problem in our code logic.
    2. We can make sure our code doesn't index off the end of an array.
    3. We want RuntimeExceptions to happen at development and testing time.
    4. We don't want to code in a try/catch and have the overhead that goes with it, to catch something that shouldn't happen in the first place.
    5. We can throw, catch, and declare RuntimeExceptions, but we don't have to, and the compiler won't check.

The compiler checks for everything except for RuntimeExceptions .

The compiler guarantees:

  1. If you throw an exception in your code you must declare it using the throw keyword in your method declaration.
  2. If you call a method that throws an exception, in other words, a method that declares it throws an exception, you must acknowledge that you're aware of the exception possibility. One way to satisfy the compiler is to wrap the call in a try/catch.
  3. If you're not prepared to handle the exception, you can still make the compiler happy by officially ducking the exception.

  1. ArrayIndexOutOfBoundsException
    Thrown when attempting to access an array with an invalid value (either negative or beyond the length of the array.
  2. ClassCastException
    Thrown when attempting to cast a reference variable to a type that fails the IS-A test.
  3. IllegalArgumentException
    Thrown when a method receives an argument formatted differently that the method expects.
  4. IllegalStateException
    Thrown when the state of the environment doesn't match the operation being attempted, e.g., using a Scanner that's been closed.
  5. NullPointerException
    Thrown when attempting to access an object with a reference variable whose current value is null.
    class NullPointerEx {
    	static String s;
    	public static void main (String[] args) {
    		System.out.println(s.length());  	//NullPointerException
    	}
    }
    
  6. NumberFormatException
    Thrown when a method that converts a String to a number receives a String that it cannot convert.
    int parseInt(String s) throws NumberFormatException {
    	boolean parseSuccess = false;
    	int result = 0;
    	// parsing
    	if(!parseSuccess)
    		throw new NumberFormatException();
    	return result;
    }
    
  7. AssertionError
    Thrown when a statement's boolean test returns false.
  8. ExceptionInInitializerError
    Thrown when attempting to initialize a static variable or an initialization block.
    public class InitializationError {
    	static int [] j = new int [4];
    	static { j[4] = 10 ; }	// bad array index
    	public static void main( String[] args) {}
    }
    
  9. StackOverflowError
    Thrown when a method recurses too deeply.
    void callme() {
    	callme();
    }
    
  10. NoClassDefFoundError
    Thrown when the JVM can't find a class it needs, because of a command-line error, a classpath issue, or a missing class file.






Ph.D. / Golden Gate Ave, San Francisco / Seoul National Univ / Carnegie Mellon / UC Berkeley / DevOps / Deep Learning / Visualization

YouTubeMy YouTube channel

Sponsor Open Source development activities and free contents for everyone.

Thank you.

- K Hong







Java Tutorials



Java Tutorial Home

Basics - Compiling and Launching

Inner Classes

Constructor

Enums

Static & Finally

Default and Protected

Polymorphism

Exception Handling

Exception Handling II

String Class

Threads

Threads II - State Transition

Threads III - Synchronization

Object Class

File I/O

Serialization

ArrayList

Autoboxing

Java Graphics Interface I - Basics

Java Graphics Interface II - Labels, Text Fields, Layouts

Java Graphics Interface III - paintComponent

TCP Sockets Server/Client

Scala - Functional Java Programming

Apache CXF install

Tomcat 7 Ubuntu 14 Install on Amazon EC2 instance

What is Apache Maven?

Maven life cycle

Eclipse Maven 3 plugin on Ubuntu 14.04

Apache Maven 3 - Setting up and creating a project

Apache Maven 3 - Compile, build, and install a Maven project

Apache Maven 3 - Dependencies

Apache Maven 3 - Web Application

Apache Maven 3 - Plugins (compiler)

Apache Maven 3 - Plugins (Jetty)

Eclipse CDT / JNI (Java Native Interface) / MinGW



Spring Framework

Hello World App with Spring 4 & Maven 3 - Part I




Sponsor Open Source development activities and free contents for everyone.

Thank you.

- K Hong







Spring Boot



Spring Boot : Hello world with Mavan 3

Spring Boot : Hello world with Gradle 2

Spring Boot (Gradle 2) : Hello world with Authentication

Spring Boot : Deploying War file to Tomcat 8's webapps

How to Setup Apache as Reverse Proxy for Tomcat Server using mod proxy

Maven : mvn command cheat sheet

Spring-Boot REST API with CORS App Maven war file deploy to Tomcat

Spring-Boot / Spring Security with AngularJS - Part I (Introduction)

Spring-Boot / Spring Security with AngularJS - Part II (Dynamic resource load from Angular)

Spring-Boot / Spring Security with AngularJS : Part III (Form-based Authentication)





JUnit & Maven Tutorial



JUnit 4 Introduction (Hello World)

JUnit 4 Test with Eclipse Luna (Hello World)

JUnit 4 Test with Maven (Hello World)











Contact

BogoToBogo
contactus@bogotobogo.com

Follow Bogotobogo

About Us

contactus@bogotobogo.com

YouTubeMy YouTube channel
Pacific Ave, San Francisco, CA 94115

Pacific Ave, San Francisco, CA 94115

Copyright © 2024, bogotobogo
Design: Web Master