In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces java how to deal with exceptions, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, let the editor take you to understand it.
1. When there is a problem with the java program, the system automatically detects the error and immediately generates an exception object corresponding to the error.
two。 Then give the exception object to the java virtual machine
3. The virtual machine automatically looks for the corresponding code to handle the exception. If not, after the virtual machine does some simple processing, the program will be forced to terminate.
4. Abnormal program entered by keyboard
Import java.util.*
Public class Test
{
Public static void main (String [] args)
{
Scanner sc=new Scanner (System.in)
Try {
Int i=sc.nextInt ()
System.out.println ("I =" + I)
}
Catch (Exception e)
{
System.out.println ("incorrect input")
}
}
}
5. Common anomalies
(1) null pointer exception B b=null
(2) Array out of bounds exception
(3) arithmetic anomaly
6. Process flow
Try {
Statement 1
Statement 2
}
The processing of catch () {/ / statement one
}
Processing of catch () {/ / statement 2
}
...
Finally {
} / / Code that must be executed
If statement 1 goes wrong, catch2 directly, and statement 2 will not be executed
7.throw
Throw is used to throw an exception
Throw new exception name (parameter)
Suppose the f method throws exception A, and there are two ways to handle the exception
(1) void f () throws A
Who calls the f method and who handles the exception? the Afocus f method itself does not handle the exception.
Note: throws A means that a class An exception may or may not be thrown when the f method is called. The exception thrown may or may not be handled.
(2) try catch
F method itself handles An exception
8. A normal class cannot be thrown. You can inherit the Throwable class.
9. Define the exception throw by yourself
Class DivisorisZeroException extends Exception {
Public DivisorisZeroException (String name) {super (name); / / A constructor of the parent class Exception can be called if the divisor cannot be 0.
}
Public class B {
Public int divide (int a pencil int b) throws DivisorisZeroException
{
Int m
If (breadbasket 0)
{
Throw new DivisorisZeroException ("Divisor cannot be 0")
}
Else
{
M=a/b
}
Return m
}
}
Public class Test
{
Public static void main (String [] args)
{
B bb=new B ()
Try {
Bb. Coach (6. 0)
}
Catch (Exception e)
{
E.printStackTrace ()
}
}
}
10.catch
Only one of all catch is executed. Maybe all of them will not be executed.
First catch subclass exceptions and then catch parent class exceptions (write in order otherwise the subclass exceptions are all caught by the parent class exceptions will never be executed)
The exception range thrown by an overridden method cannot be greater than that of the overridden method.
Thank you for reading this article carefully. I hope the article "how to deal with exceptions in java" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.