Java 需要声明aide编译器打开输入流中的奇怪错误
我正在使用Aide,它在openInputStream上给了我一个奇怪的错误:Java 需要声明aide编译器打开输入流中的奇怪错误,java,android,android-intent,compiler-errors,aide,Java,Android,Android Intent,Compiler Errors,Aide,我正在使用Aide,它在openInputStream上给了我一个奇怪的错误: The Exception 'java.io.FileNotFoundException' must be caught or declared in the throws clause 我的代码: case R.id.album: intent = new Intent("android.intent.action.GET_CONTENT"); intent.set
The Exception 'java.io.FileNotFoundException' must be caught or declared in the throws clause
我的代码:
case R.id.album:
intent = new Intent("android.intent.action.GET_CONTENT");
intent.setType("image/*");
startActivityForResult(intent, R.id.album);
break;
case R.id.camera:
intent = new Intent("android.media.action.IMAGE_CAPTURE");
intent.putExtra("output", Uri.fromFile(new File(Environment.getExternalStorageDirectory(), "camera.jpg")));
startActivityForResult(intent, R.id.camera);
break;
我没有发现我的代码中有任何错误,可能Aide需要更多的代码,或者它不支持这种代码。 我试图从其他类似的问题中找出答案,但什么也没找到。
代码是否有替换?代码的问题是,您调用的方法可能会引发
FileNotFoundException
。。。但是您没有在围绕调用的异常处理程序中捕获异常
我想它就在这里:
this.paintView.setPicture(BitmapFactory.decodeStream(
getContentResolver().openInputStream(data.getData())));
我还要注意,类似这样的代码:
try {
...
} catch (Exception e) {
// do nothing!
}
` switch (request) {
case R.id.album:
try{
if (result == -1) {
this.paintView.setPicture(BitmapFactory.decodeStream(getContentResolver().openInputStream(data.getData())));
}
}
catch (FileNotFoundException e) {}
catch (Exception e){}`
称为“异常挤压”。这是坏习惯。这种事情掩盖了错误。1) 它通常允许异常引起的“损害”扩展到应用程序的其他部分。2) 它使准确诊断错误的真正原因变得更加困难
你正在挤压异常,这会让事情变得更糟
简而言之,如果您习惯性地挤压java.lang.Exception
:
- 您的代码可能不可靠
- 你不知道为什么,而且
- 你将无法修复它
FileNotFoundException
。。。但是您没有在围绕调用的异常处理程序中捕获异常
我想它就在这里:
this.paintView.setPicture(BitmapFactory.decodeStream(
getContentResolver().openInputStream(data.getData())));
我还要注意,类似这样的代码:
try {
...
} catch (Exception e) {
// do nothing!
}
` switch (request) {
case R.id.album:
try{
if (result == -1) {
this.paintView.setPicture(BitmapFactory.decodeStream(getContentResolver().openInputStream(data.getData())));
}
}
catch (FileNotFoundException e) {}
catch (Exception e){}`
称为“异常挤压”。这是坏习惯。这种事情掩盖了错误。1) 它通常允许异常引起的“损害”扩展到应用程序的其他部分。2) 它使准确诊断错误的真正原因变得更加困难
你正在挤压异常,这会让事情变得更糟
简而言之,如果您习惯性地挤压java.lang.Exception
:
- 您的代码可能不可靠
- 你不知道为什么,而且
- 你将无法修复它
try {
...
} catch (Exception e) {
// do nothing!
}
` switch (request) {
case R.id.album:
try{
if (result == -1) {
this.paintView.setPicture(BitmapFactory.decodeStream(getContentResolver().openInputStream(data.getData())));
}
}
catch (FileNotFoundException e) {}
catch (Exception e){}`
或者,您也可以将相同的消息传递给试图访问它的其他方法
随附条款:
protectedvoidonactivityresult(int请求、int结果、意图数据)引发FileNotFoundException{
//你的代码
}
如果您正在使用此方法,那么您必须看到,此异常在您正在使用的其他方法中被捕获。如果您试图访问文件或打开文件,则完全有可能发生FileNotFoundException 因此,您需要先捕获该错误,然后再捕获任何其他类型的异常 所以你的代码应该是这样的:
try {
...
} catch (Exception e) {
// do nothing!
}
` switch (request) {
case R.id.album:
try{
if (result == -1) {
this.paintView.setPicture(BitmapFactory.decodeStream(getContentResolver().openInputStream(data.getData())));
}
}
catch (FileNotFoundException e) {}
catch (Exception e){}`
或者,您也可以将相同的消息传递给试图访问它的其他方法
随附条款:
protectedvoidonactivityresult(int请求、int结果、意图数据)引发FileNotFoundException{
//你的代码
}
如果您正在使用此方法,那么您必须看到,此异常是在您正在使用的其他方法中捕获的。更具体地说,将抛出报告的异常。因为它不是运行时异常,所以您必须捕获它。我应该在哪里添加“try{…}catch(异常e){//donothing!}”您不应该在任何地方添加它。相反,你应该阅读我给你的链接,这样你就会明白为什么这样做是如此糟糕的做法。然后你会发现正确的做法是感谢很多兄弟我还没试过,因为现在是凌晨3点,但我相信它会帮助我的。你应该去睡觉。你不能在凌晨3点学习编程。因此,如果你在凌晨3点做作业,你什么也学不到。因为家庭作业的全部目的是学习,你是在浪费时间。更具体地说,将抛出报告的异常。因为它不是运行时异常,所以您必须捕获它。我应该在哪里添加“try{…}catch(异常e){//donothing!}”您不应该在任何地方添加它。相反,你应该阅读我给你的链接,这样你就会明白为什么这样做是如此糟糕的做法。然后你会发现正确的做法是感谢很多兄弟我还没试过,因为现在是凌晨3点,但我相信它会帮助我的。你应该去睡觉。你不能在凌晨3点学习编程。因此,如果你在凌晨3点做作业,你什么也学不到。因为作业的全部目的是学习,你是在浪费时间。如果你不知道这个错误发生在哪里,那么如果你没有在相册部分的打开输入流中看到它,请正确地查看编译器错误,它会提到行号和一些其他信息,else包括所有可疑的try块如果您不知道该错误发生的位置,那么如果您没有在相册部分的打开输入流中看到它,请正确查看编译器错误,它将提及行号和其他一些信息,否则包括所有可疑的try块