• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Licensed to the Apache Software Foundation (ASF) under one or more
3  *  contributor license agreements.  See the NOTICE file distributed with
4  *  this work for additional information regarding copyright ownership.
5  *  The ASF licenses this file to You under the Apache License, Version 2.0
6  *  (the "License"); you may not use this file except in compliance with
7  *  the License.  You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  */
17 
18 package java.net;
19 
20 /**
21  * A {@code URISyntaxException} will be thrown if some information could not be parsed
22  * while creating a URI.
23  */
24 public class URISyntaxException extends Exception {
25 
26     private static final long serialVersionUID = 2137979680897488891L;
27 
28     private String input;
29 
30     private int index;
31 
32     /**
33      * Constructs a new {@code URISyntaxException} instance containing the
34      * string that caused the exception, a description of the problem and the
35      * index at which the error occurred.
36      *
37      * @param input
38      *            the string that caused the exception.
39      * @param reason
40      *            the reason why the exception occurred.
41      * @param index
42      *            the position where the exception occurred.
43      * @throws NullPointerException
44      *             if one of the arguments {@code input} or {@code reason} is
45      *             {@code null}.
46      * @throws IllegalArgumentException
47      *             if the value for {@code index} is lesser than {@code -1}.
48      */
URISyntaxException(String input, String reason, int index)49     public URISyntaxException(String input, String reason, int index) {
50         super(reason);
51 
52         if (input == null || reason == null) {
53             throw new NullPointerException();
54         }
55 
56         if (index < -1) {
57             throw new IllegalArgumentException();
58         }
59 
60         this.input = input;
61         this.index = index;
62     }
63 
64     /**
65      * Constructs a new {@code URISyntaxException} instance containing the
66      * string that caused the exception and a description of the problem.
67      *
68      *@param input
69      *            the string that caused the exception.
70      * @param reason
71      *            the reason why the exception occurred.
72      * @throws NullPointerException
73      *             if one of the arguments {@code input} or {@code reason} is
74      *             {@code null}.
75      */
URISyntaxException(String input, String reason)76     public URISyntaxException(String input, String reason) {
77         super(reason);
78 
79         if (input == null || reason == null) {
80             throw new NullPointerException();
81         }
82 
83         this.input = input;
84         index = -1;
85     }
86 
87     /**
88      * Gets the index at which the syntax error was found or {@code -1} if the
89      * index is unknown/unavailable.
90      *
91      * @return the index of the syntax error.
92      */
getIndex()93     public int getIndex() {
94         return index;
95     }
96 
97     /**
98      * Gets a description of the syntax error.
99      *
100      * @return the string describing the syntax error.
101      */
getReason()102     public String getReason() {
103         return super.getMessage();
104     }
105 
106     /**
107      * Gets the initial string that contains an invalid syntax.
108      *
109      * @return the string that caused the exception.
110      */
getInput()111     public String getInput() {
112         return input;
113     }
114 
115     /**
116      * Gets a description of the exception, including the reason, the string
117      * that caused the syntax error and the position of the syntax error if
118      * available.
119      *
120      * @return a sting containing information about the exception.
121      * @see java.lang.Throwable#getMessage()
122      */
123     @Override
getMessage()124     public String getMessage() {
125         String reason = super.getMessage();
126         if (index != -1) {
127             return reason + " at index " + index + ": " + input;
128         }
129         return reason + ": " + input;
130     }
131 }
132