Java Lesson 9 (Exception handling)

Source: Internet
Author: User

Hands on Brain 1:

Import javax.swing.*;

Class Aboutexception {
public static void Main (string[] a)
{
int I=1, j=0, K;
k=i/j;


Try
{

K = i/j; Causes Division-by-zero exception
throw new Exception ("hello.exception!");
}

catch (ArithmeticException e)
{
System.out.println ("was removed by 0." + e.getmessage ());
}

catch (Exception e)
{
if (e instanceof arithmeticexception)
System.out.println ("by 0 divide");
Else
{
System.out.println (E.getmessage ());

}
}


Finally
{
Joptionpane.showconfirmdialog (null, "OK");
}

}
}

Output Result:

Hands on Brain 2:

Code:

public class Catchwho {
public static void Main (string[] args) {
try {
try {
throw new ArrayIndexOutOfBoundsException ();
}
catch (ArrayIndexOutOfBoundsException e) {
System.out.println ("arrayindexoutofboundsexception" + "/Inner layer Try-catch");
}

throw new ArithmeticException ();
}
catch (ArithmeticException e) {
System.out.println ("occurring arithmeticexception");
}
catch (ArrayIndexOutOfBoundsException e) {
System.out.println ("arrayindexoutofboundsexception" + "/outer try-catch");
}
}

Output Result:

Hands on the brain 3;

Source:

public class Embededfinally {


public static void Main (String args[]) {

int result;

try {

System.out.println ("in Level 1");


try {

System.out.println ("in Level 2");
result=100/0; Level 2

try {

System.out.println ("in Level 3");

result=100/0; Level 3

}

catch (Exception e) {

System.out.println ("Level 3:" + E.getclass (). toString ());

}


finally {

System.out.println ("at level 3 finally");

}


result=100/0; Level 2


}

catch (Exception e) {

System.out.println ("Level 2:" + E.getclass (). toString ());

}
finally {

System.out.println ("at level 2 finally");

}

result = 100/0; Level 1

}

catch (Exception e) {

System.out.println ("Level 1:" + e.getclass (). toString ());

}

finally {

. System.out.println ("at level 1 finally");

}

}

}

Output Result:

4: When there are multiple layers of nested Finally, the exception is thrown at different levels and thrown at different locations, which may result in a different order of the finally statement block execution.

Source:
public class Systemexitandfinally {


public static void Main (string[] args)
{

try{


SYSTEM.OUT.PRINTLN ("in Main");

throw new Exception ("Exception is thrown in main");

System.exit (0);


}

catch (Exception e)

{

System.out.println (E.getmessage ());

System.exit (0);

}

Finally

{

System.out.println ("in finally");

}
}
}

Output Result:

Hands on Brain 5:

is the finally statement block bound to execute?

There are at least two cases in which the finally statement is not executed:
(1) The Try statement is not executed, such as return before the try statement, so that the finally statement is not executed. This also shows that the necessary rather than sufficient condition for the finally statement to be executed is that the corresponding try statement must be executed.

(2) There is system.exit (0) in the try block |catch block, such a statement. System.exit (0) Terminates the Java Virtual Machine JVM, and even the JVM is stopped, all of it is over, and the finally statement is not executed.

Source:

Usingexceptions.java
Demonstrating the GetMessage and Printstacktrace
Methods inherited into all exception classes.
public class Printexceptionstack {
public static void Main (String args[])
{
try {
Method1 ();
}
catch (Exception e) {
System.err.println (E.getmessage () + "\ n");
E.printstacktrace ();
}
}

public static void Method1 () throws Exception
{
Method2 ();
}

public static void Method2 () throws Exception
{
Method3 ();
}

public static void Method3 () throws Exception
{
throw new Exception ("Exception thrown in method3");
}
}

Output:

Hands on Brain 6:

How do I track the propagation path of an exception?

When an exception occurs in the program, the JVM looks for the error handler in order of the method invocation.

You can use the Printstacktrace and GetMessage methods to understand what happens when an exception occurs: Printstacktrace: Print the method call stack.

Each object of the Throwable class has a GetMessage method that returns a string that is passed in the exception constructor and typically contains information about a particular exception.

Source:

Import java.io.*;
public class Throwmultiexceptionsdemo {
public static void Main (string[] args)
{
try {
Throwstest ();
}
catch (IOException e) {
System.out.println ("catch exception");
}
}

private static void Throwstest () throws Arithmeticexception,ioexception {
System.out.println ("This is just a test");
The program process assumes that an exception occurred
throw new IOException ();
throw new ArithmeticException ();
}
}

Output:

A method can declare multiple exceptions to be thrown

int g (float h) throws Oneexception,twoexception {...}

It's just a test.
Catching exceptions

Import java.io.*;

public class Test
{
public void Test () throws IOException
{
FileInputStream fis = new FileInputStream ("A.txt");
}
}
Class Sub extends Test
{
If the test method declaration throws a larger exception than the parent class method, such as exception
The code will not compile ...
public void Test () throws FileNotFoundException
{
//...
}
}

The throws clause of a subclass throws an exception that cannot be the parent of an exception object that is thrown by a method with the same name as its base class.

Hands on Brain 7:

Write a program that requires the user to enter an integer at run time, representing the exam results of a course, followed by a "fail", "Pass", "Medium", "good", "excellent" conclusion.

Requires that the program be robust enough that it does not crash regardless of what the user enters.

Source:

Import Javax.swing.JOptionPane;
public class Score {
public static void Main (string[] args) {
for (;;)
{
String a = Joptionpane.showinputdialog ("Please enter a score:");
try{
int B = Integer.parseint (a);
if (b>0&&b<60) {
Joptionpane.showmessagedialog (NULL,
"Less than lattice!");
Break
}
else if (b>=60&&b<=70)
{
Joptionpane.showmessagedialog (NULL,
Failed ");
Break
}
else if (b>70&&b<=80)
{
Joptionpane.showmessagedialog (NULL,
"Average grade!");
Break
}
else if (b>80&&b<=90)
{
Joptionpane.showmessagedialog (NULL,
"Good grades!");
Break
}
else if (b>90&&b<=100)
{
Joptionpane.showmessagedialog (NULL,
"Excellent performance!");
Break
}
else if (b>100| | B&LT;0)
{
Joptionpane.showmessagedialog (NULL,
"You entered the score out of range, please re-enter!");
}
}
catch (Exception e) {
Joptionpane.showmessagedialog (NULL,
"Your input is wrong, please re-enter!");
}
}

}
}

Output:

Java Lesson 9 (Exception handling)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.