Java Custom Exceptions

Source: Internet
Author: User

First, the keyword explanation:

1.try catch: This includes code that is caught and processed if an exception occurs, and if there is an error between the code, it does not affect the execution of the program;

2.throw: The exception that is explicitly thrown in the program, such as the throw New Exception ();

3.throws: Indicates that the method may throw an exception, introduces an exception class through throw new Exception (), does not handle the exception within the method, and handles the exception by using the TYR catch statement;

4.finally: This code will be executed regardless of the exception program segment

Second, exception class commonly used methods:

Construction method: Public Exception ();

Public Exception (String s)://String S is a description of the exception;

Exception Handling methods:

Public String toString (): Returns the description of the current exception object information;

Public String getMessage (); Returns a detailed description of the current exception object information;

public void Printstacktrace (): Used to track the execution of stack contents when an exception event occurs;

Iii. Examples of custom exceptions:

Check for login information

Import java.util.*;
Class EXCEPTLGN extends runtimeexception{//Custom exception
EXCEPTLGN () {
Super ("Input username or password is wrong, please re-enter");
}
}

Class connectserver{
Private final String id= "test";
Private final String pwd= "123456";
void Connect (String id,string pwd) throws exceptlgn{//method declaration calling custom exception
if (this.id.equals (ID) &&this.pwd.equals (pwd)) {
SYSTEM.OUT.PRINTLN ("Emulation login server succeeded");
}else{
throw new Exceptlgn (); Throws an Object
}
}
}

public class Exceptionlogin {
public void Login (String id,string pwd) {
ConnectServer cs=new ConnectServer ();
try{
Cs.connect (ID,PWD);
}catch (RuntimeException e) {
System.out.println (E.getmessage ()); Throw exception when executing this method
System.out.println (E.tostring ());
}
}
public static void Main (string[] args) {
Exceptionlogin ext=new Exceptionlogin ();
Scanner sc=new Scanner (system.in);
System.out.println ("Please enter user name and password:");
String Id=sc.nextline ();
String Pwd=sc.nextline ();
Ext.login (ID, PWD);

}

Java Custom Exceptions

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.