java.net.URISyntaxException
A checked exception is thrown to indicate that a string could not be parsed as a URI reference.
Declaration
public class URISyntaxException extends Exception {@java.io.Serialprivate static final long serialVersionUID = 2137979680897488891L;private String input;private int index;public URISyntaxException(String input, String reason, int index) {super(reason);if ((input == null) || (reason == null))throw new NullPointerException();if (index < -1)throw new IllegalArgumentException();this.input = input;this.index = index;}public URISyntaxException(String input, String reason) {this(input, reason, -1);}public String getInput() {return input;}public String getReason() {return super.getMessage();}public int getIndex() {return index;}public String getMessage() {StringBuilder sb = new StringBuilder();sb.append(getReason());if (index > -1) {sb.append(" at index ");sb.append(index);}sb.append(": ");sb.append(input);return sb.toString();}}
Methods
1. URISyntaxException(String input, String reason)
java.net.URISyntaxException.URISyntaxException(String input, String reason)
This method takes two arguments. This method constructs an instance from the given input string and reason. The resulting object will have an error index of -1.
Parameters: Two parameters are required for this method.
input: The input string.
reason: A string explaining why the input could not be parsed.
Throws:
1. NullPointerException - If either the input or reason strings are null
Approach 1: When no exception
Java
package com.URISyntaxException;import java.net.URISyntaxException;public class URISyntaxException1 {public static void main(String[] args) {String input = "input", reason = "reason";URISyntaxException uriSyntaxException = new URISyntaxException(input, reason);System.out.println(uriSyntaxException);}}
Output:
java.net.URISyntaxException: reason: input
Approach 2: NullPointerException
Java
package com.URISyntaxException;import java.net.URISyntaxException;public class URISyntaxException1 {public static void main(String[] args) {String input = "input", reason = null;URISyntaxException uriSyntaxException = new URISyntaxException(input, reason);System.out.println(uriSyntaxException);}}
Output:
Exception in thread "main" java.lang.NullPointerException at java.base/java.net.URISyntaxException.<init>(URISyntaxException.java:65) at java.base/java.net.URISyntaxException.<init>(URISyntaxException.java:83) at com.URISyntaxException.URISyntaxException1.main(URISyntaxException1.java:9)
2. URISyntaxException(String input, String reason, int index)
java.net.URISyntaxException.URISyntaxException(String input, String reason, int index)
This method takes three arguments. This method constructs an instance from the given input string, reason, and error index.
Parameters: Three parameters are required for this method.
input: The input string.
reason: A string explaining why the input could not be parsed.
index: The index at which the parse error occurred, or -1 if the index is not known.
Throws:
1. NullPointerException - If either the input or reason strings are null.
2. IllegalArgumentException - If the error index is less than -1
Approach 1: When no exception
Java
package com.URISyntaxException;import java.net.URISyntaxException;public class URISyntaxException2 {public static void main(String[] args) {String input = "input", reason = "reason";int index = 1;URISyntaxException uriSyntaxException = new URISyntaxException(input, reason, index);System.out.println(uriSyntaxException);}}
Output:
java.net.URISyntaxException: reason at index 1: input
Approach 2: NullPointerException
Java
package com.URISyntaxException;import java.net.URISyntaxException;public class URISyntaxException2 {public static void main(String[] args) {String input = "input", reason = null;int index = 1;URISyntaxException uriSyntaxException = new URISyntaxException(input, reason, index);System.out.println(uriSyntaxException);}}
Output:
Exception in thread "main" java.lang.NullPointerException at java.base/java.net.URISyntaxException.<init>(URISyntaxException.java:65) at com.URISyntaxException.URISyntaxException2.main(URISyntaxException2.java:10)
Approach 3: IllegalArgumentException
Java
package com.URISyntaxException;import java.net.URISyntaxException;public class URISyntaxException2 {public static void main(String[] args) {String input = "input", reason = "reason";int index = -2;URISyntaxException uriSyntaxException = new URISyntaxException(input, reason, index);System.out.println(uriSyntaxException);}}
Output:
Exception in thread "main" java.lang.IllegalArgumentException at java.base/java.net.URISyntaxException.<init>(URISyntaxException.java:67) at com.URISyntaxException.URISyntaxException2.main(URISyntaxException2.java:10)
3. getIndex()
int java.net.URISyntaxException.getIndex()
This method returns an index into the input string of the position at which the parse error occurred, or -1 if this position is not known.
Parameters: NA
Returns: The error index.
Exceptions: NA
Approach
Java
package com.URISyntaxException;import java.net.URISyntaxException;public class URISyntaxExceptiongetIndex {public static void main(String[] args) {String input = "input", reason = "reason";int index = 1;URISyntaxException uriSyntaxException = new URISyntaxException(input, reason, index);System.out.println(uriSyntaxException.getIndex());}}
Output:
1
4. getInput()
String java.net.URISyntaxException.getInput()
This method returns the input string.
Parameters: NA
Returns: The input string.
Exceptions: NA
Approach
Java
package com.URISyntaxException;import java.net.URISyntaxException;public class URISyntaxExceptiongetInput {public static void main(String[] args) {String input = "input", reason = "reason";int index = 1;URISyntaxException uriSyntaxException = new URISyntaxException(input, reason, index);System.out.println(uriSyntaxException.getInput());}}
Output:
input
5. getMessage()
String java.net.URISyntaxException.getMessage()
This method returns a string describing the parse error. The resulting string consists of the reason string followed by a colon character(':'), a space, and the input string. If the error index is defined then the string " at index " followed by the index, in decimal, is inserted after the reason string and before the colon character.
Parameters: NA
Returns: A string describing the parse error.
Exceptions: NA
Approach
Java
package com.URISyntaxException;import java.net.URISyntaxException;public class URISyntaxExceptiongetMessage {public static void main(String[] args) {String input = "input", reason = "reason";int index = 1;URISyntaxException uriSyntaxException = new URISyntaxException(input, reason, index);System.out.println(uriSyntaxException.getMessage());}}
Output:
reason at index 1: input
6. String java.net.URISyntaxException.getReason()
This method returns a string explaining why the input string could not be parsed.
Parameters: NA
Returns: The reason string
Exceptions: NA
Approach
Java
package com.URISyntaxException;import java.net.URISyntaxException;public class URISyntaxExceptiongetReason {public static void main(String[] args) {String input = "input", reason = "reason";int index = 1;URISyntaxException uriSyntaxException = new URISyntaxException(input, reason, index);System.out.println(uriSyntaxException.getReason());}}
Output:
reason
No comments:
Post a Comment